当前位置:网站首页>selenium+pytest+allure综合练习
selenium+pytest+allure综合练习
2022-07-28 01:33:00 【Gxy 正在努力】
综合任务 :熟悉测试环境
链接 :测试环境
配置为汉语添加用户 功能 添加10个用户。
概要
1、先使用selenium实现
2、加 上pytest–fixture,paramtrize
3、加上allure报告。
目录
步骤
1.1 新建包 selenium_pytest_allure
1.2 新建文件py
1.3 导包from selenium import webdriver
1.4 调用chromedriver(驱动),建立浏览器。driver
1.5 添加智能等待10
1.6 打开 测试 环境get(“”)
1.7 定位用户名,输入用户名
1.8 定位密码 ,输入密码
1.9 定位提交,点击提交
2.0验证登陆成功
2.1-2.3悬停{管理员}–选择用户管理-点击用户
2.4 点击添加
2.5 定位 用户角色 * ESS 员工姓名 * 输入以查看提示… 用户名 * 状态 * 启用 密码 确认密码
2.11 点击保存
2.12 验证添加成功。
3.1退出系统
4.1关闭浏览器
1、先使用selenium实现
2、加 上pytest–fixture,paramtrize
3、加上allure报告。
1.使用selenium实现
一 .打开驱动
本次练习中使用的驱动写法为selenium4的写法
打开pycharm
新建python文件
import os
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
导入以上的包 是我们要用到的
驱动:使用selenium4拼接的方式 目的是防止报错
#打开驱动
par_path=os.path.abspath("..")
CHROMEDRIVER_PATH=par_path+r"\driver\chromedriver.exe"
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(10)
打开测试网站
#打开测试网址
driver.get("http://114.116.97.187:8080/symfony/web/index.php/auth/login")
登录
进入也F12检查 可以看到登录和密码ID 运用定位技术 进行登录
driver.find_element(By.ID, "txtUsername").send_keys("账号")
driver.find_element(By.ID, "txtPassword").send_keys("密码")
driver.find_element(By.ID, "btnLogin").click()
悬停
进入网页我发现 我要找的添加 需要将鼠标悬停在个人信息上 才会出现列表
那这里 我们就要用到悬停技术了
# 鼠标悬停
time.sleep(3)
admin_link=driver.find_element(By.LINK_TEXT,"个人信息管理系统")
user_manager=driver.find_element(By.ID,"menu_pim_addEmployee")
xuanting=ActionChains(driver)
xuanting.move_to_element(admin_link).click(user_manager).perform()
悬停【move_to_element(admin_link)】在上方并选择添加员工 且点击【click(user_manager)】
添加
也是根据定位修改key值进行写入
driver.find_element(By.ID,"user_name").send_keys("guoxiangyu12")
driver.find_element(By.ID,"user_password").send_keys(".78594 qwefh78./5kghG")
driver.find_element(By.ID,"re_password").send_keys(".78594 qwefh78./5kghG")
time.sleep(1)
删除
我要根据后面数字来进行定位
这个数字是可变的 所有我们把它设置为num
#选择一个删除
#通过用户名找到 无元素link 郭翔宇 翔宇
href=driver.find_element(By.LINK_TEXT,"宇 翔").get_attribute("href")
#获得它的属性 href值/symfony/web/index.php/pim/viewEmployee/empNumber/37
#字符串 切最后两个取回来
num=href[href.rindex("/")+1:]
#与ID拼接一下
driver.find_element(By.ID,"ohrmList_chkSelectRecord_"+num).click()
driver.find_element(By.ID,"btnDelete").click()
time.sleep(2)
driver.find_element(By.ID,"dialogDeleteBtn").click()
验证用户登陆

