To do UI automation testing, using keyword drivers is a must. It can be run in a pure code automation program, or it can be used in the test platform.

With the pure code approach, automation engineers write a generic program, and other manual testers simply fill in the keywords of page actions to perform in a table and perform automated tests. This table can be an Excel table or a YAML file.

When using a test platform, the test development project writes an interface in which manual testers can also select page keywords to operate, and then perform automated tests.

Keyword driven implementation cost is low, easy to operate, it is a good way to do UI automation testing. This paper introduces a simple keyword driven implementation, do not need very advanced programming knowledge can also master.

Keyword driven implementation is mainly divided into three parts:

  • The definition of keyword methods, which are actually common operations on the page, such as clicking, typing, and other common operations, is written by the automation test engineer.
  • The keyword table is configured. Who write use cases, automation configuration of this form, it mainly defines the page cases need to perform operation and test data, through the best script automatically generated fixed format, and can choose the operation name, easy to use, in the test platform, can use a drop-down box to search and choose the way to choose, will be more convenient.
  • Invoke keywords to perform page operations. This, also written by the automation test engineer, reads the page actions in the YAML file and executes them.

Define keyword methods

If we want to automate testing of web pages, we can define a Page class that encapsulates Page actions such as clicks and inputs.

# 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) :
    	""" Page access """
        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) :
    	"" page click """
        el = self.wait_clickable(locator)
        el.location_once_scrolled_into_view
        el.click()

    def fill(self, locator, words) :
        """ Page input """
        el = self.wait_visible(locator)
        el.send_keys(words)
Copy the code

Keyword table

The keyword table is the test steps and data for an automated use case. Here we use the YAML file, where action represents the Page action to be used, which corresponds to the method of the same name in the previous Page class, and params represents the test parameters that the method needs to pass in.

For each different use case, write a YAML file that you pass into the automation program as a parameter when you need to run the automated tests.

# 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']
Copy the code

Use keywords to manipulate the browser

Finally, the general test case writing. Yaml file to read the test steps and data, using getattr to pass in the Page operation names goto, fill, and click to get the Page class method of the same name. These methods are invoked to automate the corresponding operations.

Most of this code does not require additional modifications; when running different test cases, you simply change the name of the YAML file and invoke 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) :
    """ Get yamL file """
	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)
Copy the code

run

For now, we just need to define different YAML files and use a testing framework like PyTest to run the automated use cases. However, as a simple version of the keyword driver implementation, this program has a lot of room for optimization.

For example, after writing multiple YAML files, you now need to create multiple Python test files. More commonly, I can configure a YAML file from the command line and run the test steps for that file.

Configuring multiple YAML files enables you to run test steps for multiple YAML files. You can even configure the folder name to run the test steps for all yamL files under the folder.

Yaml file generation can also be generated from the command line.

We will implement this later, but for now, we at least know how to implement keyword driven.