The previous section describes how to build a selenium system environment, so this section explains how to start writing the first automated test script.
selenium2.x the browser native API into the Webdriver API, you can directly manipulate the elements of the browser page, and even manipulate the browser itself (screenshots, window size, start, close, install plug-ins, configuration certificates, etc.), so just like the real user in the same operation. In fact, selenium is to simulate the user's behavior, to achieve automated testing.
Then we now implement a Baidu search selenium automated test cases, below our actual test process how to operate.
1. Open the browser;
2. Enter the URL "http://www.baiud.com";
3. Enter selenium within the search box;
4. Click "Baidu" to search selenium related information;
5. Determine if the search results are correct
The above procedure is actually our actual test process operation process, that is, our test cases, below we convert the text into code
#-*-coding:utf-8-*- fromSeleniumImportWebdriverImport Time fromSelenium.common.exceptionsImportNosuchelementexceptiondriver=Webdriver. Chrome () Driver.get ("https://www.baidu.com/")#open Browser, enter URLDriver.maximize_window () search_element= driver.find_element_by_id ('kw')#Position the input box seatSearch_element.send_keys ("Selenium")#Enter search informationButton_element = driver.find_element_by_id ('su')#Locate the Search button's seatButton_element.click ()#Click the Search buttonTime.sleep (1) Driver.save_screenshot ('Baidu.png')#Get search ResultsTime.sleep (1) Driver.quit ()#Close Browser
In this way, we have completed an automated test case to determine whether the result is correct, followed by the explanation of the assertion.
The following section describes how to locate elements.
Writing the first Python Selenium program (ii)