测试用例:
import time from selenium.webdriver.support import expected_conditions as EC import pytest import yaml from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.wait import WebDriverWait result = {} zoom = 1 def get_data(): with open("../data/data.yaml", encoding="utf8") as f: data = yaml.safe_load(f.read()) return data def emp_login(driver): # 用户名↓ driver.find_element(by=By.XPATH, value="//input[@tabindex=1]").send_keys("gxaadmin") # 密码↓ driver.find_element(by=By.XPATH, value="//input[@tabindex=2]").send_keys("123456") # 登录↓ driver.find_element(by=By.XPATH, value="//button[@tabindex=3]").click() """ param data: {"e_name":"jack", "e_password":"123456", "e_sex":"男", "e_jointime":"2021-06-21 12:00:00", "e_level": "1", "e_rfid":"12345678901234567890123456789012", "e_idCard":"554001198505112345", "e_phone":"13709013307", "e_birthday":"2021-06-13 12:00:00", "e_state":"1"} """ # @pytest.mark.parametrize("e_name,e_password,e_sex,e_jointime,e_level,e_rfid,e_idCard,e_phone,e_birthday,e_state" # , # [["jack1", "123456", "男", "2021-06-21 12:00:00", 1, "12345678901234567890123456789012", # "554001198505112345", "13709013307", "2021-06-13 12:00:00", 1]]) @pytest.mark.parametrize("e_name,e_password,e_sex,e_jointime,e_level,e_rfid,e_idCard,e_phone,e_birthday,e_state" , get_data()['emp_data']) def test_add_emp(e_name, e_password, e_sex, e_jointime, e_level, e_rfid, e_idCard, e_phone, e_birthday, e_state): driver = webdriver.Chrome() try: driver.get("http://localhost:8080/#/") driver.maximize_window() print("登录开始!!!") emp_login(driver) print("登录结束!!!") time.sleep(1) driver.execute_script("document.body.style.zoom='" + str(zoom) + "'") # 缩小 # 标题 # print("title = " + driver.title) print("添加员工开始!!!!") # 添加员工↓ driver.find_element(by=By.XPATH, value="//button[@tabindex=17]").click() driver.find_element(by=By.XPATH, value="//input[@tabindex=1]").send_keys(f"{e_name}") driver.find_element(by=By.XPATH, value="//input[@tabindex=2]").send_keys(f"{e_rfid}") driver.find_element(by=By.XPATH, value="//input[@tabindex=3]").send_keys(f"{e_password}") driver.find_element(by=By.XPATH, value="//input[@tabindex=4]").send_keys(f"{e_idCard}") if '男' == e_sex: driver.find_element(by=By.XPATH, value="//label[@tabindex=5]").click() else: driver.find_element(by=By.XPATH, value="//label[@tabindex=-1]").click() driver.find_element(by=By.XPATH, value="//input[@tabindex=7]").send_keys(f"{e_phone}") driver.find_element(by=By.XPATH, value="//div[@tabindex=8]/input").send_keys(f"{e_jointime}") driver.find_element(by=By.XPATH, value="//div[@tabindex=9]/input").send_keys(f"{e_birthday}") # driver.find_element(by=By.XPATH, value="//div[@tabindex=10]/div/input").send_keys("员工") # driver.find_element(by=By.XPATH, value="//div[@tabindex=11]/div/input").send_keys("在职") driver.find_element(by=By.XPATH, value='//*[@id="app"]/div[2]/div[2]/div/div[3]/div[2]/div/form/div[10]/div/div/div[1]/div[1]/input').click() if e_level != 1: e_level = 2 for i in range(1, 10): try: driver.find_element(by=By.XPATH, value='/html/body/div[' + str(i) + ']/div[1]/div[1]/ul/li[' + str( e_level) + ']').click() except Exception as e: # print("打印《操作等级》第" + str(i) + "个异常:" + str(e.args[0])) continue print("打印《操作等级》第" + str(i) + "个div成功") break time.sleep(1) driver.find_element(by=By.XPATH, value='//*[@id="app"]/div[2]/div[2]/div/div[3]/div[2]/div/form/div[9]/div/div[1]/div[1]/input').click() if e_state == 0: e_state = 2 for i in range(1, 10): try: # /html/body/div[2]/div[1]/div[1]/ul/li[2] driver.find_element(by=By.XPATH, value='/html/body/div[' + str(i) + ']/div[1]/div[1]/ul/li[' + str( e_state) + ']').click() except Exception as e: # print("打印《在职状态》第" + str(i) + "个异常:" + str(e.args[0])) continue print("打印《在职状态》第" + str(i) + "个div成功") break driver.find_element(by=By.XPATH, value="//div[@tabindex=0]/input").send_keys( r"F:\870_1_Files\PythonFiles\SeleniumPro\selenium_pytest\test_selenium\test.jpg") driver.find_element(by=By.XPATH, value="//button[@tabindex=13]").click() # 等待弹窗显示 wait = WebDriverWait(driver, 10) wait.until(EC.alert_is_present()) alert = driver.switch_to.alert result['add_emp_result'] = alert.text alert.accept() print("添加员工结束!!!") except Exception as e: raise e finally: # time.sleep(1) driver.quit() def teardown_function(): print(result) assert result['add_emp_result'] == "添加成功" if __name__ == '__main__': # test_login_add() pytest.main() # login_and_add_user() # main()
data.yaml
emp_data: - [ jack,123456,男,2021-06-21 12:00:00,1,12345678901234567890123456789012,554001198505112345,13709013307,2021-06-13 12:00:00,1 ] #- [ jackson,123422,男,2023-10-31 12:00:00,1,12345678901234567890123456789011,554001198505112325,13709013303,2023-10-31 12:00:00,1 ]
pytest.ini
[pytest] testpaths=./ markers = p0:最高优先级 test:测试环境 pro:生成环境 addopts = -vs --alluredir ./report
运行测试用例生成报告:
在控制台输入 allure serve ./report (后面是路径)
自动打开↓
报告生成成功。
第二种生成报告方式:
1、输入命令 allure generate .\report\
生成文件夹:
2、输入命令allure open .\allure-report\
可以打开和上面一样的界面。
自定义生成报告文件夹名
输入命令 allure generate -o allure_report (-o 后面是自定义文件夹名)
标签:XPATH,allure,driver,value,生成,pytest,div,element,find From: https://www.cnblogs.com/0099-ymsml/p/17800964.html