断言 是否是你登录的用户
assert 'admin' in driver.find_element(By.PARTIAL_LINK_TEXT,"欢迎").text.lower()
以上是里面的概要设计
下面是整体代码
#在一个 方法实现打开关闭浏览器,并且是初始化销毁
import os
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
#打开驱动
from selenium.webdriver.support.select import Select
par_path=os.path.abspath("..")
CHROMEDRIVER_PATH=par_path+r"\driver\chromedriver.exe"
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(10)
#打开测试网址
driver.get("http://114.116.97.187:8080/symfony/web/index.php/auth/login")
driver.find_element(By.ID, "txtUsername").send_keys("admin")
driver.find_element(By.ID, "txtPassword").send_keys("Bitnami.12345")
driver.find_element(By.ID, "btnLogin").click()
# 鼠标悬停
time.sleep(3)
admin_link=driver.find_element(By.LINK_TEXT,"个人信息管理系统")
user_manager=driver.find_element(By.ID,"menu_pim_addEmployee")
xuanting=ActionChains(driver)
xuanting.move_to_element(admin_link).click(user_manager).perform()
time.sleep(2)
# 添加
driver.find_element(By.ID,"firstName").send_keys("郭翔宇1")
driver.find_element(By.ID,"middleName").send_keys("翔")
driver.find_element(By.ID,"lastName").send_keys("郭")
driver.find_element(By.ID,"photofile").send_keys(r"E:\selenium_project\selenium_pytest_allure_demo\2.png")
time.sleep(2)
#创建登录详情
## 勾选
driver.find_element(By.ID,"chkLogin").click()
time.sleep(1)
## 添加信息
driver.find_element(By.ID,"user_name").send_keys("guoxiangyu12")
driver.find_element(By.ID,"user_password").send_keys(".78594 qwefh78./5kghG")
driver.find_element(By.ID,"re_password").send_keys(".78594 qwefh78./5kghG")
time.sleep(1)
## 创建用户详情的状态选择
driver.find_element(By.ID,"status").click()
## 启用1 或则禁用2
driver.find_element(By.XPATH,"//*[@id='status']/option[2]").click()
driver.save_screenshot("1.png")
time.sleep(1)
# 保存
driver.find_element(By.ID,"btnSave").click()
time.sleep(5)
# 断言
assert '宇' in driver.find_element(By.ID,"profile-pic").text
#编辑
driver.find_element(By.ID,"btnSave").click()
time.sleep(1)
#选择性别 personal_optGender_1[男];personal_optGender_2[女]
driver.find_element(By.ID,"personal_optGender_1").click()
time.sleep(1)
#婚姻状况
personal_cmbMarital_ele = driver.find_element(By.ID,"personal_cmbMarital")
Select(personal_cmbMarital_ele).select_by_visible_text("单身")
time.sleep(1)
#选择出生日期
#driver.find_element(By.ID,"personal_DOB").click()
#下拉列表选择国籍
personal_cmbNation_ele = driver.find_element(By.ID,"personal_cmbNation")
Select(personal_cmbNation_ele).select_by_visible_text("中国人")
#保存
driver.find_element(By.ID,"btnSave").click()
time.sleep(1)
#查看员工列表
driver.find_element(By.ID,"menu_pim_viewEmployeeList").click()
time.sleep(3)
#选择一个删除
#通过用户名找到 无元素link 郭翔宇 翔宇
href=driver.find_element(By.LINK_TEXT,"宇 翔").get_attribute("href")
#获得它的属性 href值/symfony/web/index.php/pim/viewEmployee/empNumber/37
#字符串 切最后两个取回来
num=href[href.rindex("/")+1:]
#与ID拼接一下
driver.find_element(By.ID,"ohrmList_chkSelectRecord_"+num).click()
driver.find_element(By.ID,"btnDelete").click()
time.sleep(2)
driver.find_element(By.ID,"dialogDeleteBtn").click()
#验证用户登录
assert 'admin' in driver.find_element(By.PARTIAL_LINK_TEXT,"欢迎").text.lower()
#登出
driver.find_element(By.ID,"welcome").click()
driver.find_element(By.LINK_TEXT,"登出").click()
driver.find_element(By.XPATH,"//*[@id='welcome-menu']/ul/li[4]/a").click()
time.sleep(1)
#关闭浏览器
driver.close()
运行结果为
已经成功啦 以上就是 用selenium方式执行的脚本
2.使用pytest框架 执行
包含pytest–fixture,paramtrize
新建一个文件起名为test开头代表测试
写入如下文件
import os
import time
import allure
import pytest
import yaml
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.service import Service as ChromeService
par_path = os.path.abspath("..")
@pytest.fixture(scope="module")
def driver():
with allure.step("0.打开浏览器"):
# selenium4以上的编写方法
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ['enable-automation','enable-logging'])
options.add_experimental_option("useAutomationExtension", False)
CHROMEDRIVER_PATH = par_path + r"\driver\chromedriver.exe"
service = ChromeService(executable_path=CHROMEDRIVER_PATH)
driver = webdriver.Chrome(service=service)
driver.implicitly_wait(10)
yield driver
time.sleep(1)
with allure.step("8.关闭浏览器"):
# 关闭浏览器
driver.close()
# 打开测试网址
@allure.feature("登录")
@pytest.mark.parametrize("value", yaml.safe_load(
open(r"E:\selenium_project\selenium_pytest_allure_demo\test_data_login.yaml", encoding="utf8")))
def test_logins(driver, value):
txtUsername = value['txtUsername']
txtPassword = value['txtPassword']
firstName = value['firstName']
with allure.step("1.打开登录页"):
driver.get("http://114.116.97.187:8080/symfony/web/index.php/auth/login")
with allure.step("2.输入用户名密码"):
allure.attach.file(par_path+"/selenium_pytest_allure_demo/test_data_login.yaml",attachment_type=allure.attachment_type.YAML)
driver.find_element(By.ID, "txtUsername").send_keys(txtUsername)
driver.find_element(By.ID, "txtPassword").send_keys(txtPassword)
with allure.step("3.点击登录"):
driver.find_element(By.ID, "btnLogin").click()
# 验证用户登陆
with allure.step("4.验证名字正确"):
assert firstName in driver.find_element(By.PARTIAL_LINK_TEXT, "欢迎").text
with allure.step("5.截图验证登录成功"):
driver.save_screenshot("login_" + txtUsername + ".png")
allure.attach.file("login_" + txtUsername + ".png", "用户登录成功的截图", attachment_type=allure.attachment_type.PNG)
# 登出
with allure.step("6.登出"):
driver.find_element(By.ID, "welcome").click()
driver.find_element(By.LINK_TEXT, "登出").click()
# driver.find_element(By.XPATH,"//*[@id='welcome-menu']/ul/li[4]/a").click()
time.sleep(1)
新建一个yaml文件
写入 我们的用户名和密码 (用户名和密码的写法 在上面的selenium 用例里)
yaml 文件格式为字典类型
也就是
- txtUsername: xxxxx
txtPassword: xxxxxxx
firstName: xxxxx
- txtUsername: xxxxx
txtPassword: xxxxxxx
firstName: xxxxx
- txtUsername: xxxxx
txtPassword: xxxxxxx
firstName: xxxxx
执行这个用例
终端进入所在文件的目录下
输入pytest 加文件名(这是指定一个文件执行 )
3.生成报告
终端进入所在文件的目录下
运行pytest -q test_oranghrm_login.py --alluredir=./result(test…py是你的测试文件名)
执行成功后会出现如下的测试数据
这个时候我们要 把这些数据 输入成报告的模式 显示出来并且又详细信息
终端cd进入所在文件的目录下
输入 allure serve ./result (resul就是我们刚才生成数据存储的文件)
尝试使用三次迸发的形式出去
同样在 终端CD进入要执行文件的目录下
输入命令:pytest -n 3 pytest -n 3 文件名 --alluredir=./result/
这样我们的总和练习就结束啦
边栏推荐
- From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
- 【软件测试】—— 自动化测试之unittest框架
- Smart contract security -- selfdestroy attack
- TypeScript(零) —— 简介、环境搭建、第一个实例
- [advanced ROS chapter] Lecture 10 gadf integrated simulation process and examples based on gazebo
- AWS elastic three swordsman
- 程序里随处可见的interface,真的有用吗?真的用对了吗?
- mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
- windbg
- what‘s the meaning of “rc“ in release name
猜你喜欢

