当前位置:网站首页>Selenium+pytest+allure comprehensive exercise
Selenium+pytest+allure comprehensive exercise
2022-07-28 02:49:00 【Gxy is working hard】
Integrated Mission : Familiar with test environment
link : Test environment
Configure as ChineseAdd users function add to 10 Users .
Summary
1、 First use selenium Realization
2、 Add On pytest–fixture,paramtrize
3、 add allure The report .
step
1.1 New package selenium_pytest_allure
1.2 New file py
1.3 Guide pack from selenium import webdriver
1.4 call chromedriver( drive ), Build browser .driver
1.5 Add smart wait 10
1.6 open test Environmental Science get(“”)
1.7 Locate the user name , enter one user name
1.8 Location code , Input password
1.9 Locate submit , Click on the submit
2.0 Verify successful login
2.1-2.3 hover { Administrators }– Select user management - Click user
2.4 Click Add
2.5 location User roles * ESS Employee name * Enter to see the prompt … user name * state * Enable password Confirm the password
2.11 Click save
2.12 Verification added successfully .
3.1 Exit the system
4.1 Close the browser
1、 First use selenium Realization
2、 Add On pytest–fixture,paramtrize
3、 add allure The report .
1. Use selenium Realization
One . Turn on the drive
The driving method used in this exercise is selenium4 Writing
open pycharm
newly build python file
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
Import the above package It's what we're going to use
drive : Use selenium4 Splicing method The purpose is to prevent error reporting
# Turn on the drive
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)
Open the test site
# Open the test website
driver.get("http://114.116.97.187:8080/symfony/web/index.php/auth/login")
Sign in
Enter also F12 Check You can see login and password ID Use positioning technology Log in 
driver.find_element(By.ID, "txtUsername").send_keys(" account number ")
driver.find_element(By.ID, "txtPassword").send_keys(" password ")
driver.find_element(By.ID, "btnLogin").click()
hover
I found that I am looking for add You need to hover over your personal information The list will appear 
So here We are going to use hover Technology
# Mouse hovering
time.sleep(3)
admin_link=driver.find_element(By.LINK_TEXT," Personal information management system ")
user_manager=driver.find_element(By.ID,"menu_pim_addEmployee")
xuanting=ActionChains(driver)
xuanting.move_to_element(admin_link).click(user_manager).perform()
hover 【move_to_element(admin_link)】 Above and select add employee And click 【click(user_manager)】
add to
It is also modified according to the positioning key Value to write
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)
Delete
I want to locate according to the following numbers
This number is variable So we set it to num
# Select a delete
# Find... By user name Element free link Guoxiangyu Xiangyu
href=driver.find_element(By.LINK_TEXT," Woo Xiang ").get_attribute("href")
# Get its properties href value /symfony/web/index.php/pim/viewEmployee/empNumber/37
# character string Cut the last two and take them back
num=href[href.rindex("/")+1:]
# And ID Splicing
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()
Verify user login

