Automated testing-Selenium controls the operation of the browser

Selenium is a tool for testing web applications. Selenium tests run directly in the browser, just like real users are operating. Supported browsers include IE (7, 8, 9, 10, 11), Mozilla Firefox, Safari, Google Chrome, Opera, etc. The main functions of this tool include: test compatibility with browsers-test your application to see if it works well on different browsers and operating systems Test system functions-create regression tests to verify software functions and user needs. Supports automatic recording of actions and automatic generation of test scripts in .Net, Java, Perl and other languages.

Sometimes we want to make the browser open in a certain size, let the access page run in this size, or control the browser to go back and forward operations, etc.

Controlling the browser window size
Sometimes we need to evaluate the style of the mobile site, but the normal size of the browser is obviously inappropriate. At this time, we will want to set the browser to the size of the mobile terminal. WebDriver provides set_window_size () method to set the browser size:

from selenium.webdriver import Chrome
driver = Chrome(‘C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe’)
driver.get(‘https://www.baidu.com/’)
driver.set_window_size(480,800)

In addition to the set_window_size method to set the size of the browser, WebDriver also provides a method to set the browser full-screen display maximize_window (), its usage is the same as set_window_size (), except that maximize_window () does not need to pass parameters:

from selenium.webdriver import Chrome
driver = Chrome(‘C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe’)
driver.get(‘https://www.baidu.com/’)
driver.maximize_window()

It is worth noting that when positioning elements, if some web pages are not displayed in full screen, the positioning may be incorrect! ! !

Control the browser forward and backward
When using the browser to browse the web, the browser provides forward and backward buttons, you can easily switch between the web pages you have viewed, WebDriver also provides the corresponding back () and forward () Methods to simulate the back and forward buttons, here is a simple chestnut to demonstrate the use of these two methods:

from selenium.webdriver import Chrome
driver = Chrome ('C: \ Program Files (x86) \ Google \ Chrome \ Application \ chromedriver.exe')
driver.get (' https://www.baidu.com ')
driver.get (' https://blog.csdn.net/y472360651 ')
# Back to the Baidu page
driver.back ()
# Forward to the blog page
driver.forward ()

Refresh the page
Sometimes you need to manually refresh the (F5) page. WebDriver also provides the corresponding method refresh (), which is very simple to use, as follows:
driver.refresh ()

Window screenshots The
automation use case is executed by the program, so sometimes the error message printed is not very clear. If the screenshot of the current window can be saved when the script is executed incorrectly, the cause of the error can be seen very intuitively through the picture. WebDriver provides a screenshot function get_screenshot_as_file () to capture the current window:
driver.get_screenshot_as_file ("abc.png")
It is worth noting that when using the window screenshot function, the extension of the picture must be png.

Published 15 original articles · praised 7 · views 4015

Guess you like

Origin blog.csdn.net/weixin_43988159/article/details/95079412