Selenium is currently the mainstream Web automation tools, providing a variety of browser support (Chrome,firefox, IE, etc.), of course, you can also use their favorite language (Java,c#,python, etc.) to write use cases, it is easy to get started. When you finish writing your first automated use case, you must feel "wow ... Good cow x ", but everyone with the peripheral scan code, the heart may be collapsed, because it is too messy! Like this:
__author__='Xua' fromSeleniumImportWebdriver fromSelenium.webdriver.common.keysImportKeysImportUnitTestclassTcrepeatlogin (unittest. TestCase):defsetUp (self):#WebdriverSelf.driver = Webdriver. Chrome (R'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe') self.driver.implicitly_wait (30) Self.base_url="http://10.222.30.145:9000/" defTest_ (self): driver=self.driver driver.get (self.base_url)#enter username and passwordDRIVER.FIND_ELEMENT_BY_ID ("username"). Clear () driver.find_element_by_id ("username"). Send_keys ("Sbxadmin") driver.find_element_by_id ("Password"). Clear () driver.find_element_by_id ("Password"). Send_keys ("IGTtest1"+Keys.return)#Find dialog and checkDialogTitle = Driver.find_element (By.xpath,'//html/body/div[7]/div/div/div[1]/h3') self.assertequal (" Sign In", Dialogtitle.text)#Find Cancel button and clickCANCELBTN = Driver.find_element (By.xpath,'//html/body/div[7]/div/div/div[3]/button[2]') Cancelbtn.click ()defTearDown (self): Self.driver.close ()if __name__=="__main__": Unittest.main ()
From a few points to analyze the next top of the code:
1. Legibility: very difficult to understand. So many find element? Is this also test case?
2. Scalability: are isolated test case, no extensibility to speak of
3. Reusability: No public method, difficult to refer to reuse
4. Maintainability: Once the page element is modified, it is necessary to modify all relevant use cases accordingly, effort large
Based on the above questions, Python gives us the page pattern to manage the test, which is probably the case:
About page mode:
1. Abstract a basepage base class that contains a property that points to Selenium.webdriver
2. Each webpage is inherited from the BasePage base class, through the driver to get the elements of this page, each page operation is abstracted into a method
3. TestCase inherits from the Unittest.testcase class and relies on the corresponding page class to implement the corresponding test case step
Using the page pattern to implement the above use case, the code is as follows:
basepage.py:
__author__='Xua' fromSelenium.webdriver.common.byImport by fromSelenium.webdriver.common.keysImportKeys#Super ClassclassBasePage (object):def __init__(self, driver): Self.driver=Driver
classLoginPage (basepage):#page Element IdentifierUsename = (By.id,'username') Password= (By.id,'Password') DialogTitle= (By.xpath,'//html/body/div[7]/div/div/div[1]/h3') CancelButton= (By.xpath,'//html/body/div[7]/div/div/div[3]/button[2]') #Get username textbox and input username defSet_username (self,username): Name= Self.driver.find_element (*loginpage.usename) Name.send_keys (username)#Get password textbox and input password, then hit return defSet_password (self, password): pwd= Self.driver.find_element (*loginpage.password) pwd.send_keys (password+Keys.return)#Get pop Up dialog title defGet_diaglogtitle (self): Digtitle= Self.driver.find_element (*loginpage.dialogtitle)returnDigtitle.text#Get "Cancel" button and then click defClick_cancel (self): cancelbtn= Self.driver.find_element (*Loginpage.cancelbutton) Cancelbtn.click ()
test_login.py:
__author__='Xua' fromSeleniumImportWebdriver fromSelenium.webdriver.common.keysImportKeys fromSelenium.webdriver.common.alertImportAlertImportUnitTestImport TimeImportBasePageclassTest_login (unittest. TestCase):#Setup defsetUp (self): Self.driver= Webdriver. Chrome (R'C:\Users\xua\Downloads\chromedriver_win32\chromedriver.exe') self.driver.implicitly_wait (30) Self.base_url="http://10.222.30.145:9000/" #TearDown defTearDown (self): Self.driver.close ()defTest_login (self):#Step1:open Base Siteself.driver.get (Self.base_url)#step2:open Login PageLogin_page =basepage.loginpage (self.driver)#step3:enter usernameLogin_page.set_username ("Sbxadmin") #step4:enter PasswordLogin_page.set_password ("IGTtest1") #Checkpoint1:check Popup Dialog TitleSelf.assertequal (Login_page.get_diaglogtitle ()," Sign In") #Step5:cancel DialogLogin_page.click_cancel ()if __name__=="__main__": Unittest.main ()
Ok, then we look back to see if the page mode solves the top four problems:
1. Legibility: Now look at the Test_login method, do a bit of test case, every step is clear
2. Extensibility: Because each page of the element operations are integrated into a page class, so adding and removing changes are convenient
3. Reusability: The basic operation of page has become a method, can be reused in different test case
4. Maintainability: If the page is modified, simply modify the method in the corresponding page class without modifying each test case
Summarize:
Page mode provides us with a good separation mechanism for page and use case implementations, reduces coupling, improves cohesion, and enables us to do our best in Web automation.
Selenium Automation page mode (Python)