In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Object-oriented features: encapsulation, inheritance, polymorphism. It is also applicable in automation, there is a name often mentioned in Selenium automated testing (the idea is the same as the object-oriented features), through the PO pattern can greatly improve the efficiency of test case maintenance.
# # disadvantages of traditional test scripts
PageObject design patterns such as test script separation, high maintenance cost, poor scalability, low reusability, etc.
Core elements of PO:
It is abstractly encapsulated into a BasePage class in the PO schema, and the base class should have a property that implements only the webdriver instance. Each page inherits BasePage, manages the elements in this page through driver, and encapsulates the operations in page into individual methods. TestCase inherits the unittest.Testcase class and relies on the page class to implement the corresponding test steps. Case base case
The previous basic scenario selects the baidu search page (baidu page is simple and does not need to build a test environment) baidu.py
From selenium import webdriverfrom time import sleepdriver = webdriver.Firefox () driver.get ("http://www.baidu.com")driver.find_element_by_xpath("//input[@id='kw']").send_keys("Bela")driver.find_element_by_xpath("//input[@id='su']").click()sleep(5)driver.quit()
Put the above script in the baidu.py file.
Analysis.
Through the analysis of the baidu.py script, we can extract:
Different running script environments, different browsers: driver webdriver.Firefox () can be stripped, request address change (production environment and test environment): when url== http://www.baidu.com can peel off operation elements, you often need to wait for the element to be loaded before operating: whether you can encapsulate the findelement* method provided by webdriver before operating the element, determine whether the element is operable.
=
In the actual test scenario, there may be multiple test scenarios, if each test scenario needs to maintain url, browser driver, element positioning, and so on, the efficiency will be very low.
Therefore, based on the above analysis, whether it is possible to design a base class for all the test pages (selenium itself is testing the Bamp S system) to maintain some common methods. The name BasePage.py is defined here, which is used to store the public methods of the page and the secondary encapsulation of the original methods of webdriver.
The content of BasePage.py is as follows: from selenium.webdriver.support.wait import WebDriverWaitfrom selenium import webdriverfrom selenium.webdriver.support import expected_conditions as ECclass BasePage (object): "BasePage encapsulates methods common to all pages, such as driver, Find_Element, etc. When instantiating a BasePage class, the first thing to execute is the _ _ init__ method. The input parameter of this method is actually the input parameter of the BasePage class. # _ _ init__ method cannot have a return value Only None def _ init__ (self,selenium_driver,base_url) can be returned: self.driver = selenium_driver self.base_url = base_url # self.pagetitle = pagetitle def on_page (self,pagetitle): return pagetitle in self.driver.title def _ open (self) Url): self.driver.get (url) self.driver.maximize_window () def open (self): self._open (self.base_url,self.pagetitle) def find_element (self,*loc): # * any number of loc position parameters (with a single asterisk) # return self.driver.find_element (* loc) try: WebDriverWait (self.driver 10) .blank (EC.visibility_of_element_located (loc)) return self.driver.find_element (* loc) except: print ("% s page could not find% s element"% (self,loc)) def script (self,src): self.driver.excute_script (src) def send_keys (self,loc, vaule, clear_first=True Click_first=True): try: loc = getattr (self "_% s"% loc) # getattr is equivalent to implementing self.loc if click_first: self.find_element (* loc) .click () if clear_first: self.find_element (* loc) .clear () self.find_element (* loc) .send_keys (vaule) except AttributeError: Print (s element could not be found in s page self Loc)) Optimization of test scripts
After the BasePage.py extraction, the BasePage class is designed, and some webdriver methods are re-encapsulated.
Baidu.py is optimized based on BasePage.py (fully embodies the design idea of PO, encapsulation, inheritance)
# basic test scenario # from selenium import webdriver# from time import sleep## driver = webdriver.Firefox () # driver.get ("http://www.baidu.com")## driver.find_element_by_xpath (" / / input [@ id='kw'] "). Send_keys (" Bela ") # input box # driver.find_element_by_xpath (" / / input [@ id='su'] "). Click () # Baidu button # # sleep (3) # driver.quit () # optimized test scenario from selenium.webdriver.common.by import Byfrom PODemo.BasePage import BasePage # assumes baidu.py, BasePage.py are located in the PODemo.BasePage directory from selenium import webdriverclass SearchPage (BasePage): # location element search_loc = (By.ID) Kw) btn_loc = (By.ID, "su") def open (self): self._open (self.base_url) def search_content (self,content): BaiduContent = self.find_element (* self.search_loc) BaiduContent.send_keys (content) def btn_click (self): BaiduBtn = self.find_element (* self.btn_loc) BaiduBtn.click () PageObject Summary
The BasePage base class in the PO design pattern corresponds to the BasePage.py file in the case. Page1 or pageN in PO pattern corresponds to Search.pyPO in case TestCase corresponds to TestCase.py in case. If you want to learn systematically, advertise it.
To promote my blog column, I have selected a topic "learn Selenium Automated testing Framework from Zero". Let's start with the code and implement Web automated testing framework step by step.
This project will build an available automated testing framework (based on python+selenium) from scratch.
Premise: you should master the basics of python and selenium. Or you won't understand.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.