1.Web自动化登陆及登陆验证处理
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://cart.taobao.com/cart.htm")
driver.find_element("id","fm-login-id").send_keys("云纱璃英")
driver.find_element("id","fm-login-password").send_keys("xxx")
driver.find_element("xpath","//div[@class='fm-btn']").click()
import os
from selenium import webdriver
from time import sleep
"""
使用编写的脚本以debug模式启动谷歌浏览器,脚本内容:
chrome.exe --remote-debugging-port=9222
必须通过os.popen执行,通过os.system执行命令无效
"""
os.popen("d:/chrome.bat")
sleep(5)
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options = options)
sleep(2)
driver.find_element_by_xpath("//ul/li[2]/div/div[2]/div[1]/a")
handls = driver.window_handles
print(handls)
driver.switch_to.window(handls[1])
driver.find_element_by_xpath('//ul[@data-property="颜色分类"]/li').click()
2.Web自动化项目框架搭建
1.基于Pytest进行用例管理
def test_cart_login(browser):
browser.get("https://cart.taobao.com/cart.htm")
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
browser.find_element('id','fm-login-password').send_keys('xxx')
browser.find_element('xpath',"//div[@class='fm-btn']").click()
"""
装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
a.可以获取测试用例的信息,比如用例函数的描述
b.可以获取测试用例的结果,yield,返回一个result对象
"""
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
2.关键字驱动
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
def open(self,url):
self.driver.get(url)
def locator(self,name,value):
"""
:param name: 元素定位的方式
:param value: 元素定位的路径
:return:
"""
el = self.driver.find_element(name,value)
self.locate_station(el)
return el
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green"
)
import pytest
from class36.p03.key_world.keyword import WebKeys
@pytest.mark.skip
def test_cart_login(browser):
browser.get("https://cart.taobao.com/cart.htm")
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
browser.find_element('id','fm-login-password').send_keys('xxx')
browser.find_element('xpath',"//div[@class='fm-btn']").click()
def test_case_login2(browser):
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
wk.locator('id','fm-login-id').send_keys('云纱璃英')
wk.locator('id','fm-login-password').send_keys('xxx')
wk.locator('xpath',"//div[@class='fm-btn']").click()
import os
from time import sleep
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
"""
全局定义浏览器驱动,方便下面的hook函数引用driver
:return:
"""
global driver
os.popen("d:/chrome.bat")
sleep(3)
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
driver.implicitly_wait(10)
"""
yield之前的代码是用例前置
yield之后的代码是用例后置
"""
yield driver
"""
这种debug模式,下面的方法无法退出浏览器
driver.quit()
driver.close()
"""
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
"""
装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
a.可以获取测试用例的信息,比如用例函数的描述
b.可以获取测试用例的结果,yield,返回一个result对象
"""
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
3.PO模式
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
def open(self,url):
self.driver.get(url)
def locator(self,name,value):
"""
:param name: 元素定位的方式
:param value: 元素定位的路径
:return:
"""
el = self.driver.find_element(name,value)
self.locate_station(el)
return el
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green"
)
from class36.p04.key_world.keyword import WebKeys
class CatLoginPage(WebKeys):
def login(self,username,password):
self.open("https://cart.taobao.com/cart.htm")
self.locator('id', 'fm-login-id').send_keys(username)
self.locator('id', 'fm-login-password').send_keys(password)
self.locator('xpath', "//div[@class='fm-btn']").click()
import pytest
from class36.p04.key_world.keyword import WebKeys
from class36.p04.page.cartLogin_page import CatLoginPage
@pytest.mark.skip
def test_cart_login(browser):
browser.get("https://cart.taobao.com/cart.htm")
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
browser.find_element('id','fm-login-password').send_keys('xxx')
browser.find_element('xpath',"//div[@class='fm-btn']").click()
@pytest.mark.skip
def test_case_login2(browser):
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
wk.locator('id','fm-login-id').send_keys('云纱璃英')
wk.locator('id','fm-login-password').send_keys('xxx')
wk.locator('xpath',"//div[@class='fm-btn']").click()
def test_case_login3(browser):
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
import os
from time import sleep
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
"""
全局定义浏览器驱动,方便下面的hook函数引用driver
:return:
"""
global driver
os.popen("d:/chrome.bat")
sleep(3)
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
driver.implicitly_wait(10)
"""
yield之前的代码是用例前置
yield之后的代码是用例后置
"""
yield driver
"""
这种debug模式,下面的方法无法退出浏览器
driver.quit()
driver.close()
"""
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
"""
装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
a.可以获取测试用例的信息,比如用例函数的描述
b.可以获取测试用例的结果,yield,返回一个result对象
"""
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
4.PO模式优化,元素定位分离
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
def open(self,url):
self.driver.get(url)
def locator(self,name,value):
"""
:param name: 元素定位的方式
:param value: 元素定位的路径
:return:
"""
el = self.driver.find_element(name,value)
self.locate_station(el)
return el
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green"
)
"""
淘宝首页登录
https://login.taobao.com/
page_indexLogin
"""
page_indexLogin_user = ['id','fm-login-id']
page_indexLogin_pwd = ['id','fm-login-password']
page_indexLogin_loginBtn = ['xpath', "//div[@class='fm-btn']"]
from class36.p05.key_world.keyword import WebKeys
from class36.p05.page import allPages
class CatLoginPage(WebKeys):
def login(self,username,password):
self.open("https://cart.taobao.com/cart.htm")
self.locator(*allPages.page_indexLogin_user).send_keys(username)
self.locator(*allPages.page_indexLogin_pwd).send_keys(password)
self.locator(*allPages.page_indexLogin_loginBtn).click()
import pytest
from class36.p05.key_world.keyword import WebKeys
from class36.p05.page.cartLogin_page import CatLoginPage
@pytest.mark.skip
def test_cart_login(browser):
browser.get("https://cart.taobao.com/cart.htm")
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
browser.find_element('id','fm-login-password').send_keys('xxx')
browser.find_element('xpath',"//div[@class='fm-btn']").click()
@pytest.mark.skip
def test_case_login2(browser):
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
wk.locator('id','fm-login-id').send_keys('云纱璃英')
wk.locator('id','fm-login-password').send_keys('xxx')
wk.locator('xpath',"//div[@class='fm-btn']").click()
def test_case_login3(browser):
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
import os
from time import sleep
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
"""
全局定义浏览器驱动,方便下面的hook函数引用driver
:return:
"""
global driver
os.popen("d:/chrome.bat")
sleep(3)
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
driver.implicitly_wait(10)
"""
yield之前的代码是用例前置
yield之后的代码是用例后置
"""
yield driver
"""
这种debug模式,下面的方法无法退出浏览器
driver.quit()
driver.close()
"""
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
"""
装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
a.可以获取测试用例的信息,比如用例函数的描述
b.可以获取测试用例的结果,yield,返回一个result对象
"""
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()
5.PO模式优化,流程复用和流程备注
from selenium import webdriver
class WebKeys:
def __init__(self,driver):
self.driver = driver
def open(self,url):
self.driver.get(url)
def locator(self,name,value):
"""
:param name: 元素定位的方式
:param value: 元素定位的路径
:return:
"""
el = self.driver.find_element(name,value)
self.locate_station(el)
return el
def locate_station(self,element):
self.driver.execute_script(
"arguments[0].setAttribute('style',arguments[1]);",
element,
"border: 2px solid green"
)
def get_title(self):
return self.driver.title
"""
淘宝首页登录
https://login.taobao.com/
page_indexLogin
"""
page_indexLogin_user = ['id','fm-login-id']
page_indexLogin_pwd = ['id','fm-login-password']
page_indexLogin_loginBtn = ['xpath', "//div[@class='fm-btn']"]
"""
购物车详情页
https://cart.taobao.com/cart.htm
page_cartDetail
"""
page_cartDetail_selectAll = ["id","J_SelectAll1"]
page_cartDetail_payBtn = ["xpath","//div[@class='btn-area']"]
page_cartDetail_submitBtn = ["link text","提交订单"]
page_cartDetail_delBtn = ["link text","删除"]
page_cartDetail_closeBtn = ["partial link text","闭"]
page_cartDetail_confirmBtn = ["partial link text","确"]
page_cartDetail_colorClum = "//p[@class='sku-line']"
page_cartDetail_modifyBtn = ["xpath","//span[text()='修改']"]
page_cartDetail_colors = ["xpath","//div[@class='sku-edit-content']/div/dl/dd/ul/li[2]"]
page_cartDetail_colorsConfirmBtn = ["xpath","//div[@class='sku-edit-content']/div/p/a"]
page_cartDetail_favorites = ["link text","移入收藏夹"]
page_cartDetail_singleGoodsClum = "//div[@class='order-content']"
page_cartDetail_similarGoodsLink = ["link text","相似宝贝"]
page_cartDetail_simlarGoddsNext = ["xpath","//a[contains(@class,'J_fs_next')]"]
page_cartDetail_simlarGoods = ["xpath","//div[@class='find-similar-body']/div/div/div[2]"]
from time import sleep
import allure
from class36.p06.key_world.keyword import WebKeys
from class36.p06.page import allPages
from class36.p06.page.cartLogin_page import CatLoginPage
class CartPage(WebKeys):
@allure.step("商品结算")
def pay(self):
with allure.step("流程代码路径: %s" % __file__):
pass
loginPage = CatLoginPage(self.driver)
loginPage.login('云纱璃英','xxx')
with allure.step("点击全选按钮"):
self.locator(*allPages.page_cartDetail_selectAll).click()
sleep(1)
with allure.step("点击结算按钮"):
self.locator(*allPages.page_cartDetail_payBtn).click()
sleep(1)
from time import sleep
import allure
from class36.p06.key_world.keyword import WebKeys
from class36.p06.page import allPages
class CatLoginPage(WebKeys):
@allure.step("登录")
def login(self,username,password):
with allure.step("流程代码路径: %s"%__file__):
pass
with allure.step("打开网页")
self.open("https://cart.taobao.com/cart.htm")
with allure.step("输入账户"):
self.locator(*allPages.page_indexLogin_user).send_keys(username)
with allure.step("输入密码"):
self.locator(*allPages.page_indexLogin_pwd).send_keys(password)
with allure.step("点击登录"):
self.locator(*allPages.page_indexLogin_loginBtn).click()
sleep(2)
result = self.get_title()
expect = "购物车"
with allure.step("结果检查:(预期){1} in 实际{0}".format(result,expect)):
assert expect in result
import pytest
from class36.p06.key_world.keyword import WebKeys
from class36.p06.page.cartLogin_page import CatLoginPage
from class36.p06.page.cart_page import CartPage
@pytest.mark.skip
def test_cart_login(browser):
browser.get("https://cart.taobao.com/cart.htm")
browser.find_element('id','fm-login-id').send_keys('云纱璃英')
browser.find_element('id','fm-login-password').send_keys('xxx')
browser.find_element('xpath',"//div[@class='fm-btn']").click()
@pytest.mark.skip
def test_case_login2(browser):
wk = WebKeys(browser)
wk.open("https://cart.taobao.com/cart.htm")
wk.locator('id','fm-login-id').send_keys('云纱璃英')
wk.locator('id','fm-login-password').send_keys('xxx')
wk.locator('xpath',"//div[@class='fm-btn']").click()
def test_case_login3(browser):
cartLogin = CatLoginPage(browser)
cartLogin.login('云纱璃英','xxx')
def test_case_pay(browser):
cartPage = CartPage(browser)
cartPage.pay()
import os
from time import sleep
import allure
import pytest
from selenium import webdriver
@pytest.fixture(scope='function')
def browser():
"""
全局定义浏览器驱动,方便下面的hook函数引用driver
:return:
"""
global driver
os.popen("d:/chrome.bat")
sleep(3)
options = webdriver.ChromeOptions()
options.debugger_address = '127.0.0.1:9222'
driver = webdriver.Chrome(options=options)
sleep(2)
driver.implicitly_wait(10)
"""
yield之前的代码是用例前置
yield之后的代码是用例后置
"""
yield driver
"""
这种debug模式,下面的方法无法退出浏览器
driver.quit()
driver.close()
"""
os.system('taskkill /im chromedriver.exe /F')
os.system('taskkill /im chrome.exe /F')
"""
装饰器@pytest.hookimpl(hookwrapper=True)等价于@pytest.mark.hookwrapper的作用:
a.可以获取测试用例的信息,比如用例函数的描述
b.可以获取测试用例的结果,yield,返回一个result对象
"""
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport():
out = yield
"""
从返回一个result对象(out)获取调用结果的测试报告,返回一个report对象
report对象的属性
包括when(setup,call,teardown三个值)、nodeid(测试用例的名字)、
outcome(用例的执行结果:passed,failed)
"""
report = out.get_result()
if report.when == 'call':
xfail = hasattr(report,"wasfail")
if(report.skipped and xfail) or (report.failed and not xfail):
with allure.step("添加失败截图。。"):
allure.attach(driver.get_screenshot_as_png(),"失败截图",allure.attachment_type.PNG)
import os
import pytest
def run():
pytest.main(['-v','--alluredir','./result','--clean-alluredir','--allure-no-capture'])
os.system('allure generate ./result -o ./report_allure/ --clean')
if __name__ == '__main__':
run()