Secondary encapsulation of selenium

This article will introduce how to encapsulate several commonly used webdriver methods into a class written by yourself. This encapsulation process is called secondary encapsulation of Selenium methods. We encapsulate the methods of opening the site, forwarding and rewinding the browser, closing and exiting the browser into a newly written class.

We create two new packages and two .py files in PyCharm according to the following hierarchy:


      In the picture above, baidu_search.py ​​is the python file where we write the test script, and the specific test code is written in this file. The basepage.py file under the package test1 is newly created by the secondary encapsulation selenium method we introduced this time. It is mentioned here that the default rule in python is that the package name and file name are all lowercase, the first letter of the class name is uppercase, the function name is lowercase, and multiple letters are separated by underscores. We try to abide by this unwritten agreement.


Let's take a look at the content of basepage.py:

[python]  view plain copy  
  1. # coding=utf-8  
  2.   
  3.   
  4. class BasePage(object):  
  5.     """ 
  6.     Mainly to encapsulate several commonly used Selenium methods into the BasePage class, we demonstrate the following methods here 
  7.     back() 
  8.     forward() 
  9.     get() 
  10.     quit() 
  11.     """  
  12.     def __init__(self, driver):  
  13.         """ 
  14.         Write a constructor with one parameter driver 
  15.         :param driver: 
  16.         """  
  17.         self.driver = driver  
  18.   
  19.     def back(self):  
  20.         """ 
  21.         browser back button 
  22.         :param none: 
  23.         """  
  24.         self.driver.back()  
  25.   
  26.     def forward(self):  
  27.         """ 
  28.         browser forward button 
  29.         :param none: 
  30.         """  
  31.         self.driver.forward()  
  32.   
  33.     def open_url(self, url):  
  34.         """ 
  35.         open url site 
  36.         :param url: 
  37.         """  
  38.         self.driver.get(url)  
  39.   
  40.     def quit_browser(self):  
  41.         """ 
  42.         Close and stop the browser service 
  43.         :param none: 
  44.         """  
  45.         self.driver.quit()  
The above '''''' is a document comment, usually at the beginning of the class and the beginning of the function, enclosed by two '''''', briefly describing the function of the class or function.

Next, let's see how to call our own encapsulated method in our script file.

The content of baidu_search.py ​​is as follows:

[python]  view plain copy  
  1. # coding=utf-8  
  2. import time  
  3. from selenium import webdriver  
  4. from test1.basepage import BasePage  
  5.   
  6.   
  7. class BaiduSearch(object):  
  8.   
  9.     driver = webdriver.Chrome()  
  10.     driver.maximize_window()  
  11.     driver.implicitly_wait(10)  
  12.   
  13.     basepage = BasePage(driver)  
  14.   
  15.     def open_baidu(self):  
  16.         self.basepage.open_url("https://www.baidu.com")  
  17.         time.sleep(1)  
  18.   
  19.     def test_search(self):  
  20.         self.driver.find_element_by_id('kw').send_keys("Selenium")  
  21.         time.sleep(1)  
  22.         self.basepage.back()  
  23.         self.basepage.forward()  
  24.         self.basepage.quit_browser()  
  25.   
  26. baidu = BaiduSearch()  
  27. baidu.open_baidu ()  
  28. baidu.test_search()  

The few lines of code in self.basepage       above are to call our own encapsulated method to perform related webdriver operations. This is just a simple encapsulation introduction. Later, we will introduce string cutting, and we will introduce the secondary encapsulation Selenium method again. For example, we will encapsulate the eight find_element methods into one method.

For more understanding and communication, please add QQ group: 49044146

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325966272&siteId=291194637