Today to explain to you is the analysis of automated test examples, if there is a wrong place, please give more advice.

The following is an example of an automated test:

from selenium import webdriver     from selenium.webdriver.common.keys import Keys     driver = webdriver.Chrome()     driver.get("https://pypi.org/")    print(driver.title)     assert'Python' indriver.title     elem = driver.find_element_by_name('q')     elem.clear()     elem.send_keys('deniro')     elem.send_keys(Keys.RETURN)     assert'No results found.' not indriver.page_source     driver.close()Copy the code

The analysis is as follows:

(1) Import webDriver and Keys modules first. The Selenium. Webdriver module contains the implementation of the driver browser. Supports Firefox, Chrome, and Internet Explorer. The Keys module contains Keys from the keyboard, such as F1, ALT, and so on.

  from selenium import webdriver

   from selenium.webdriver.common.keys import Keys
Copy the code

Next, create an instance of Chrome WebDriver.

 driver = webdriver.Chrome()

Copy the code

The driver.get method opens the specified URL page in the browser. When the page is fully loaded (triggering the “onload” event), control is handed over to the test script we wrote.

Note: If you load a page that uses a lot of AJAX requests, the WebDriver may not know when it is fully loaded.

(3) Then there is an assertion that tests the page title (i.e. “Python” string in H5) :

assert 'Python' in driver.title
Copy the code

(4) WebDriver provides several ways to find elements, such as using find_element_by_*. For example, the find_element_by_name method is used to find input box elements by the name attribute of their tag.

 elem = driver.find_element_by_name('q')
Copy the code

(5) Then, we simulate keyboard operation, input characters in the input box. The special characters you can use selenium.webdriver.com mon. Keys to input, such as the example, the carriage return. Some input fields have set defaults, so we first clear the defaults in the input fields to avoid affecting the expected results:

elem.clear()     elem.send_keys('deniro')     elem.send_keys(Keys.RETURN)Copy the code

If software testing, interface testing, automated testing, interview experience exchange. If you are interested, you can add software test exchange: 1085991341, and there will be technical exchanges with peers.

(6) After the press Enter, the browser will call the back-end service, perform the search operation, and finally return the search results. We can test the characters we want or don’t want to see in the returned page source code:

 assert 'No results found.' not in driver.page_source
Copy the code

(7) Close the browser. You can call the quit method to do something similar, but there are some differences. The quit method exits the entire browser, while the close method closes only one TAB in the browser. If you open only one TAB, most modern browsers close the entire browser.

 driver.close()
Copy the code

Above, hope to help you, have been helped to the friends welcome to praise, comment.