Currently, gradually understand the role of automation in order to solve the work is often repetitive motions. If an action must be used in each project, we can consider him automated.
As long as hands free, even to achieve a small function, also called automation.
Some slowly combing operation in the testing process can be automated.
scene one:
- Background: Currently playing android apk project package, you need to log jenkins account ---> select job ---> select build with parameters in the job details page, select the environment and branch ---> Click now to build
variable parameter:
runtime:【"DEV"、"TEST"、"PRE"、"RELEASE"】
branch:【origin/master-bug、origin/master、origin/NetHospital、origin/2.2.0R、origin/2.1.1R】需求
- Requirements: hands free, automate log on, select the corresponding job, build immediately.
- Code
import unittest from selenium import webdriverfrom time import sleep from selenium.webdriver.support.select import Select class jenkins_unpack(unittest.TestCase): @classmethod def setUpClass(cls): cls.driver = webdriver.Chrome() cls.driver.maximize_window() def test_001_jenkins_login(self): username = "root" passwd = "123456" # 打开url self.driver.get("http://localhost:8080/jenkins/login?from=%2Fjenkins%2F") self.driver.find_element_by_name('j_username').clear() self.driver.find_element_by_name('j_username').send_keys(username) sleep(3) self.driver.find_element_by_name('j_password').clear() self.driver.find_element_by_name('j_password').send_keys(passwd) sleep(3) self.driver.find_element_by_id('Gen1-YUI ' ) .click () SLEEP ( 10 ) DEF test_002_click_job (Self): "" " Click to enter the job details page from the list " "" self.driver.find_element_by_partial_link_text ( ' -doctor ' ) .click () SLEEP ( 10 ) DEF test_003_unpack (Self): "" " details page select the environment and to build a branch of the " "" self.driver.find_element_by_link_text ( ' build with the Parameters ' ) .click () SLEEP ( 5 ) # according to the index selected first - select the test environment = self.driver.find_element_by_xpath Runtime ( ' // * [@ ID = "main-Panel"] / form / Table / tbody [. 1] / TR [. 1] / TD [. 3] / div / SELECT ' ) # Environment [ "DEV", "tEST", "PRE", "RELEASE"] - by index query ok, the other two ways not tried the select (Runtime) .select_by_index ( " 1 " ) # according to the index selected first - choice NetHospital branch self.driver.find_element_by_xpath ( " // * [@ ID = 'SELECT'] / Option [. 4] " ) .click () self.driver.implicitly_wait ( . 5 ) # branch self.driver.find_element_by_xpath = ( '/ / * [@ id = "BRANCH-d6130d93-98b5-4095-a27e-1b868f289c9c"]/input') #Select (branch) .select_by_visible_text ( 'origin / NetHospital') # there have been problems with select # Click Start building self.driver.find_element_by_id ( ' YUI-Gen1-the Button ' ) .click () @classmethod DEF tearDownClass (CLS): CLS .driver.quit () IF the __name__ == ' __main__ ' : unittest.main ()
Related to knowledge
1.unittest.TestCase
run before each test case run: the setUp ()
tearDown (): executed after each test case runs out
setUpClass (): You must use @classmethod decorator, runs only run once before all the case
tearDownClass (): You must use @classmethod decoration is run only once after the completion of all case run
Article from: https://www.cnblogs.com/zpf1092841490/p/10114372.html
2.selenium eight targeting method --- a positioning element method
- driver.find_element_by_name () - the most common and simple
- driver.find_element_by_id () - the most common and simple
- driver.find_element_by_class_name()
- driver.find_element_by_tag_name () - most do not fly
- driver.find_element_by_link_text () - easy connection anchor text
- driver.find_element_by_partial_link_text () - easy connection anchor text
- driver.find_element_by_xpath () - the most flexible, versatile
- driver.find_element_by_css_selector()
Article from: https://www.cnblogs.com/VseYoung/p/selenium_location_python.html
3.selenium drop-down box processing
Take a TEST
from selenium.webdriver.support.select Import the Select Runtime = self.driver.find_element_by_xpath ( ' // * [@ ID = "main-Panel"] / form / Table / tbody [. 1] / TR [. 1] / TD [. 3 ] / div / SELECT ' ) the Select (Runtime) .select_by_index ( " . 1 " ) # 0 starts from index the Select (Runtime) .select_by_value ( " the TEST " ) based on the attribute values displayed as # = value "the TEST" the Select (Runtime). seletct_visible_text ( " the TEST " ) according to the text value of the positioning #
4. The following issue with the top 3 solutions, has been a problem. Js I think it is not the reason. I do not know js.
So using the method of comparative death:
self.driver.find_element_by_xpath("//*[@id='select']/option[4]").click()
self.driver.implicitly_wait(5)
The beginning of written code, debugging always reported when the element is not found, finally found the problem.
After the selected element, no click. click ()