1.基准:base.py包含一些基本的方法定位元素、执行操作等
class basepagecl:
def __init__(self,driver):
self.driver=driver
# 进入页面
def open(self,url):
self.driver.get(url)
# 退出页面
def quit(self):
self.driver.quit()
# 定位元素
def loctor(self,loct):
return self.driver.find_element(*loct)
# 输入
def input(self,loct,text):
self.loctor(loct).send_keys(text)
# 点击
def click(self,loct):
self.loctor(loct).click()
2.基于base写 登录页面的方法
from selenium.webdriver.common.by import By
from base.basepage import basepagecl
class loginpagecl(basepagecl):
url = 'http://666666666/shopxo/index.php?s=/index/user/logininfo.html'
username = (By.NAME, 'accounts')
pwd = (By.NAME, 'pwd')
button = (By.XPATH, '/html/body/div[4]/div/div[2]/div[2]/form/div[3]/button')
def login(self,username1,pwd1):
self.open(self.url)
self.input(self.username,username1)
self.input(self.pwd,pwd1)
self.click(self.button)
3. 独立的测试用例文件;通过@pytest.mark.parametrize('data',Getdata()
.get('../data/logindata.yaml'))方法获取数据;且该目录下存放浏览器的驱动文件
import pytest
from selenium import webdriver
from getdata.getdataa import Getdata
from login_page.loginpage import loginpagecl
import time
from base.basepage import basepagecl
class TestCases:
@pytest.mark.parametrize('data',Getdata().get('../data/logindata.yaml'))
def test_login(self,data):
driver=webdriver.Firefox()
login1=loginpagecl(driver)
login1.login(data['username'],data['pwd'])
time.sleep(5)
if __name__ == '__main__':
pytest.main(['-s','--html=../report/report.html'])
basepagecl.quit()
4.通过yaml文件获取数据的代码
import yaml class Getdata: def get(self,path): file=open(path,'r',encoding='utf-8') data=yaml.load(file,yaml.FullLoader) return data
5. yaml格式数据,此格式为字典列表;此处username、pwd分别为两个变量名;步骤3中获取数据时也需要为这两个变量
- username: sysadmin1 pwd: sysadmin1 - username: 123456 pwd: 124333 - username: sysadmin pwd: sysadmin