python_selenium_pytest+yaml+parametrize 数据驱动

python_selenium_pytest+yaml+parametrize 数据驱动_第1张图片

python_selenium_pytest+yaml+parametrize 数据驱动_第2张图片 

 一、准备YAML文件编写数据(同级对齐)

-
  user: 142452536423456782345
  password: WDEqq1233

-
  user: 4363836637
  password: 1

-
  user: HHH436383
  password: 13423423

-
  user: 436383SDWFWEFWsfwfe
  password: 13423423

 二、编写读取YAML的代码文件

import yaml


def read_yaml():
    with open('y01.yml', encoding='utf-8', mode='r') as f:
        value = yaml.load(stream=f, Loader=yaml.FullLoader)
        print(value)
        return value

if __name__ == '__main__':
    read_yaml()

 三、调用读取文件,编写用例

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
import pytest
from yaml_util import read_yaml

class TestClass():

    @pytest.mark.parametrize("key", read_yaml())
    def test_demo002(self, key):
        driver = webdriver.Chrome()
        driver.maximize_window()
        driver.get('http://www.people.com.cn/')
        sleep(2)
        driver.find_element(By.XPATH, '//*[@id="login_button"]/a').click()
        handles = driver.window_handles
        driver.switch_to.window(handles[-1])
        driver.find_element(By.XPATH, '//*[@id="username"]').send_keys(key['user'])
        driver.find_element(By.XPATH, '//*[@id="password"]').send_keys(key['password'])
        sleep(2)
        driver.find_element(By.XPATH, '//*[@id="submit"]').click()

        driver.quit()


if __name__ == '__main__':
    pytest.main(['test_2.py'])

pip install pyyaml     记得下载pyyaml插件!!!! 

你可能感兴趣的:(python,selenium,pytest,测试工具)