LETV responded that employees live an immortal life without internal problems and bosses; Apple refuses to store user icloud data in Russia; Dapr 1.8.0 release | geek headlines

使用BigDecimal类型应该避免哪些问题?(荣耀典藏版)

【 图像去雾】基于暗通道和非均值滤波实现图像去雾附matlab代码

How do you use the jar package sent by others (how to use the jar package sent by others)
![[Yugong series] use of tabby integrated terminal in July 2022](/img/df/bf01fc77ae019200d1bf57be783cb9.png)
[Yugong series] use of tabby integrated terminal in July 2022
![[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control](/img/7f/f0360210e8a9f7e45410d79635bfd9.png)
[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control

作业7.27 IO进程
![[data processing] boxplot drawing](/img/4e/c4f863d06d8b318e6bb2d40e0c5ed3.png)
[data processing] boxplot drawing

MySQL high availability and master-slave synchronization

Important arrangements - the follow-up live broadcast of dx12 engine development course will be held at station B
随机推荐
mysql: error while loading shared libraries: libtinfo.so. 5 solutions
2022.7.8 supplement of empty Luna
"Risking your life to upload" proe/creo product structure design - seam and buckle
Welcome to CSDN markdown editor Assad
Three core issues of concurrent programming (glory Collection Edition)
Say yes, I will love you, and I will love you well
How MySQL uses indexes (glory Collection Edition)
Wechat campus bathroom reservation applet graduation design finished product (2) applet function
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
What is eplato cast by Plato farm on elephant swap?
windbg
MySQL's way to solve deadlock - lock analysis of common SQL statements
The virtual host website cannot access the self-test method
unordered_ The hash function of map and the storage mode of hash bucket
Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)
[Yugong series] use of tabby integrated terminal in July 2022
Read Plato & nbsp; Eplato of farm and the reasons for its high premium
"The faster the code is written, the slower the program runs"
Use of Day6 functions and modules
【英雄哥七月集训】第 27天:图