Python UI automation - keyword + excel form data driven

step:

  1. Secondary encapsulation of selenium to create a library of keywords

  2. Prepare a table file to write all test case steps

  3. Read the content of the table and use the mapping relationship to call and execute the use case

     4. Execute the use case

1. Secondary encapsulation of selenium to create a library of keywords

from time import sleep
from selenium import webdriver


class Key:

    def __init__(self):
        self.driver = webdriver.Chrome()

    # 浏览器操作------------------------------------------------------------------
    def open(self, txt):
        # 打开网址
        self.driver.get(txt)
        # 最大化浏览器窗口
        self.driver.maximize_window()
        # 隐式等待10秒
        self.driver.implicitly_wait(10)

    def quit(self):
        # 退出浏览器
        self.driver.quit()

    def sleep(self, txt):
        # 强制等待
        sleep(txt)

    # 元素操作函数-----------------------------------------------------------------
    def input(self, txt, value, name="xpath"):
        # 输入
        el = self.driver.find_element(name, value)
        el.send_keys(txt)

    def click(self, value, name="xpath"):
        # 点击
        el = self.driver.find_element(name, value)
        el.click()

2. Create a table and write the test steps

Put the table in any path of the project, remember the path, you will need to read the file later, I put it here

Explain: (positioning method) is empty, because the keyword method has already brought default parameters when encapsulating

3. Write an excel table reading method, read the content of the table, and use the mapping relationship to call and execute the use case.

You will understand what it means by reading the comments.

import os
import openpyxl
from UI.Base.selenium_key import Key

# 获取该路径“../TestExampleExcel”模板下所有xlsx文件
filenames = os.listdir(r"../TestExampleExcel")

# 实例化驱动
wd = Key()

# 遍历所有xlsx文件
for i in filenames:
    excel = openpyxl.load_workbook(f'../TestExampleExcel/{i}')

    # 获取全部sheet页,遍历sheet页执行不同sheet页中的用例
    for name in excel.sheetnames:
        sheet = excel[name]
        print(f"正在执行{i}文件中的{name}用例")

        # 打印每一行表格数据
        for values in sheet.values:

            # 如果excel表格的第三列不是int类型,则不打印。
            if isinstance(values[2], int):
                data = {}
                data['name'] = values[4]
                data['value'] = values[5]
                data['txt'] = values[6]

                # 将字典中的None值给去除掉
                for k in list(data.keys()):
                    if data[k] is None:
                        del data[k]
                print(f"正在执行:{values[1]}")
                getattr(wd, values[3])(**data)

4. Execute the use case

Execute the Excel file reading method

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/myh919/article/details/132744374