Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

Save the little sister of the test with selenium in Python. Finally, she was very satisfied and rewarded me

Programmer two black 2022-01-15 04:45:16 阅读数:8 评论数:0 点赞数:0 收藏数:0

  • I'm erhei , A tester who's going bald

  • Welcome to thumb up Collection Leaving a message. Please correct any mistakes !

  • I collected some software testing data , Follow my official account : Programmer two black , Free collection !

  • The most difficult time , That's when we're not far from success !


The recently participated project has entered the centralized testing stage , Poor test girl, I have to input all kinds of data manually , I can't bear it . At this time, we need the program ape to come out , Use all kinds of high technology to save my little sister .

use python Version of selenium To implement a simple automation program is very simple , Specifically selenium There is no need to repeat the tutorial on the use of , The official website document describes it very clearly , A link is attached for your reference :https://www.selenium.dev/documentation/zh-cn/

Get down to business , The name of the project is AutoTest Configurable automated test tools ,python+selenium Configurable automated test tool , Just a little understanding of HTML And knowledge of related queries , You can customize the test process by writing the test process file , The form is automatically filled in , Button click , Screenshot of the process , Keyboard events , Browser cache operation and other functions , Currently only supported Chrome, IE browser .

Starting method

Development environment running

  • Please install the local environment in advance python3.8, Download the project to any local folder , Use pip install pipenv package , Use vscode After loading the project , Input at the console pipenv shell Establish an independent development environment , Then input pipenv install Complete the package installation required for the project .

  • Add the following locally lauch.json Startup file

{

"version": "0.2.0",
"configurations": [
{

"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/autotest.py",
"console": "integratedTerminal"
}
]
}
  • Click on F5 The project can be started .
    exe File run

  • After completing the development environment configuration , This can be done through the... Provided in the project run.bat File to generate exe Executable to start the project .

Instructions

Related configuration files

  • autotest.yaml Project global profile , Configure such as default launch browser , Whether to use agents, etc .
  • logger.yaml Log profile , Configure log level and output type
  • const.yaml Constant definition , Use... When customizing the test process

Process document definition description

  • The process document is json Format file , All files must be placed in the root directory of the project flowdata Under the folder , The file name is arbitrary , The prefix for cmn_XXXX.json The document will be used as a general process document , Provided to other process documents , It will not be loaded directly into the project executable process file .
  • As shown in the following code , Each key value pair represents an operation step , Dictionary keys have no specific meaning , It can be used as operation prompt and remark information ,type Specify operation type , there open It refers to the operation of opening a specific website ,targeturl Indicates the target web address ,option Indicates some additional actions after the current operation , For example { “type”: “wait”, “params”: 5} For pause 5 second .
{

" Jane, open the book ": {

"type": "open",
"targeturl": "https://www.jianshu.com/",
"option": {
 "type": "wait", "params": 5}
}
}

type The following types are included

  • open Open the specified URL .
  • Necessary attribute :targeturl Specify the destination site address ;
  • Optional attribute :
  • option Specify some additional operations after the current operation is completed , Include refresh Refresh the page ,wait pause ,close Close the browser ,switch Switch windows ,screenshot Screenshot ;
  • doafter Specify the sub operation after the operation is completed , The syntax is consistent with the parent node .

read Read specified mock Data files .

  • Necessary attribute :

    • findmethod Specifies the selector used by the page element , Include ID,XPATH,LINK_TEXT,PARTIAL_LINK_TEXT,NAME,TAG_NAME,CLASS_NAME,CSS_SELECTOR;
    • targeturl Specify a read target mock File address .mock File as a common data file to be used in the process , Put it all together in mockdata In the folder ,mock The file format is also json Format , The dictionary key corresponds to findmethod Matching query expression .
  • Optional attribute :option,doafter.

click Process page elements and click actions .

  • Necessary attribute :findmethod,target Specify and findmethod Matching query expression ;
  • Optional attribute :
    • count Specify the number of hits , The default is 1 Time ;
    • index Find the corresponding element from the matching element set by specifying the index value
    • option;
    • doafter.

dbclick Handle the double-click operation of page elements .

  • Necessary attribute :findmethod,target;
  • Optional attribute :option,doafter.

alert A dialog box will pop up to confirm the operation . In the system, sometimes when you click a key button, a prompt box for confirmation of relevant operation will appear , Confirm to enter the next node by configuring this node by default .

  • Optional attribute :option,doafter.

copy Copy the matching values to the clipboard , For future use .

  • Necessary attribute :

    • By designation findmethod,target Combine , You can copy page specified element values to the clipboard ;
    • By designation itemval, You can copy its corresponding value to the clipboard ,itemval Support setting constant values , Such as copy Operation configuration in a loop , Can also pass Appoint format To complete the dynamic value setting .
  • Optional attribute :option,doafter.

paste Sets the clipboard value to the specified element of the page .

  • Necessary attribute :findmethod,target;
  • Optional attribute :option,doafter

for Cyclic operation .

  • Necessary attribute :

    • By designation startidx,endidx Combine , Completes the index cycle for the specified range , Its current index value can be provided as a dynamic value to the child nodes in the loop body ;
    • By designation findmethod,target Combine , Complete the circular operation on the page matching element set , The current element value can be provided to the child nodes in the loop body . When the circulatory system click When the child node does not specify an element , The current element in the loop is used for the click operation .
    • flow Defines the set of child nodes to be processed by the loop body .
  • Optional attribute :option,doafter

cache Set browser cache .

  • Necessary attribute :
    • cachekey Specifies the browser cache key ;
    • itemval Specify the value corresponding to the cache key .
  • Optional attribute :option,doafter

