Python combined with Selenium to simply implement Web automated testing

This article is an entry-level application of Python + Selenium for automated testing, including environment construction and simple examples. It has certain reference value. Interested friends can refer to it

This article is an entry-level application of Python + Selenium for automated testing, including environment construction and simple examples. The basic idea is to use the Firefox Selenium IDE plug-in to record scripts and generate Python code, and use Python to execute scripts in conjunction with Selenium and browser drivers. The operating system is Windows. If you don’t understand something during the process, remember to download it from Baidu, such as how to open the Windows CMD command line, how to set environment variables, etc., because the premise of this article is that the reader has certain basic knowledge.

To install Python, visit its official website www.python.org to download the latest version. It is recommended to choose the version of Python 3 (do not choose the embeddable version). When installing, remember to check two items: add Python to environment variables and install pip. Be sure to select these two items to save some trouble for the following steps.

​After the installation is complete, enter python and pip on the CMD command line to verify whether the installation is correct. If there is no prompt "not an internal or external command", it means that the installation is normal, otherwise it may not be added to the environment variable.

The easiest way to install the python selenium package is to enter python -m pip install selenium on the CMD command line to install it. Note that there is a pip here, which is the reason for checking the installation of pip above. In the figure below, because my environment has already been installed, it prompts already satisfied.

In addition to the above pip installation, you can also directly download the selenium installation package of python, which can be downloaded from the selenium official website ( http://www.seleniumhq.org/download/ ). After downloading, install it with python setup.py install on the command line. This method will not be introduced here.

Install Selenium IDE for Firefox. This step is to record the script with Firefox, but it is not necessary, because you can also directly write the script manually, but it is more convenient for beginners to record. Open Firefox, search for Selenium IDE in the add-ons, find the components shown in the figure, and install them.

After restarting Firefox, you can see the option of Selenium IDE in the Tools menu, indicating that the installation is successful.

Next, we need to download some browser drivers (webdriver). Note that Firefox is natively supported by Selenium, but it seems that not all Firefox versions support it, so we need to find some drivers. In addition, it seems that Firefox is not stable in execution, and Google's Chrome browser is better, so we choose Chrome here. Its drivers can be found on the Selenium official website, including IE and other browsers.

​The two marked in the above figure are used for Firefox and Chrome respectively. After downloading and saving, add them to the environment variables. For convenience, it is recommended to directly put them in the directory where Python was installed before. Note that you must ensure that the directory of the driver is added to the environment variable, otherwise it cannot be executed.

​At this point, the simple Selenium environment is ready. Let's take Baidu as an example for a simple demonstration.

1. Open Firefox, start Selenium IDE

​2.
The Selenium IDE window pops up, enter the Base URL http://www.baidu.com , and the recording button on the right keeps the recording open (already opened by default)

3. Enter http://www.baidu.com in the address bar of the browser , click search, and you can see that the script information has been generated in Selenium, you can click the green button in the upper left corner to test it

4. Assuming that our recording has ended at this time, then export the script as python code, select File>Export Test Case As>Python, note that this is python 2, generally it does not matter, if your python environment is python 3, sometimes you may need to make some changes to the code

5. Save it as a python source file, suffixed with .py

6. Open the generated code, the default is to use the Firefox browser as the driver, it is recommended to change it to Chrome

7. At this time, you can execute the test through python, open the CMD command line, enter the directory where the python code was stored just now, and enter python test.py to execute

8. If the previous steps are correct, the browser will be opened automatically, and Baidu will be opened to search. If there is a mistake, it must be eliminated step by step. It is not clear in a few words. In addition, it is recommended to use Sublime Text 3, open the py file directly after installation, and press Ctrl+B to execute.

This article only briefly explains the construction of the Selenium environment. Selenium has many other things, such as element positioning, distributed tasks, etc., which are not within the scope of this article.

This is the end of this article about Python combined with Selenium to easily implement Web automation testing!

The following is the supporting information. For friends who do [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse also accompanied me through the most difficult journey. I hope it can help you too!

Software testing interview applet

The software test question bank maxed out by millions of people! ! ! Who is who knows! ! ! The most comprehensive quiz mini program on the whole network, you can use your mobile phone to do the quizzes, on the subway or on the bus, roll it up!

The following interview question sections are covered:

1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux

6. web, app, interface automation, 7. performance testing, 8. programming basics, 9. hr interview questions, 10. open test questions, 11. security testing, 12. computer basics

Information acquisition method:

Guess you like

Origin blog.csdn.net/myh919/article/details/131868099