浏览器copy出来的xpath可读性比较差,在做自动化项目的时候,最好还是自己写xpath
//*[@id="app"]/div/div[2]/div[1]/div/form/div[1]/div/div[2]/input
//*[@id="app"]/div/div[2]/div[1]/div/form/div[2]/div/div[2]/input
//*[@id="app"]/div/div[2]/div[1]/div/form/div[3]/div/div/div[1]/input
/html/body/div[2]/div[1]/div[1]/ul/li[4]
//*[@id="app"]/div/div[2]/div[1]/div/form/div[5]/div/button
//*[@id="app"]/div/section/header/div[2]/div/div/span[2]
//*[@id="app"]/div/section/section/main/div/section/aside/div/div[2]/div/div[1]/div[1]/span
//*[@id="app"]/div/section/section/main/div/section/aside/div/div[2]/div/div[1]/div[2]/div[3]/div[1]
//*[@id="app"]/div/section/section/main/div/section/main/div/div[1]/div[1]/button[1]
/html/body/div[2]/div/div[2]/form/div[1]/div/div[1]/input
/html/body/div[2]/div/div[2]/form/div[2]/div/div[2]/input
/html/body/div[2]/div/div[2]/form/div[3]/div/div[2]/input
/html/body/div[2]/div/div[2]/form/div[6]/div/div/div/input
/html/body/div[2]/div[1]/div[1]/ul/li[2]
/html/body/div[2]/div/div[2]/form/div[8]/div/div/div/input
/html/body/div[2]/div[1]/div[1]/ul/li[1]
/html/body/div[2]/div/div[2]/form/div[9]/div/div/div/input
/html/body/div[2]/div[1]/div[1]/ul/li[1]
/html/body/div[4]/div/div[3]/div/button[1]
//form[contains(@class,'loginForm')]/div[1]/div/div[2]/input
//form[contains(@class,'loginForm')]//div[2]/input[contains(@type,'password')]
//form[contains(@class,'loginForm')]//input[contains(@placeholder,'请选择公司')]
//li/span[contains(text(),'乐才云餐饮集团yzc')]/parent:: *
//span[contains(text(),'登录')]/parent:: *
//span[contains(text(),'乐才云餐饮集团yzc')]/ancestor::div[contains(@role,'tree')]/div/div//span
//span[contains(text(),'美丽狮tes')]/ancestor::div[@class='custom-tree-node']
//span[contains(text(),'新增员工')]/parent:: button
//form/div/label[@for='userName']/following-sibling::div//input
//form/div/label[@for='phone']/following-sibling::div/div[2]/input
//form/div/label[@for='pnid']/following-sibling::div/div[2]/input
//form/div/label[@for='gangweiId']/following-sibling::div//input
//li/span[contains(text(),'项目经理')]/parent:: *
//form/div/label[@for='workStatus']/following-sibling::div//input
//li/span[contains(text(),'合同工')]/parent:: *
//form/div/label[@for='userStatus']/following-sibling::div//input
//li/span[contains(text(),'实习')]/parent:: *
//span[contains(text(),'下一步')]/parent:: *
关键字 | 表达式 | 描述 |
---|---|---|
following-sibling | ./following-sibling:: * | 选取当前节点之后的兄弟节点 |
比如:
//form/div/label[@for='workStatus']/following-sibling::div//input
关键字 | 表达式 | 描述 |
---|---|---|
parent | ./parent:: * | 选取当前节点的父节点 |
比如:
//span[contains(text(),'下一步')]/parent:: *
关键字 | 表达式 | 描述 |
---|---|---|
ancestor | ./ancestor:: * | 选取当前节点的所有先辈节点(父、祖父) |
比如:
//span[contains(text(),'乐才云餐饮集团yzc')]/ancestor::div[contains(@role,'tree')]/div/div//span
from selenium.webdriver import *
from common.utils import *
import pytest
import allure
class TestJoyHR:
@pytest.fixture(scope="module", autouse=True)
def before(self):
global driver
global entry_phone
global entry_card
global entry_name
entry_name = generate_person_name_yzc()
entry_card = generate_id_card_yzc()
entry_phone = generate_mobile_number_yzc()
driver = Chrome()
# 最大化窗口
driver.maximize_window()
# 设置默认的等待时长
driver.implicitly_wait(15)
yield
driver.quit()
@allure.feature("模块名:登录模块")
@allure.story("功能名:登录功能")
@allure.testcase("https://blog.csdn.net/qq_37251897", "查看测试用例")
@allure.step(title=u'输入账号和密码,点击登录')
@pytest.mark.parametrize('url, phone_xpath, password_xpath, select_xpath, '
'company_xpath, submit_xpath, user_xpath, phone, password, expected',
[
(
'https://tes.xinlecai.cn/#/login',
'//form[contains(@class,\'loginForm\')]/div[1]/div/div[2]/input',
'//form[contains(@class,\'loginForm\')]//div[2]/input[contains(@type,\'password\')]',
'//form[contains(@class,\'loginForm\')]//input[contains(@placeholder,\'请选择公司\')]',
'//li/span[contains(text(),\'乐才云餐饮集团yzc\')]/parent:: *',
'//span[contains(text(),\'登录\')]/parent:: *',
'//*[@id="app"]/div/section/header/div[2]/div/div/span[2]',
'17721038951',
'0000000000',
u'杨振春'
)
],
ids=['使用正确的账号和密码登录成功']
)
@pytest.mark.run(order=1)
def test_login(self, url, phone_xpath, password_xpath, select_xpath, company_xpath, submit_xpath, user_xpath,
phone, password, expected):
"""
测试用例
:param url:
:param phone_xpath:
:param password_xpath:
:param select_xpath:
:param company_xpath:
:param submit_xpath:
:param user_xpath:
:param phone:
:param password:
:param expected:
:return:
"""
global driver
# 打开网页
driver.get(url)
# 输入账号
driver.find_element_by_xpath(phone_xpath).send_keys(phone)
# 输入密码
driver.find_element_by_xpath(password_xpath).send_keys(password)
driver.find_element_by_xpath(select_xpath).click()
li_element = driver.find_element_by_xpath(company_xpath)
ActionChains(driver).move_to_element(li_element).perform()
li_element.click()
driver.find_element_by_xpath(submit_xpath).click()
span_element = driver.find_element_by_xpath(user_xpath)
assert expected in span_element.text
@allure.feature("模块名:员工管理")
@allure.story("功能名:员工入职")
@allure.testcase("https://blog.csdn.net/qq_37251897", "查看测试用例")
@allure.step(title=u'录入基本信息,点击下一步')
@pytest.mark.parametrize('url, root_xpath, dept_xpath, add_xpath, name_xpath, phone_xpath, '
'card_xpath, job_xpath, job_li_xpath, work_xpath, work_li_xpath, '
'status_xpath, status_li_xpath, next_xpath',
[
(
'https://tes.xinlecai.cn/#/personnelStaff',
'//span[contains(text(),\'乐才云餐饮集团yzc\')]/ancestor::div[contains(@role,\'tree\')]/div/div/span',
'//span[contains(text(),\'美丽狮tes\')]/ancestor::div[@class=\'custom-tree-node\']',
'//span[contains(text(),\'新增员工\')]/parent:: button',
'//form/div/label[@for=\'userName\']/following-sibling::div//input',
'//form/div/label[@for=\'phone\']/following-sibling::div/div[2]/input',
'//form/div/label[@for=\'pnid\']/following-sibling::div/div[2]/input',
'//form/div/label[@for=\'gangweiId\']/following-sibling::div//input',
'//li/span[contains(text(),\'项目经理\')]/parent:: *',
'//form/div/label[@for=\'workStatus\']/following-sibling::div//input',
'//li/span[contains(text(),\'合同工\')]/parent:: *',
'//form/div/label[@for=\'userStatus\']/following-sibling::div//input',
'//li/span[contains(text(),\'实习\')]/parent:: *',
'//span[contains(text(),\'下一步\')]/parent:: *',
)
],
ids=['填写正确的入职信息,入职成功']
)
@pytest.mark.run(order=2)
def test_user_entry_1(self, url, root_xpath, dept_xpath, add_xpath, name_xpath, phone_xpath, card_xpath, job_xpath, job_li_xpath,
work_xpath, work_li_xpath, status_xpath, status_li_xpath, next_xpath):
global driver
global entry_name
global entry_phone
global entry_card
# 打开网页
driver.get(url)
sleep(3)
driver.find_element_by_xpath(root_xpath).click()
sleep(1)
driver.find_element_by_xpath(dept_xpath).click()
sleep(1)
driver.find_element_by_xpath(add_xpath).click()
sleep(1)
driver.find_element_by_xpath(name_xpath).click()
driver.find_element_by_xpath(name_xpath).send_keys(entry_name)
driver.find_element_by_xpath(phone_xpath).send_keys(entry_phone)
driver.find_element_by_xpath(card_xpath).send_keys(entry_card)
driver.find_element_by_xpath(name_xpath).click()
sleep(1)
driver.find_element_by_xpath(job_xpath).click()
sleep(1)
driver.find_element_by_xpath(job_li_xpath).click()
driver.find_element_by_xpath(work_xpath).click()
sleep(1)
driver.find_element_by_xpath(work_li_xpath).click()
driver.find_element_by_xpath(status_xpath).click()
sleep(1)
driver.find_element_by_xpath(status_li_xpath).click()
driver.find_element_by_xpath(next_xpath).click()
sleep(3)
@allure.feature("模块名:员工管理")
@allure.story("功能名:员工入职")
@allure.testcase("https://blog.csdn.net/qq_37251897", "查看测试用例")
@allure.step(title=u'完善员工信息,点击保存')
@pytest.mark.parametrize('eng_name_path, area_path, entry_time_path, shop_time_path, work_type_path, '
'full_work_path, kq_status_path, kq_daka_path, hurry_person_path, '
'hurry_relation_path, other_path, hurry_phone_path, zjzl_id, card_path, '
'notice_path, save_path, alert_path, eng_name, area, entry_time, shop_time, hurry_person, '
'hurry_phone, card_jpg, notice_jpg, expected',
[
(
'//label[text()=\'英文名\']/following-sibling::div/div[2]/input',
'//label[text()=\'内外场\']/following-sibling::div/div[2]/input',
'//label[text()=\'入职时间\']/following-sibling::div/div/input',
'//label[text()=\'进店时间\']/following-sibling::div/div/input',
'//label[text()=\'工作类型\']/following-sibling::div/div/div/input',
'//li/span[text()=\'全职工\']/parent:: *',
'//label[text()=\'考勤状态\']/following-sibling::div/div/div/input',
'//li/span[text()=\'考勤打卡\']/parent:: *',
'//label[text()=\'紧急联系人\']/following-sibling::div/div/input',
'//label[text()=\'联系人关系\']/following-sibling::div/div/div/input',
'//li/span[text()=\'其他\']/parent:: *',
'//label[text()=\'联系人电话\']/following-sibling::div/div/input',
'tab-empEntryInfos',
'//p[contains(text(),\'身份证\')]/ancestor::tr//input[contains(@type,\'file\')]',
'//p[contains(text(),\'入职通知书\')]/ancestor::tr//input[contains(@type,\'file\')]',
'//div[contains(@class,\'footerSlot\')]//button/span[text()=\'保存\']/parent:: *',
'//*[contains(text(), \'添加成功\')]',
'Eason', '外场', '2020-08-12', '2020-08-12', 'zhangsan', '13312345678',
'D:/temp11/身份证.JPG', 'D:/temp11/健康证.JPEG', '添加成功'
)
],
ids=['填写正确的信息,保存成功']
)
@pytest.mark.run(order=3)
def test_user_entry_2(self, eng_name_path, area_path, entry_time_path, shop_time_path,
work_type_path, full_work_path, kq_status_path, kq_daka_path,
hurry_person_path, hurry_relation_path, other_path, hurry_phone_path,
zjzl_id, card_path, notice_path, save_path, alert_path,
eng_name, area, entry_time, shop_time, hurry_person, hurry_phone,
card_jpg, notice_jpg, expected):
global driver
global entry_name
global entry_phone
global entry_card
driver.find_element_by_xpath(eng_name_path).send_keys(eng_name)
driver.find_element_by_xpath(area_path).send_keys(area)
driver.find_element_by_xpath(entry_time_path).send_keys(entry_time)
driver.find_element_by_xpath(shop_time_path).send_keys(shop_time)
driver.find_element_by_xpath(work_type_path).click()
sleep(1)
driver.find_element_by_xpath(full_work_path).click()
driver.find_element_by_xpath(kq_status_path).click()
sleep(1)
driver.find_element_by_xpath(kq_daka_path).click()
driver.find_element_by_xpath(hurry_person_path).send_keys(hurry_person)
driver.find_element_by_xpath(hurry_relation_path).click()
sleep(1)
driver.find_element_by_xpath(other_path).click()
driver.find_element_by_xpath(hurry_phone_path).send_keys(hurry_phone)
driver.find_element_by_id(zjzl_id).click()
sleep(1)
driver.find_element_by_xpath(card_path).send_keys(card_jpg)
sleep(1)
driver.find_element_by_xpath(notice_path).send_keys(notice_jpg)
sleep(1)
driver.find_element_by_xpath(save_path).click()
sleep(2)
div_element = driver.find_element_by_xpath(alert_path)
assert expected in div_element.text
sleep(5)
@allure.feature("模块名:xxx")
@allure.story("功能名:xxx")
@allure.testcase("https://blog.csdn.net/qq_37251897", "查看测试用例")
@allure.step(title=u'xxx')
@pytest.mark.parametrize('url',
[
(
'https://www.baidu.cn'
)
],
ids=['xxx']
)
@pytest.mark.skip(reason="跳过执行这条case")
@pytest.mark.run(order=4)
def test_xxx(self, url):
# 打开网页
driver.get(url)
参考资料
[01] Xpath基本语法
[02] scrapy框架xpath和css选择器语法
[03] XPath 语法
[04] XPath 轴
微信扫一扫关注公众号
点击链接加入群聊
https://jq.qq.com/?_wv=1027&k=5eVEhfN
软件测试学习交流QQ群号:511619105