setval The difference in read Multi item read , This operation is to set the value of the page single item , It is mainly used to set upload files , Dynamic value setting, etc .

  • Necessary attribute :findmethod,target, itemval;
  • Optional attribute :option,doafter

flowfile Load common process template file .

  • Necessary attribute :targeturl;
  • Optional attribute :option,doafter

keyboard Keyboard operation . Mostly used for clicking on the keyboard PAGE_DOWN Event completion, page scrolling, etc , Keyboard key combinations are not supported .

  • Necessary attribute :
    • By designation findmethod,target Combine , Apply the corresponding keyboard operation on the specified page element .
    • itemval Specify keyboard key values ;
  • Optional attribute :option,doafter

Other attribute description

  • option Including parameterless commands , There are commands , You can also include multiple commands in the form of an array .
    • No parameter command :

      • {“option”:“refresh”} Refresh the browser
      • {“option”:“close”} Close the browser
      • {“option”:“screenshot”} Screen capture of the currently displayed window
    • There are commands

      • {“option”: { “type”: “wait”, “params”: 5} } Execution wait , Waiting time by params Parameter assignment . It is mostly used to solve the problem of slow page loading .
      • {“option”: { “type”: “switch”, “params”: “new”} } Switch to a new window ;{“option”: { “type”: “switch”, “params”: “origin”} } After closing the new window , Switch back to the original window
      • Capture the full screen of the page ,params Specify an array of parameters , The first parameter is the floating element to be processed in the page ,xpath Specify how to query elements , The current support id=xxx,class=yyy, tag=zzz etc. 3 Ways of planting ,display_page Indicates where the floating element is to be displayed , In general, floating elements such as the head of a brief book need to be displayed on the first page of the screenshot, and it is set to first, If the return to top button of the brief book needs to be displayed on the last page of the screen capture, set it to last, The full screen capture function is only available through Chrome test ; The second parameter specifies whether the page needs to be preloaded , For example, the list of articles on the front page of a simple book is loaded with scrolling , If you need to capture the full screen, you need to set it to True, On the contrary, the page loaded at one time is set to False.
{

"type": "screenshot",
"params": [
[
{
 "xpath":"tag=nav", "display_page":"first"},
{
 "xpath":"class=side-tool", "display_page":"last"}
],
"True"
]
}

bat The document states

  • run.bat After execution, it will generate... In the project root directory dist Folder , It contains the project executable program exe And other relevant documents . Before packaging , Please enter first pipenv After the created independent environment , Please execute the command again pip install pyinstaller Install the corresponding package .
  • zip.bat Use windows Comes with compression software , Compress the compressed file used by the browser proxy plug-in , Please modify... Before use proxy Agent configuration file under file background.js, direct cmd Enter the project directory and enter zip Command execution .

Simple book praise process file analysis

{

" Jane, open the book ": {

"type": "open",
"targeturl": "https://www.jianshu.com/",
"option": {
 "type": "wait", "params": 5}
},
" add to cookie": {

"type": "cache",
"cachekey": "remember_user_token",
"itemval": " Enter... After account login token value ",
"option": ["refresh", {

"type": "wait",
"params": 5
}, {

"type": "screenshot",
"params": [
[
{
 "xpath":"tag=nav", "display_page":"first"},
{
 "xpath":"class=side-tool", "display_page":"last"}
],
"True"
]
}]
},
" Cycle through the article list ": {

"type": "for",
"findmethod": "CSS_SELECTOR",
"target": "#list-container>ul.note-list>li>div.content>a.title",
"flow": {

" Open the article ": {

"type": "click",
"option": {

"type": "switch",
"params": "new"
}
},
" I like the article ": {

"type": "click",
"findmethod": "CLASS_NAME",
"target": "_2VdqdF",
"index": 0,
"option": {

"type": "switch",
"params": "origin"
}
}
}
}
}

The process file contains 3 Nodes , The first node represents opening the profile website ; The second node operates the cache , The Jianshu website will be cached in the browser after you log in remember_user_token Record your token value , You can log in automatically by manually adding the cache , At the same time, after adding the cache , Additional operations need to be performed in order , Refresh the browser to make the login status effective , Capture the full screen of the article list ( Another purpose here is to load all articles for subsequent likes ); The third node passes through CSS_SELECTOR Selectors , Find a list of all articles , And cycle the list to complete the subsequent likes . The child node opens the article , Because there is no way to find matching elements , By default, you will click the current element of the loop, that is, the current article , Complete switching to a new window in additional operations , When the sub node article likes, click the like button , In additional operations, close the current window and switch back to the main window .

The following is the startup interface after the program runs , Select the process file , Click Start .


The rescued little sister was very satisfied , Rewarded a chicken leg .

Finally, for the convenience of everyone to learn the test , I specially prepared a 13G Super practical dry goods learning resources , The content involved is very comprehensive .


Include , Software learning Roadmap ,50 Video of class for many days 、16 It's a real assault project ,80 More than one software for testing ,37 A test document ,70 A software testing related problem ,40 Test experience level article , Thousands of test questions to share , also 2021 Software testing interview Dictionary , There's also a selection of resumes for software testing jobs , I hope that's helpful ……

Follow my official account :【 Programmer two black 】 You can get this information !

If you don't want to experience it again, you can't find information when you study on your own , No one answers the question , If you insist on giving up after a few days , You can join our group :785128166 Let's talk about it , There are also various software testing materials and technical exchanges .

Recommended reading

High paid programmers can't escape 35 Year old … When ability is out of step with age , How can we help ourselves

College graduates start selling … Not reconciled to the status quo , The road to self-help in the transition test

From crown sales to unemployment , Finally, choose software testing , Looking back at this section of the road , I was lucky !

Copyright statement
In this paper,the author:[Programmer two black],Reprint please bring the original link, thank you

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;

支付宝红包,每日可领