Selenium browser interaction principle and application, play with Web automation testing

 Table of contents

 Foreword:

Browser interaction:

Selenium's implementation:

Selenium WebDriver:

WebDriver's waiting mechanism:

Summarize:

 Web automated testing:


 Foreword:

Web automated testing is an essential link in modern software development, it can help developers quickly and automatically test web applications, thereby greatly shortening the testing cycle, improving testing efficiency, and reducing testing costs. Selenium is a widely used web automation testing tool, which provides a rich API, which can easily simulate user operations in the browser, such as click, input, selection, etc., and automate the testing of web applications.

Browser interaction:

Before introducing the principles of Selenium, let's take a look at browser interaction. The communication between the web application and the browser is through the HTTP protocol. Simply put, the client (browser) sends a request to the server (web server), and the server returns a response. When the client receives the response, it will display it according to the content of the response. During this process, the browser parses and displays Web pages based on Web technologies such as HTML, CSS, and JavaScript.

Selenium uses the driver provided by the browser to simulate the user's operation in the browser. In Selenium, different browsers require different drivers. For example, Chrome browser needs to use ChromeDriver, Firefox browser needs to use GeckoDriver, Edge browser needs to use EdgeDriver, etc. Selenium interacts with the browser through the driver, simulating the user's operations in the browser, such as opening a web page, clicking a link, entering text, submitting a form, and so on.

Selenium's implementation:

Let's take a look at the implementation of Selenium. Selenium provides three implementations, namely Selenium RC, Selenium IDE and Selenium WebDriver. Selenium RC is the earliest Selenium tool, which simulates the user's operations in the browser by embedding JavaScript scripts in the browser. Selenium IDE is a visual tool that can record user operations in the browser and generate automated test scripts. Selenium WebDriver is the latest implementation of Selenium. Due to its powerful functions and flexibility, it has become the mainstream tool for current Web automation testing.

Selenium WebDriver:

Selenium WebDriver is implemented based on the W3C Web Driver protocol, which provides various APIs that can easily simulate user operations in the browser. For example, we can implement the following sample code using WebDriver:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

# 创建Chrome浏览器对象
driver = webdriver.Chrome()

# 打开百度首页
driver.get("http://www.baidu.com")

# 在搜索框中输入关键字
elem = driver.find_element_by_name("wd")
elem.send_keys("Selenium Webdriver")

# 模拟回车键
elem.send_keys(Keys.RETURN)

# 关闭浏览器
driver.close()

In this code, we first create the Chrome browser object, then open the Baidu homepage, search for the keyword "Selenium Webdriver", and finally close the browser. Among them, the `driver.get()` method is used to open the specified URL, the `driver.find_element_by_name()` method is used to find the element with the specified name, and the `elem.send_keys()` method is used to enter text in the text box. The `elem.send_keys(Keys.RETURN)` method simulates the Enter key, and the `driver.close()` method is used to close the browser.

In addition to the above APIs, Selenium WebDriver also provides many other APIs, such as `driver.find_element_by_id()`, `driver.find_element_by_xpath()`, `driver.find_element_by_css_selector()`, etc., which can be based on element ID, XPath or CSS The selector finds the specified element. In addition, you can also click on the element (`elem.click()`), get the attribute of the element (`elem.get_attribute()`), get the text of the element (`elem.get_text()`), etc.

When using Selenium, you need to pay attention to some details. For example, the path to the driver needs to be specified in the program (`webdriver.Chrome("path/to/chromedriver")`), otherwise Selenium cannot start the browser. At the same time, you need to pay attention to the corresponding relationship between the browser version and the driver version, otherwise there will be compatibility problems.

WebDriver's waiting mechanism:

In addition, Selenium also provides some advanced features, such as WebDriver's waiting mechanism. In web automation testing, it is necessary to wait for the page loading to complete before proceeding to the next step, otherwise the test may fail. WebDriver provides two waiting methods: implicit waiting (`driver.implicitly_wait()`) and explicit waiting (`WebDriverWait`). The implicit waiting method will automatically wait for the page to load within the specified time, while the explicit waiting method needs to specify the waiting conditions, and the next step will not be performed until the conditions are met. For example, the explicit wait method in the following sample code waits for an element on the page to appear before clicking:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

# 创建Chrome浏览器对象
driver = webdriver.Chrome()

# 打开百度首页
driver.get("http://www.baidu.com")

# 在搜索框中输入关键字
elem = driver.find_element_by_name("wd")
elem.send_keys("Selenium Webdriver")

# 等待搜索结果出现,并点击第一个结果
wait = WebDriverWait(driver, 10)
elem = wait.until(EC.presence_of_element_located((By.XPATH, '//div[@class="result c-container"][1]//a')))
elem.click()

# 关闭浏览器
driver.close()

In this sample code, we first create the Chrome browser object, then open the Baidu homepage, search for the keyword "Selenium Webdriver", wait for the first link in the search results to appear, and finally click the link. Among them, `WebDriverWait` sets the maximum waiting time to 10 seconds, and `EC.presence_of_element_located` is used to specify the waiting condition, that is, the next step will not be performed until at least one element meeting the condition appears on the page.

Summarize:

To sum up, Selenium is a powerful web automation testing tool that can simulate user operations in a browser to automate testing of web applications. Selenium WebDriver is the latest implementation of Selenium. It provides rich APIs and advanced functions, which can help developers quickly build web automation test scripts, and greatly improve the efficiency and reliability of web automation testing.

[Automated test communication]: 574737577 icon-default.png?t=N3I4http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=NMRVNWflxt3xkgJD_Cj1eSi6GHgTNQAw&authKey=G4Z6oltN4M9aCbBQfUODeoKPeKUsDSGmyxsSOXuwLjjN%2BBt m5ZJD3KeOsXJHwpC%2F&noverify=0&group_code=574737577

automated test:

 Web automated testing:

Automated Testing - Benefits:

Guess you like

Origin blog.csdn.net/Free355/article/details/130724742