当前位置:网站首页>[appium] write scripts by designing Keyword Driven files

[appium] write scripts by designing Keyword Driven files

2022-04-23 17:42:00 BetterFate!

Preface : Drive files through keywords , Separate the script from the test case , It allows people with weak code to fill in the parameters required for use cases in a format , You can run automated scripts , It also makes the code more concise .

Catalog

One 、 Design driver file

Two 、 Script according to the file


One 、 Design driver file

This passage csv file To write test cases , In addition to the basic elements of use cases IDname describe outside , In order to associate with the script, add How elements are positioned The element object itself Operation method Test data , Of course, the actual situation is still based on the project requirements .

csv The preliminary design of the document is as follows :
Be careful : The test steps have to be written in the order of the actual test , Don't mess up

Two 、 Script according to the file

First, clarify the positioning method , I used to use driver.find_element(By.ID, ' Elements id') such By. The way Methods , But there is another way :driver.find_element('id', ' Elements id') This method is also possible .

Other corresponding properties are as follows :

  • ID = "id"
  • XPATH = "xpath"
  • LINK_TEXT = "link text"
  • PARTIAL_LINK_TEXT = "partial link text"
  • NAME = "name"
  • TAG_NAME = "tag name"
  • CLASS_NAME = "class name"
  • CSS_SELECTOR = "css selector"

Be careful : There should be no fewer spaces , stay csv The positioning method filled in the must also be based on this

The script for reading the file is as follows :

    def test_addnote(self):
        #  Read the keyword driven test case file 
        file = open('keyframe.csv', 'r')
        table = csv.reader(file)
        header = next(table)  #  Skip the first line 
        for row in table:
            # print(row[3])
            if row[6] == 'click':
                self.driver.find_element(str(row[4]), str(row[5])).click()
            elif row[6] == 'send_keys':
                self.driver.find_element(str(row[4]), str(row[5])).send_keys(str(row[7]))

The example is that there is a cloud on the simulator used app Add notes , The complete code is as follows :

#  Import Appium Class library 
from appium.webdriver.webdriver import WebDriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import csv


class yd_addnote():
    #  Use the initialization method to set the test parameters 
    def __init__(self):
        self.caps = {
            'automationName': 'UiAutomator2',
            'platformName': 'Android',
            'platformVersion': '6.0',
            'deviceName': '192.168.23.101:5555',
            'appPackage': 'com.youdao.note',
            'appActivity': '.activity2.MainActivity'}
        self.driver = WebDriver('http://127.0.0.1:4723/wd/hub', self.caps)
        self.driver.implicitly_wait(10)
        #  Enter Youdao cloud 
        el = WebDriverWait(self.driver, 10).until(
            lambda x: x.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button'))
        if el:
            #  Click the Agree button 
            self.driver.find_element(By.ID, 'com.android.packageinstaller:id/permission_allow_button').click()

    def test_addnote(self):
        #  Read the keyword driven test case file 
        file = open('keyframe.csv', 'r')
        table = csv.reader(file)
        header = next(table)  #  Skip the first line 
        for row in table:
            # print(row[3])
            if row[6] == 'click':
                self.driver.find_element(str(row[4]), str(row[5])).click()
            elif row[6] == 'send_keys':
                self.driver.find_element(str(row[4]), str(row[5])).send_keys(str(row[7]))


if __name__ == '__main__':
    addnote = yd_addnote()
    addnote.test_addnote()

版权声明
本文为[BetterFate!]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231739290846.html