测试演示shopXO: https://demo.shopxo.net/
1.编写一个pytest脚本文件
import pytest
import pytest_html
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import allure
@allure.feature("购物测试类")
class Test_New(object):
@pytest.fixture(scope='session')
def driver(self):
global _browser
_browser = webdriver.Chrome(executable_path="C:\chromedriver\chromedriver")
_browser.maximize_window()
yield _browser
_browser.close()
def screen(self,driver,s):
if s is not None:
allure.attach(driver.get_screenshot_as_png(), s, allure.attachment_type.PNG) #保存截图为allure的附件
@allure.story("流程1")
@allure.title("游客登录")
def test_guest(self,driver):
driver.get("https://demo.shopxo.net")
self.screen(driver,'进入首页') #保存当前截图
driver.implicitly_wait(2)
driver.find_element_by_xpath("//div[@class='member-login']//a[1]").click()
self.screen(driver,'点击登录后') #保存当前截图
driver.implicitly_wait(2)
driver.find_element_by_xpath("//div[@class='forgetpwd-top']//a[2]").click()
WebDriverWait(driver,10).until(EC.text_to_be_present_in_element((By.XPATH,"//div[@class='am-g my-content tips-success']//span[@class='msg']"),"登录成功"))
self.screen(driver,'点击游客后') # 保存当前截图
driver.implicitly_wait(2)
if __name__=='__main__':
pytest.main(['-s','--disable-warnings','test_new.py','--alluredir', './allure_report'])
运行后就可以看每次操作的截图,可以封装webdriver操作进行截图,运行效果: