What I want to share with you today is: some basic things about automated testing of selenium
Installation Environment
1. Python environment
After the installation is complete, enter "python" through the Windows command prompt CMD to check whether the installation is successful
2. Install setuptools and pip
setuptools is a sub-project of PEAK (
PythonenterpriseApplicationKit), which is an enhanced tool of python's distutilsde, which can make it easier to create and publish python packages, especially if they have dependencies on other packages;
pip is a tool for installing and managing python packages. It is very simple to install python packages through pip, which saves the tedious process. The installation of pip depends on setuptools. Before installing pip, you need to install setuptools first;
3. Download the selenium package
pipinstallselenium
4. Download the browser driver
Firefox and Google each have their own drivers
Download link:
https://www.seleniumhq.org/do…
Simple example
Example name: test_python_org_search.py
importunittest
#Introducing the unittest module is a built-in module of Python based on JAVAJUnit. This module provides a framework
To organize test cases
fromseleniumimportwebdriver
The #selenium.webdriver module provides all WebDriver implementations
fromselenium.webdriver.common.keysimportKeys
#Keys class provides all keyboard key operations
classPythonOrgSearch(unittest.TestCase):
#The test class inherits from unittest.TestCase. Inheriting the TestCase class tells the unittest module that this class is a
test case
defsetUp(self):
self.driver=webdriver.Firefox()
The #SetUp method is part of the initialization that creates an instance of FirefoxWebDriver
deftest_search_in_python_org(self):
driver=self.driver
driver.get(“http://www.python.org”)
The #driver.get method will open the website according to the URL address given in the method
self.assertIn(“Python”,driver.title)
#Use the assert assertion method to determine whether "Python" is included in the page title
elem=driver.find_element_by_name(“q”)
#Find the DOM node with name q
elem.send_keys(“pycon”)
#In the DOM node with name q, enter pycon
elem.send_keys(Keys.RETURN)
#press enter
assert"Noresultsfound."notindriver.page_source
deftearDown(self):
self.driver.close()
The #tearDown method will be executed after each test method is executed. This method can be used to do some cleaning, such as closing the browser. Of course, you can also call the quit method instead of the close
method
#Difference: quit will close the entire browser, but close
only one tab
if__name__==“main”:
unittest.main()
#entry function
Can be run directly in the shell:
pythontest_python_org_search.py
Explanation of common methods
1. Open a page
driver.get(“http://www.baidu.com”)
WebDriver will wait until the page is fully loaded (actually until the onload method has finished executing), then go back and continue executing your script. It's worth noting that if your page uses a lot of Ajax loading, WebDriver may not know when the page has fully loaded.
2. Interact with the page
WebDriver provides a number of methods to help you find elements, such as
<inputtype="text"name="passwd"id=“passwd-id”/>
You can find it by:
element=driver.find_element_by_id(“passwd-id”)
element=driver.find_element_by_name(“passwd”)
element=driver.find_element_by_xpath("//input[@id=‘passwd-id’]")
You can also find him by the text of the link, it should be noted that this text must match exactly. When you use XPATH, you have to take care that if more than one element is matched, only the first element is returned. If none of the above is found, a NoSuchElementException will be thrown.
Do something like
Type something into the textbox: element.send_keys("sometext")
Clear content: element.clear()
Select drop-down box: WebDriver's support class has a class called Select
fromselenium.webdriver.support.uiimportSelect
select.select_by_index(index) according to the order of options
select.select_by_visible_text("text") by text
select.select_by_value(value) according to its value
Deselect: select.deselect_all()
Submit selection: element.submit()
3. Drag and drop
element=driver.find_element_by_name(“source”)
target=driver.find_element_by_name(“target”)
fromselenium.webdriverimportActionChains
action_chains=ActionChains(driver)
action_chains.drag_and_drop(element,target).perform()
4. How to handle pop-up dialog boxes
alert=driver.switch_to_alert()
switch_to_alert()#Location popup dialog
text()#Get the dialog text value
accept()# is equivalent to clicking "confirm"
dismiss()# is equivalent to clicking "Cancel"
send_keys()#Input value, there is no input dialog for this alert and confirm, so it cannot be used here, so it can only be used here in prompt.
5. Manipulating cookies
Open a page driver.get("http://www.example.com")
Now set Cookies, this cookie takes effect in the root directory of the domain name ("/") cookie={'name':'foo','value':'bar'}driver.add_cookie(cookie)
Now get all Cookiesdriver.get_cookies() available under the current URL
6. Find elements
find_element_by_id
find_element_by_name
find_element_by_xpath
find_element_by_link_text
find_element_by_partial_link_text
find_element_by_tag_name
find_element_by_class_name
find_element_by_css_selector
Finally: [May help you]
These materials should be the most comprehensive and complete preparation warehouse for friends who are considering advanced [software testing] skills. This warehouse has also accompanied me through the most difficult journey, and I hope it can also help you.
Follow my WeChat public account [Program Yuanmuzi] to get it for free
My learning exchange group: 644956177 There are technical experts in the group to communicate and share~