Package Design! Abstract BasePage to improve the quality and efficiency of WEB automated test cases

 Table of contents

Foreword:

1. What is abstract BasePage

2. Properties and methods in BasePage

3. Code implementation in BasePage

Fourth, the abstract Page object

5. Test cases

6. Summary


Foreword:

For test engineers, WEB automated testing is a very important part. However, the development cost of WEB automated testing is relatively high, and maintenance is also very difficult. Therefore, how to standardize the development of WEB automated testing has become one of the issues that every test engineer needs to think about. This article will introduce how to use Python and Selenium Webdriver to encapsulate the WEB automated testing framework, including how to abstract BasePage.

1. What is abstract BasePage

Generally speaking, we need to create many page objects (Page Object) when developing a WEB automated testing framework, such as login page, registration page, home page and so on. Each page object needs to have many methods, such as entering data in the input box, clicking a button, and so on. In order to avoid duplication of code, we can abstract a BasePage.

BasePage is a base class that contains common methods and properties. All page objects inherit from BasePage and implement their own methods and properties through method rewriting, which makes the code more concise and standardized.

2. Properties and methods in BasePage

Attributes include: driver (browser driver instance), url (URL of the current page), title (title of the current page).

Methods include: open (open page), find_element, find_elements, click, and input.

Among them, find_element and find_elements are methods for locating page elements, click is a method for clicking elements, and input is a method for inputting text to elements.

3. Code implementation in BasePage

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class BasePage(object):
     def __init__(self, driver):
         self.driver = driver
         self.url = "https://www.example.com"
         self.title = "Example"
         
     def open(self):
         self.driver.get(self.url)
         assert self.title in self.driver.title
         
     def find_element(self, *loc):
         return self.driver.find_element(*loc)
         
     def find_elements(self, *loc):
         return self.driver.find_elements(*loc)
         
     def click(self, *loc):
         element = WebDriverWait(self.driver, 10).until(EC.element_to_be_clickable(loc))
         element.click()
         
     def input(self, *loc, text):
         element = WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(loc))
         element.clear()
         element.send_keys(text)

Fourth, the abstract Page object

In actual development, we need to create a specific Page object, inherit BasePage, and rewrite the properties and methods of the parent class.

Take the login page as an example:

class LoginPage(BasePage):
      def __init__(self, driver):
            super(LoginPage, self).__init__(driver)
            self.url = "https://www.example.com/login"
            self.title = "Login"
            
      def input_username(self, username):
            input_loc = (By.ID, "username")
            self.input(input_loc, username)
            
      def input_password(self, password):
            input_loc = (By.ID, "password")
            self.input(input_loc, password)
            
      def click_submit(self):
            button_loc = (By.ID, "submit")
            self.click(button_loc)

5. Test cases

In the actual testing process, we use the Page object to execute the test cases.

For example:

from selenium import webdriver
from pageobjects import LoginPage

# 创建浏览器对象
driver = webdriver.Chrome()

# 打开登录页面
login_page = LoginPage(driver)
login_page.open()

# 输入用户名
login_page.input_username("testuser")

# 输入密码
login_page.input_password("testpassword")

# 点击登录按钮
login_page.click_submit()

# 断言是否登录成功(例如判断是否跳转到首页)
assert "Home" in driver.title

# 关闭浏览器
driver.quit()

6. Summary

In the WEB automated testing framework, the abstract BasePage is a very important part, which can avoid the generation of repeated code and make the code more concise and standardized. In actual development, we need to create a specific Page object, inherit BasePage, and rewrite the properties and methods of the parent class. Using Page objects to execute test cases can effectively improve the readability and maintainability of the code.

At the same time, when writing test cases, you need to pay attention to the following points:

1. Try to avoid hard-coding (Hard-coding), use constants or configuration files instead.

2. Handle error conditions, such as element positioning failure, etc., and do not throw exceptions directly.

3. Encapsulate public methods, such as login method and exit method, to facilitate the writing of test cases.

4. Use asserts to determine whether the test results meet expectations, such as determining whether the page title is correct, whether the element exists, and so on.

In general, abstracting BasePage allows us to write WEB automation test cases more easily and efficiently, improving test quality and efficiency.

 As someone who has been here, I also hope that you will avoid some detours. Here I will share with you some necessities on the way forward for automated testing, hoping to help you. (WEB automated testing, app automated testing, interface automated testing, continuous integration, automated test development, big factory interview questions, resume templates, etc.), I believe it can make you better progress!

Just leave [Automated Test]
[Automated Test Communication]: 574737577 (remark ccc) http://qm.qq.com/cgi-bin/qm/qr?_wv=1027&k=3Ro31suVzmZQQTuC3pd1DnAqOjZcVkMI&authKey=3LzbiKyASmsZRXGK7rlihB36U4cUmJ1Fkwmo upezXCD23%2FnD4mH5mBxDRs4GTCQF&noverify=0&group_code=574737577

Guess you like

Origin blog.csdn.net/Free355/article/details/130967348