1、环境配置
3、start_script.py启动脚本文件代码
import os# file_path 是自动化脚本文件file_path = "20.py"# xmlpth是生成的xml数据文件,用来生成最终报告xmlpath = "./xml"xmlStr = "pytest -s -q {file_path} --alluredir {xmlpath}".format(file_path=file_path, xmlpath=xmlpath)print("xmlStr",xmlStr)# 执行命令,生成xml文件a = os.system(xmlStr)# 生成报告,--clean会清除旧文件htmlStr = "allure generate {0} -o ./report/ --clean".format(xmlpath)os.system(htmlStr)
# file_path 是自动化脚本文件
file_path = "20.py"
# xmlpth是生成的xml数据文件,用来生成最终报告
xmlpath = "./xml"
xmlStr = "pytest -s -q {file_path} --alluredir {xmlpath}".format(file_path=file_path, xmlpath=xmlpath)
print("xmlStr",xmlStr)
# 执行命令,生成xml文件
a = os.system(xmlStr)
# 生成报告,--clean会清除旧文件
htmlStr = "allure generate {0} -o ./report/ --clean".format(xmlpath)
os.system(htmlStr)
4、20.py自动化测试脚本文件
所有的方法均调用的methods.py里面
import allure, os,syssys.path.insert(0,r"C:\\Users\\Administrator\\Desktop\\关于网站\\ccc\\爬虫系统\\go\\allure_test")from allure_test import methodsclass Test_20: def setup_class(self): methods.max_window() def teardown_class(self): methods.close() @allure.feature('打开京东') @allure.story('点击登陆')def test_case_15(self): '''用例名称京东-登录-百度-新闻-百度''' methods.get_url("https://www.jd.com", desc="打开京东") methods.click("xpath=>//*[contains(text(),'请登录')]", desc="登陆") methods.click("xpath=>//*[contains(text(),'账户登录')]", desc="切换账户登陆") methods.send_key("xpath=>//*[@id='loginname']", 188888888, desc="发送账户名密码") methods.wait(5) @allure.feature('打开京东') @allure.story('点击登陆') def test_case_16(self): '''用例名称京东-登录-百度-新闻-百度''' methods.get_url("https://www.jd.com", desc="打开京东") methods.click("xpath=>//*[contains(text(),'请登录')]", desc="登陆") methods.click("xpath=>//*[contains(text(),'账户登录')]", desc="切换账户登陆") methods.send_key("xpath=>//*[@id='loginname']", 188888888, desc="发送账户名密码") methods.wait(5)
sys.path.insert(0,r"C:\\Users\\Administrator\\Desktop\\关于网站\\ccc\\爬虫系统\\go\\allure_test")
from allure_test import methods
class Test_20:
def setup_class(self):
methods.max_window()
def teardown_class(self):
methods.close()
@allure.feature('打开京东')
@allure.story('点击登陆')
def test_case_15(self):
'''用例名称京东-登录-百度-新闻-百度'''
methods.get_url("https://www.jd.com", desc="打开京东")
methods.click("xpath=>//*[contains(text(),'请登录')]", desc="登陆")
methods.click("xpath=>//*[contains(text(),'账户登录')]", desc="切换账户登陆")
methods.send_key("xpath=>//*[@id='loginname']", 188888888, desc="发送账户名密码")
methods.wait(5)
@allure.feature('打开京东')
@allure.story('点击登陆')
def test_case_16(self):
'''用例名称京东-登录-百度-新闻-百度'''
methods.get_url("https://www.jd.com", desc="打开京东")
methods.click("xpath=>//*[contains(text(),'请登录')]", desc="登陆")
methods.click("xpath=>//*[contains(text(),'账户登录')]", desc="切换账户登陆")
methods.send_key("xpath=>//*[@id='loginname']", 188888888, desc="发送账户名密码")
methods.wait(5)
5、methods.py文件
举例说明
send_key方法:
1、loc :需要传入的元素位置,定义规则是xpath=>"*****",id=>"*****",这样我用split切分=>符号后,就可以获取元素选取方式和具体xpath路径或者id名字,例如 xpath=>//*[@id='loginname'] 切分后,可以得到["xpath","//*[@id='loginname'] "],供我定位选取元素使用
2、key:需要传入的值
3、desc:用例步骤描述
4、with allure.step用来记录步骤生成allure报告
def send_key(loc,key,desc=None): with allure.step(desc): try: getElement(loc).send_keys(Keys.CONTROL,'a') getElement(loc).send_keys(key) except Exception as e: raise e
with allure.step(desc):
try:
getElement(loc).send_keys(Keys.CONTROL,'a')
getElement(loc).send_keys(key)
except Exception as e:
raise e
# 获取单个页面元素def getElement(loc): try: by = loc.split("=>")[0] value = loc.split("=>")[1] element = WebDriverWait(driver, 10).until(lambda x: x.find_element(by=byTypeDict[by], value=value)) # print(element) return element except Exception as e: raise e
def getElement(loc):
try:
by = loc.split("=>")[0]
value = loc.split("=>")[1]
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(by=byTypeDict[by], value=value))
# print(element)
return element
except Exception as e:
raise e
def click(loc,desc=None): with allure.step(desc): try: print("这里是点击方法") getElement(loc).click() except Exception as e: raise e
with allure.step(desc):
try:
print("这里是点击方法")
getElement(loc).click()
except Exception as e:
raise e
import osfrom selenium.webdriver.common.keys import Keysfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.action_chains import ActionChainsfrom selenium import webdriverfrom selenium.webdriver.support.select import Selectfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECimport timeimport allurebyTypeDict = { "xpath": By.XPATH, "id": By.ID, "name": By.NAME, "class_name": By.CLASS_NAME, "tag_name": By.TAG_NAME, "link_text": By.LINK_TEXT, "partial_link_text": By.PARTIAL_LINK_TEXT, "css selector" : By.CSS_SELECTOR}driver = webdriver.Chrome(executable_path='C:\chrome\chromedriver246.exe')# 获取单个页面元素def getElement(loc): try: by = loc.split("=>")[0] value = loc.split("=>")[1] element = WebDriverWait(driver, 10).until(lambda x: x.find_element(by=byTypeDict[by], value=value)) # print(element) return element except Exception as e: raise e# 获取一组相同的元素,以列表形式返回def getElements(loc): try: by = loc.split("=>")[0] value = loc.split("=>")[1] elements = WebDriverWait(driver, 5).until(lambda x: x.find_elements(by=byTypeDict[by], value=value)) return elements except Exception as e: raise edef get_url(*args, desc=None): with allure.step(desc): try: driver.get(args[0]) except Exception as e: raise edef wait(*args, desc=None): with allure.step(desc): try: time.sleep(args[0]) except Exception as e: raise edef max_window(*args, **kwargs): try: driver.maximize_window() except Exception as e: raise edef close(desc=None): with allure.step(desc): try: driver.quit() except Exception as e: raise edef click(loc,desc=None): with allure.step(desc): try: print("这里是点击方法") getElement(loc).click() except Exception as e: raise edef send_key(loc,key,desc=None): with allure.step(desc): try: getElement(loc).send_keys(Keys.CONTROL,'a') getElement(loc).send_keys(key) except Exception as e: raise e
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
import allure
byTypeDict = {
"xpath": By.XPATH,
"id": By.ID,
"name": By.NAME,
"class_name": By.CLASS_NAME,
"tag_name": By.TAG_NAME,
"link_text": By.LINK_TEXT,
"partial_link_text": By.PARTIAL_LINK_TEXT,
"css selector" : By.CSS_SELECTOR
}
driver = webdriver.Chrome(executable_path='C:\chrome\chromedriver246.exe')
# 获取单个页面元素
def getElement(loc):
try:
by = loc.split("=>")[0]
value = loc.split("=>")[1]
element = WebDriverWait(driver, 10).until(lambda x: x.find_element(by=byTypeDict[by], value=value))
# print(element)
return element
except Exception as e:
raise e
# 获取一组相同的元素,以列表形式返回
def getElements(loc):
try:
by = loc.split("=>")[0]
value = loc.split("=>")[1]
elements = WebDriverWait(driver, 5).until(lambda x: x.find_elements(by=byTypeDict[by], value=value))
return elements
except Exception as e:
raise e
def get_url(*args, desc=None):
with allure.step(desc):
try:
driver.get(args[0])
except Exception as e:
raise e
def wait(*args, desc=None):
with allure.step(desc):
try:
time.sleep(args[0])
except Exception as e:
raise e
def max_window(*args, **kwargs):
try:
driver.maximize_window()
except Exception as e:
raise e
def close(desc=None):
with allure.step(desc):
try:
driver.quit()
except Exception as e:
raise e
def click(loc,desc=None):
with allure.step(desc):
try:
print("这里是点击方法")
getElement(loc).click()
except Exception as e:
raise e
def send_key(loc,key,desc=None):
with allure.step(desc):
try:
getElement(loc).send_keys(Keys.CONTROL,'a')
getElement(loc).send_keys(key)
except Exception as e:
raise e
7、启动脚本python3 start_script.py
源码:
公众号回复:测试报告,获取源码
python爬虫人工智能大数据公众号