当前位置:网站首页>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
边栏推荐
- From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
- Product axure9 English version, using repeater repeater repeater to realize multi-choice and single choice
- Welcome to CSDN markdown editor Assad
- [software testing] - unittest framework for automated testing
- 0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence
- 初识C语言 -- 结构体,分支和循环语句
- pytest最好的测试框架
- 2020.7.7 eth price analysis
- New infrastructure helps the transformation and development of intelligent road transportation
- 【TA-霜狼_may-《百人计划》】图形3.7 移动端TP(D)R架构
猜你喜欢

Introduction to the reduce() function in JS

AWS elastic three swordsman

Email security report in the second quarter: email attacks have soared fourfold, and well-known brands have been used to gain trust

CNN循环训练的解释 | PyTorch系列(二十二)

Center-based 3D Object Detection and Tracking(基于中心的3D目标检测和跟踪 / CenterPoint)论文笔记

Sqlserver problem solving: replication components are not installed on this server. Please run SQL Server Setup again and select the option to install replication components
![[leetcode] 13. linked list cycle · circular linked list](/img/58/c8796bb5ed96d09325b8f2fa6a709e.png)
[leetcode] 13. linked list cycle · circular linked list
![[wechat applet development (VI)] draw the circular progress bar of the music player](/img/eb/9ce5d196970a6d6a887bf3e1d742ee.png)
[wechat applet development (VI)] draw the circular progress bar of the music player

mysql 如图所示,现有表a,表b,需求为 通过projectcode关联a、b表,查出address不同的 idcardnum。

树的孩子兄弟表示法
随机推荐
[TA frost wolf \u may hundred people plan] Figure 3.5 early-z and z-prepass
unordered_ The hash function of map and the storage mode of hash bucket
修改MySQL密码的四种方法(适合初学者)
Welcome to CSDN markdown editor Assad
【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码
POC模拟攻击利器 —— Nuclei入门(一)
Share an esp32 relay
[TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal
Is it safe to buy funds on Alipay? I want to make a fixed investment in the fund
【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass
what‘s the meaning of “rc“ in release name
[software testing] - unittest framework for automated testing
"The faster the code is written, the slower the program runs"
分层图解决的一些最短路问题
First knowledge of C language -- structure, branch and loop statements
Deep residual learning for image recognition shallow reading and Implementation
A brief analysis of the differences between functional testing and non functional testing, recommended by Shanghai haokoubei software testing company
2022.7.8 eth price analysis
How do gateways and chirpstacks in lorawan communicate? UDP? GRPC? MQTT?
0 dynamic programming medium leetcode873. Length of the longest Fibonacci subsequence