robotframework+selenium for webui page automation testing

Robotframework is actually an automated framework. What kind of automated testing you want to perform, you need to add corresponding library files to this framework, and the selenium library is used for automated testing of webui pages.

 I will not talk about the construction of the robotframework framework here. Today, I will tell you about the related applications of the selenium library based on a login example. To use the selenium library, first import the information of this library. Where to import it?

That is to import in the operation page of the robotframework that we have installed, that is, ride.

Open ride and build projects in the following order:

1. Create a new test project

2. Create a new test suite and create a test suite under the already built project

3. Create a new test case, and create an automated test case under the already established suite

After construction, the structure is as follows:

Left-click the package name on the created directory, and the following page will appear:

Click the library label on the upper right, and the following page will pop up:

On this page, you can directly enter the file name of the selenium library in the name column and click OK, or you can click the browse button to select the file name of the selenium library. Generally, I directly enter the name to import.

The library file name entered here is Selenium2Library. After the import is successful, a black record will be displayed on the setting project page. The following figure is the display after importing multiple libraries. If the import fails, the library name will be displayed in red , at this time you have to check the log to find the reason, so I won’t say much here.

The library files can be imported under the suite or under the root directory. The scope of import used in different locations is different, and there is no difference in others.

After the library file is successfully imported, we can start writing the webui page automation test script.

Let's do a test by logging in to the 163 mailbox.

The test case for logging in to mailbox 163 looks like this:

The first is to open the 163 mailbox login page

Next, enter the correct account number and password

then log in

The last is to check whether the login was successful.

We write this automated test script based on the written test case steps and inspection results. The written script looks like this:

Combined with this script, I will tell you about the usage of these fields:

Teardown: This field can be understood as post-processing, which is a processing done after the automation script runs. When using the selenium library for automated testing, it is generally set to close browse, which is to close the browser after the automation use case script runs. .

Timeout: Timeout setting. The function of this setting is to prevent the automation script from getting stuck in a certain link when executing the test, and cannot proceed to the next script test. The setting of this time should be filled in according to the current time spent by your test script, and there is no fixed value.

These two are a preset value of the test script, and the following content is an actual test script for logging into the 163 mailbox.

The keywords in the blue font in the first column in the screenshot we saw are actually our corresponding actions.

Open browse: It is to open the browser. The second column corresponding to this line is the address of the 163 mailbox we want to log in, and the corresponding third column is the browser type. Our commonly used browser types include IE, chrome, firefox, etc. , this is filled in according to the actual situation. What I want to explain is that no matter which browser you input, in the python installation directory, there must be a corresponding driver file for this browser. The version of the driver file must be the same as that currently installed on your computer. The installed browser versions are consistent, otherwise an error will be reported when the script runs.

Sleep: After the action of opening the browser, I used a keyword sleep, which actually means to wait, because it takes a certain amount of time to open the browser webpage until the page is fully displayed, so here I used a sleep keyword, The time is an estimated time based on the complete display of the webpage. You can use the keyword wait until element contains to wait

Select frame.: After it is fully displayed, here is another select frame to select the window. Why sele uses this is because the selection of input account, password, and login uses such a nested one in the web page Format, if you don’t use the select frame to select it, no matter how correct the positioning of our account number and password input is, we cannot enter the account number and password to log in when the test is automatically executed.

So how to know that this page uses a frame, you have to look at the html code of the web page. Like this login page, when the account input box is selected, the corresponding position of its position code is marked blue. Follow this position to check its parent path, and find that there is an iframe tag, select the code of the iframe. tag, and find the entire account, password frame and the location where the login is located are all selected, indicating that these elements are under the iframe tag. If we want to locate these elements, we need to select them first and switch to them.

There are also nested tables in web pages that need to be selected and switched, and new web pages are opened through jump links. When positioning elements, you should pay more attention to the corresponding labels. When positioning elements, use the correct selection key Character.

For example, if you enter the hao123 webpage through a link from the Baidu homepage, because you are opening a new webpage, you will use the switch window here.

Input text: account number, password input, the second column corresponding to this line is the position corresponding to the account password box, how to locate this position, the element positioning in the selenium library is used here. CSS, XPath, name, id, etc. are commonly used for element positioning. I won’t go into details here. If you don’t know it, you can search it on the Internet.

The third column corresponding to these two rows is the corresponding username and password. Here I set the username and password as a variable, so when using it, the variable name is directly referenced.

The setting of variables is very simple. Right-click on the package or root directory, select new scalar in the pop-up menu, and in the next pop-up page, set the variable name and variable value and click OK, such as user name Variable settings:

Click element: This is an action of clicking the left mouse button, and the corresponding second column is the location path of this element. There are click button, click link, and click button keywords for left mouse button click, which are generally used when the element type is button. If the element type is not button type, using this keyword will not execute the mouse action.

Click link is generally used when this element contains a hyperlink, such as the hao123 picture on Baidu's homepage, it has a hyperlink attribute:

Well, following the above script, after the login action, another sleep is used here. In fact, keywords such as wait until can also be used here. This depends on personal habits. No matter which one is used to wait, this place is It takes a few seconds to proceed to the next step, for the same reason as above.

unselect frame: This is the same as select frame. After using the select frame keyword, it must be released after jumping out of this format.

Element should be enabled: This is a verification after a successful login, verifying whether the page has jumped to the page we expected, then the corresponding element position in the second column here can represent this page after the jump The position of an element.

Well, the relevant content of using the selenium library for webui page testing will be introduced here. Finally, let me tell you that there are many keywords in the selenium library. If you can’t remember which one to use and how to use it, you can press the F5 key on the ride use case writing page to view it.

A full set of practical tutorials for Web automation testing: Python+Selenium4 environment construction

Guess you like

Origin blog.csdn.net/dad22211/article/details/132190848