Web automated testing: how does selenium implement keyword drive

To do ui automated testing, using keyword-driven testing is a must-have testing method. It can be run in a pure code automation program or used in a test platform.

When using the pure code method, the automation engineer first writes a general program, and other manual testers only need to fill in the executed page operation keywords into a form to execute the automated test. This table can be an Excel table or a yaml file.

When using the test platform, the test development project will write an interface. In the mask, manual testers can also select the page keywords that need to be operated. After the selection, the automated test can be executed.

Keyword-driven implementation is low cost and easy to operate. It is really a good way to do ui automation testing. This article introduces a simple keyword-driven implementation that can be mastered without advanced programming knowledge.

Keyword-driven implementation is mainly divided into three parts:

  • The definition of the keyword method is actually the general operation of the page, such as common operations such as click and input, which are written by automated test engineers.
  • Keyword table configuration. Who writes the automation use case, who configures this table, it mainly defines the page operations and test data that the use case needs to perform, it is best to automatically generate a fixed-format file through scripts, and describe the names of the operations that can be selected for easy use, in the test platform , you can search and select through the drop-down box, which will be more convenient.
  • Call keywords to perform page operations. This is also written by automated test engineers, reads the page operations in the yaml file, and executes them.

define keyword method

If we want to automate testing of web pages, we can define the Page class to encapsulate page operations such as click and input.

# keywords.py
from selenium.webdriver.remote.webdriver import WebDriver
from selenium.webdriver.remote.webelement import WebElement
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
 
DEFAULT_TIMEOUT = 8
 
class Page:
    def __init__(self, driver: WebDriver):
        self.driver = driver
 
    def goto(self, url):
    	"""页面访问"""
        self.driver.get(url)
 
    def wait_clickable(self, locator, timeout=None) -> WebElement:
        timeout = DEFAULT_TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)
        return wait.until(expected_conditions.element_to_be_clickable(locator))
 
    def wait_visible(self, locator, timeout=None) -> WebElement:
        timeout = DEFAULT_TIMEOUT if timeout is None else timeout
        wait = WebDriverWait(self.driver, timeout=timeout)
        return wait.until(expected_conditions.visibility_of_element_located(locator))
 
    def click(self, locator):
    	"""页面点击"""
        el = self.wait_clickable(locator)
        el.location_once_scrolled_into_view
        el.click()
 
    def fill(self, locator, words):
        """页面输入"""
        el = self.wait_visible(locator)
        el.send_keys(words)

 keyword table

The keyword table is the test steps and data of an automated use case. Here we use a yaml file. Action indicates the page operation to be used. It corresponds to the method of the same name in the previous Page class. Params indicates the test parameters that the method needs to pass in.

For each different use case, write a yaml file. When you need to run an automated test, just pass the yaml file as a parameter into the automation program.

# login.yaml
-
  action: goto
  params:
    url: "https://petstore.octoperf.com/actions/Account.action?signonForm="
-
  action: fill
  params:
    locator: ['name', 'username']
    words: 'yuze'
-
  action: fill
  params:
    locator: ['name', 'password']
    words: '1234'
-
  action: click
  params:
    locator: ['name', 'signon']

Operate the browser using keywords

Finally, general test case writing. Define a test_keyword function, first read the test steps and data in the login.yaml file, use the getattr method, pass in the name of the page operation to be called goto, fill, click, and get the method of the same name in the Page class, and call these methods Can realize the corresponding automatic operation.

Most of these codes do not require additional modification. When different test cases need to be run, only the name of the yaml file needs to be modified to call different yaml test steps.

# test_login.py
 
import yaml
import pytest
from selenium import webdriver
from keyworks import Page
 
 
@pytest.fixture
def driver():
    d = webdriver.Chrome()
    d.implicitly_wait(8)
    d.maximize_window()
    yield d
    d.quit()
 
def test_keyword(driver):
    """获取 yaml 文件"""
	with open('signin.yaml', encoding='utf-8') as f:
    steps = yaml.safe_load(f)
    page = Page(driver)
 
    for step in steps:
        action_name = step.get('action')
        params = step.get('params')
        action = getattr(page, action_name)
        action(**params)

run

Now, we only need to define different yaml files, and then use a test framework such as pytest to run automation cases. However, as a simple keyword-driven implementation, this program still has a lot of room for optimization.

For example, after writing multiple yaml files now, you also need to create multiple python test files. The more common way is to configure a yaml file through the command line, and I can run the test steps of the yaml file.

By configuring multiple yaml files, you can run the test steps of multiple yaml files. You can even configure the folder name, and run the test steps of all yaml files in the folder.

The generation of yaml files can also be generated through the command line.

We will implement this later. For now, at least we already know the keyword-driven implementation.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive 

Guess you like

Origin blog.csdn.net/okcross0/article/details/130711949