"Teaching you hand in hand" series of basic articles 2-python+ selenium automated testing-opening and closing the browser (detailed)

1 Introduction

This section introduces how to initialize a webdriver instance object driver, and then open and close the firefox browser. To open the fiefox browser with selenium. First, you need to download a driver plug-in geckodriver.exe, download address https://github.com/mozilla/geckodriver/releases, after downloading the exe file, put this file in

Under your python installation directory, for example: C:\Python27\geckdriver.exe.

As a beginner, it is not recommended to use the Python IDE tool here, just choose a powerful notepad editing tool, such as Notepad++. When you have accumulated enough in the future, you need to write more files, or a simple framework design later, we will introduce and Use the IDE tool Pycharm.

In the last article, after installing selenium in a hurry, I did not introduce it to my friends. Today, I will popularize selenium to my friends and students who are not clear.

2. Basic introduction to Selenium

Selenium` is an open source automated testing tool. It is mainly used for automated testing of web applications, not limited to this, and supports automation of all web-based management tasks.

2.1 Introduction of Selenium official website

Selenium is a suite of tools to automate web browsers across many platforms.

runs in many browsers and operating systems

can be controlled by many programming languages and testing frameworks.

Selenium official website: Selenium

Selenium Github homepage: https://github.com/SeleniumHQ/selenium

Selenium is a popular framework for testing web application user interfaces (UI). It is a powerful tool for running end-to-end functional tests. You can write tests in several programming languages ​​and Selenium can execute them in one or more browsers.

Selenium has gone through three versions: Selenium 1, Selenium 2 and Selenium 3. Selenium is not a simple tool, but consists of several tools, each of which has its own characteristics and application scenarios.

Selenium was born in 2004 when Jason Huggins at ThoughtWorks was testing an internal application. Being a smart guy, he realized that his time should be better spent than manually testing every change. He developed a Javascript library that can drive interactive pages, enabling multi-browser

Automatically return test results. That library eventually became the core of Selenium, which is the basis for all functionality of Selenium RC (remote control) and Selenium IDE. Selenium RC is groundbreaking because no other product lets you control your browser using your favorite language. This is Selenium 1.

However, because it uses a Javascript-based automation engine, and browsers have many security restrictions on Javascript, some things are difficult to achieve. To make matters worse, web applications are becoming more and more powerful, and they use all kinds of features provided by new browsers, all of which make these limitations painful.

In 2006, a Google engineer, Simon Stewart started development based on this project, which was named WebDriver. At this point, Google was already a heavy user of Selenium, but test engineers had to work around its limitations to work with the tool. Simon needs a native method that works across browsers and operating systems

A testing tool that directly communicates with the browser to solve the problem of Javascript environment sandbox. The goal of the WebDriver project is to solve Selenium's pain points.

In 2008, the Selenium and WebDriver projects merged. Selenium has rich community and commercial support, but WebDriver clearly represents the wave of the future. The combination of the two provides a common set of functionality for all users and draws on some of the brightest minds in test automation. This is Selenium 2.

In 2016, Selenium 3 was born. Removed Selenium RC from Selenium 1 which is no longer used, and officially rewrote all browser drivers.

2.2 Selenium toolset

2.2.1 Selenium IDE

Selenium IDE (Integrated Development Environment) is a prototyping tool for creating test scripts. It is a Firefox plug-in, which realizes the recording and playback function of simple browser operations, and provides a suggested interface for creating automated tests. Selenium IDE has a recording function that can record user actions and export them to a reusable

in the script for subsequent execution.

2.2.2 Selenium RC

Selenium RC is the core tool of the selenium family. Selenium RC supports many different languages ​​to write automated test scripts, and uses the selenium RC server as a proxy server to access the application to achieve the purpose of testing.

Selenium RC uses sub-Client Libraries and Selenium Server.

The Client Libraries library is mainly used to write test scripts to control the library of selenium Server.

Selenium Server is responsible for controlling browser behavior. In general, Selenium Server mainly includes 3 parts: Launcher, Http Proxy, Core.

2.2.3 Selenium Grid

Selenium Grid enables the Selenium RC solution to increase the processing power for large test suites or test suites that need to run in multiple environments. Selenium Grid allows you to run your tests in parallel, that is, different tests can run on different remote machines at the same time. This does two things, first, if you have a

For a large test suite, or a test suite that runs very slowly, you can use Selenium Grid to divide your test suite into several parts and run it on several different machines at the same time, which can significantly improve its performance. Also, if you have to run your test suite in multiple environments, you can get support for multiple remote machines, which will run concurrently

your test suite. In each case, Selenium Grid can significantly reduce the processing time of your test suite through parallel processing.

2.2.4 Selenium WebDriver

WebDriver is the main tool promoted by Selenium 2. In fact, WebDriver is a substitute for Selenium RC, because Selenium needs to retain backward compatibility. In Selenium 2, Selenium RC has not been completely abandoned. If you use Selenium to develop a new automated testing projects, then we strongly recommend using

Selenium2's WebDriver for coding. Also, in Selenium 3, Selenium RC was removed.

3. Browser driver driver installation

3.1 Browser driver download

The download of the driver should be a tricky part, and you must pay attention to the browser version.

Enter the download interface from the selenium official website , please pull down at this time, although the third-party browsers are not officially developed by selenium, but you can find the driver download link corresponding to the browser supported by selenium on the selenium official website, because I am locally The browser version used is relatively new, so the corresponding driver version can also download the latest version.

The following are the download links for the three most commonly used browsers:

Google Chrome driver:https://sites.google.com/a/chromium.org/chromedriver/downloads

Mozilla GeckoDriver:https://github.com/mozilla/geckodriver/releases

Internet Explorer Driver:http://selenium-release.storage.googleapis.com/3.13/IEDriverServer_x64_3.13.0.zip

3.2 browser driver installation

The downloaded zip file is decompressed to the python installation directory, which can be placed in: C:\Users\Administrator\AppData\Local\Programs\Python\Python36, but it is recommended to be placed in the scripts directory: C:\Users\Administrator\AppData \Local\Programs\Python\Python36\Scripts

 After completing the above work, we can test and use selenium to drive the browser

4. Test Drive Browser

4.1 Start python in CMD and import webdriver package from selenium

from selenium import webdriver

4.2 Drive chrome browser

Open the Chrome browser, visit the Google website, and then close the Chrome browser.

Reference Code

# coding=utf-8

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2019-11-25
@author: 
Project: python+ selenium-打开和关闭浏览器
'''

# 3.导入模块
from selenium import webdriver
Ch_driver = webdriver.Chrome()
Ch_driver.get("https://www.google.com")
Ch_driver.quit() # 使用quit()关闭了chrome并结束了此次测试,如果是close()只是关闭chrome,后台仍在进行。

4.3 Drive Firefox browser

Open the Firefox browser, visit the Google website, and then close the Firefox browser. Reference Code

​

# coding=utf-8

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2019-11-25
@author: 
Project: python+ selenium-打开和关闭浏览器


'''

# 3.导入模块
from selenium import webdriver
Fi_driver = webdriver.Firefox()
Fi_driver.get("https://www.google.com")
Fi_driver.quit()
​

4.4 Drive IE browser

Open the IE browser, visit the Google website, and then close the IE browser.

Reference Code

# coding=utf-8

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2019-11-25
@author:
Project: python+ selenium-打开和关闭浏览器
'''

# 3.导入模块
from selenium import webdriver
Ie_driver = webdriver.Ie()
Ie_driver.get("https://www.google.com")
Ie_driver.quit()

It all seems to drive the browser normally and open the webpage, then close and exit the browser. In this way, we have completed the preparations for opening and closing the browser for selenium automated testing~

Note: possible errors

Exception: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.

Solution: Internet Options->Security; set the security interface of the Internet site, local Intrant, and trusted site to the same level, for example, they are all set; run the code again to open Baidu with IE.

Summary: At present, due to the instability and speed of IE, it is recommended to use Firefox or Google Chrome to test and debug automation scripts.

4.5 Summary

The above code can be written in a file and then executed. Careful friends or children's shoes feel that the window is a bit small when you first open the browser, you can maximize the browser through code.

Open a blank file with Notepad++, enter the following code, and save it as first.py

# coding=utf-8

# 1.先设置编码,utf-8可支持中英文,如上,一般放在第一行

# 2.注释:包括记录创建时间,创建人,项目名称。
'''
Created on 2019-11-25
@author: 
Project: python+ selenium-打开和关闭浏览器
'''

# 3.导入模块

from selenium import webdriver # 导入webdriver包

driver = webdriver.Firefox() # 初始化一个火狐浏览器实例:driver

driver.maximize_window() # 最大化浏览器

driver.get("https://www.baidu.com") # 通过get()方法,打开一个url站点

driver.quit() #关闭并退出浏览器

Open the cmd window, cd to the path where first.py is located, enter the command in cmd: python first.py and press Enter, you can see whether Firefox is opened, Baidu is opened, and Firefox is closed.

summary

Through the above code, and this article, I basically understand how to open and close the browser. If you want to open IE or Chrome browser, you also need to download the driver.exe file corresponding to the browser and put it in the python installation directory.

Note: You may encounter some errors

1. Geckodriver.exe is not placed in the python installation directory, and this type of error is reported.

2. After python first.py is executed, the file is found, indicating that the path to the folder where first.py is located has not been switched to through the cd command.

Guess you like

Origin blog.csdn.net/nhb687096/article/details/131639779