Mainstream Automation Frameworks
Selenium is the web automation framework for appium app requests for interface testing
Selenium tool class encapsulation
Selenium provides many methods for manipulating web elements. In practice, our variables are only element positioning and operations on elements, and everything else is repetitive. To reduce the amount of code, we need to encapsulate Selenium as a utility class. The utility class is our toolbox, and the methods inside it are our tools
Creating common Tools
Create a module under the project folder new ->Python Package
A Python framework folder with init files is generated
From time import sleep class WebKeys: def __init__(self): self.driver = webdriver.chrome () # def open(self, url): Self.driver. Get (url) # exit def quit(self): self.driver. Return self. Driver. Find_element (name, value) def input(self, name, value, TXT): el = self.locator(name, value) el.clear() el.send_keys(txt)`Copy the code
Pytest writes test cases and performs tests
Import pyTest framework PyTest framework integrates many methods for us to test test cases, efficient, easy to use, can save a lot of work PyTest test cases must be named test files below
import pytest from time import * from data_driver import yaml_driver @pytest.mark.parametrize('data', yaml_driver.load_yaml('.. /data/baidu.yaml')) def test_login(data): wk = WebKeys() wk.open(data['url']) wk.input(data["name"], data["value"], data["txt"]) sleep(3) wk.quit() if __name__ == '__main__': pytest.main()Copy the code
Data driven
When we have a lot of test cases, we need to write many use cases. Using data-driven, we can save a lot of time. The company commonly uses YAML library 1 and uses PIP to import YAML
def load_yaml(path):
file = open(path, 'r', encoding='utf-8')
data = yaml.load(file, Loader=yaml.FullLoader)
return data
Copy the code
Create a new folder for your test case data. Create a YAML file and place the data in the correct format
-url: http://www.baidu.com name: xpath value: //*[@id="kw"] TXT: dogecoin - url: http://www.baidu.com name: xpath value: / / * [@ id = "kw"] TXT: fire currency - the url: http://www.baidu.com the name: xpath value: / / * [@ id = "kw"] TXT: COINS - url: http://www.baidu.com name: xpath value: //*[@id="kw"] TXT: EthereumCopy the code
Note that this – and subsequent urls must be below – right or the system will report file alignment errors
Inside the test case code is a line that essentially calls our encapsulated YAMl class and returns a list of variables: data @pytest.mark.parametrize(‘data’, yaml_driver.load_yaml(‘.. /data/baidu.yaml’)) .. / means to jump to the parent directory and find the YAML file in the path. In the test case, we only need to specify the data variable for one method, and the method will be executed four times, which is the equivalent of writing four use cases
if __name__ == '__main__':
pytest.main()
Copy the code
The pytest.main function has a lot of arguments, so we can call it if we want.