Assertion Is it the user you logged in
assert 'admin' in driver.find_element(By.PARTIAL_LINK_TEXT," welcome ").text.lower()
The above is the outline design
Here's the whole code
# In a Method to open and close the browser , And it is initialization destruction
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
# Turn on the drive
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)
# Open the test website
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()
# Mouse hovering
time.sleep(3)
admin_link=driver.find_element(By.LINK_TEXT," Personal information management system ")
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)
# add to
driver.find_element(By.ID,"firstName").send_keys(" Guoxiangyu 1")
driver.find_element(By.ID,"middleName").send_keys(" Xiang ")
driver.find_element(By.ID,"lastName").send_keys(" outer wall or surrounding area of a city ")
driver.find_element(By.ID,"photofile").send_keys(r"E:\selenium_project\selenium_pytest_allure_demo\2.png")
time.sleep(2)
# Create login details
## Check
driver.find_element(By.ID,"chkLogin").click()
time.sleep(1)
## Add information
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)
## Create a status selection for user details
driver.find_element(By.ID,"status").click()
## Enable 1 Or disable 2
driver.find_element(By.XPATH,"//*[@id='status']/option[2]").click()
driver.save_screenshot("1.png")
time.sleep(1)
# preservation
driver.find_element(By.ID,"btnSave").click()
time.sleep(5)
# Assertion
assert ' Woo ' in driver.find_element(By.ID,"profile-pic").text
# edit
driver.find_element(By.ID,"btnSave").click()
time.sleep(1)
# Sex selection personal_optGender_1[ male ];personal_optGender_2[ Woman ]
driver.find_element(By.ID,"personal_optGender_1").click()
time.sleep(1)
# Marital status
personal_cmbMarital_ele = driver.find_element(By.ID,"personal_cmbMarital")
Select(personal_cmbMarital_ele).select_by_visible_text(" single ")
time.sleep(1)
# Choose a date of birth
#driver.find_element(By.ID,"personal_DOB").click()
# Select nationality from the drop-down list
personal_cmbNation_ele = driver.find_element(By.ID,"personal_cmbNation")
Select(personal_cmbNation_ele).select_by_visible_text(" Chinese ")
# preservation
driver.find_element(By.ID,"btnSave").click()
time.sleep(1)
# View employee list
driver.find_element(By.ID,"menu_pim_viewEmployeeList").click()
time.sleep(3)
# Select a delete
# Find... By user name Element free link Guoxiangyu Xiangyu
href=driver.find_element(By.LINK_TEXT," Woo Xiang ").get_attribute("href")
# Get its properties href value /symfony/web/index.php/pim/viewEmployee/empNumber/37
# character string Cut the last two and take them back
num=href[href.rindex("/")+1:]
# And ID Splicing
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()
# Verify user login
assert 'admin' in driver.find_element(By.PARTIAL_LINK_TEXT," welcome ").text.lower()
# Log out
driver.find_element(By.ID,"welcome").click()
driver.find_element(By.LINK_TEXT," Log out ").click()
driver.find_element(By.XPATH,"//*[@id='welcome-menu']/ul/li[4]/a").click()
time.sleep(1)
# Close the browser
driver.close()
The running result is 
It has been successful That's all use selenium A script executed in
2. Use pytest frame perform
contain pytest–fixture,paramtrize
Create a new file named test The beginning represents the test
Write the following file
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. Open the browser "):
# selenium4 The above compilation method
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. Close the browser "):
# Close the browser
driver.close()
# Open the test website
@allure.feature(" Sign in ")
@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. Open the landing page "):
driver.get("http://114.116.97.187:8080/symfony/web/index.php/auth/login")
with allure.step("2. Enter the username and password "):
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. Click login "):
driver.find_element(By.ID, "btnLogin").click()
# Verify user login
with allure.step("4. Verify that the name is correct "):
assert firstName in driver.find_element(By.PARTIAL_LINK_TEXT, " welcome ").text
with allure.step("5. Screenshot verification login succeeded "):
driver.save_screenshot("login_" + txtUsername + ".png")
allure.attach.file("login_" + txtUsername + ".png", " Screenshot of successful user login ", attachment_type=allure.attachment_type.PNG)
# Log out
with allure.step("6. Log out "):
driver.find_element(By.ID, "welcome").click()
driver.find_element(By.LINK_TEXT, " Log out ").click()
# driver.find_element(By.XPATH,"//*[@id='welcome-menu']/ul/li[4]/a").click()
time.sleep(1)
Create a new one yaml file
write in Our user name and password ( How to write user name and password Above selenium In use cases )
yaml The file format is dictionary type
That is to say
- txtUsername: xxxxx
txtPassword: xxxxxxx
firstName: xxxxx
- txtUsername: xxxxx
txtPassword: xxxxxxx
firstName: xxxxx
- txtUsername: xxxxx
txtPassword: xxxxxxx
firstName: xxxxx
Execute this use case
The terminal enters the directory of the file
Input pytest Add file name ( This is to specify a file to execute )
3. Generate a report
The terminal enters the directory of the file
function pytest -q test_oranghrm_login.py --alluredir=./result(test…py Is your test file name )
After successful execution, the following test data will appear 
At this time we're going to Take these data Enter the mode as a report Displayed and detailed
terminal cd Enter the directory of your file
Input allure serve ./result (resul It is the file we just generated for data storage )
Try to use the form of three bursts to go out
Also in terminal CD Enter the directory of the file to be executed
Enter the command :pytest -n 3 pytest -n 3 file name --alluredir=./result/
So our summation practice is over
边栏推荐
- P6118 [JOI 2019 Final]珍しい都市 题解
- Cesium3Dtilesets 使用customShader的解读以及泛光效果示例
- 超参数调整和实验-训练深度神经网络 | PyTorch系列(二十六)
- Use try-with-resources or close this
- 第三章 队列
- 怎么简单实现菜单拖拽排序的功能
- First knowledge of C language -- structure, branch and loop statements
- Flutter神操作学习之(满级攻略)
- 程序里随处可见的interface,真的有用吗?真的用对了吗?
- ERD online 4.0.0 free private deployment scheme
猜你喜欢

MySQL is shown in the figure. The existing tables a and B need to be associated with a and B tables through projectcode to find idcardnum with different addresses.

基于stm32的恒功率无线充电

【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码

Explore flex basis

From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project

Typescript (zero) -- introduction, environment construction, first instance

使用PyTorch的TensorBoard-可视化深度学习指标 | PyTorch系列(二十五)
![[TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass](/img/85/2b6c9cf83340ee8bc01e85f77ec101.png)
[TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass
![[software testing] - unittest framework for automated testing](/img/7a/29b222cb0b6a5953b98f8d797cd106.png)
[software testing] - unittest framework for automated testing

mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。
随机推荐
Use of Day6 functions and modules
[wechat applet development (VI)] draw the circular progress bar of the music player
Typescript (zero) -- introduction, environment construction, first instance
mysql: error while loading shared libraries: libtinfo.so. 5 solutions
2020.7.7 eth price analysis
程序里随处可见的interface,真的有用吗?真的用对了吗?
Use try-with-resources or close this
Canonical Address
Representation of children and brothers of trees
[leetcode] 13. linked list cycle · circular linked list
windbg
Today in history: the father of database passed away; Apple buys cups code; IBM chip Alliance
P6118 [JOI 2019 Final]珍しい都市 题解
Notes for the fourth time of first knowing C language
【图像隐藏】基于DCT、DWT、LHA、LSB的数字图像信息隐藏系统含各类攻击和性能参数附matlab代码
特征值和特征向量
Eigenvalues and eigenvectors
Deep residual learning for image recognition shallow reading and Implementation
Introduction to the reduce() function in JS
Welcome to CSDN markdown editor Assad