登陆
场景:token过期时间为1天但每天零点会强制失效需重新登陆
from common.deal_data import *
from common.timeShift import *
import json
import os
from seleniumbase import BaseCase
BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
print(BASE_PATH)
data_file_path = os.path.join(BASE_PATH, "config", "config.ini")
cookie_path = os.path.join(BASE_PATH, "testCases", "testDeliverySystem", "saved_cookies", "cookies.txt")
data = data.load_ini(data_file_path)["userInfo"]
print(data)
userName = data['userName']
passWord = data['passWord']
class LoginPage(BaseCase):
"""没有cookie文件或者cookie失效时"""
def login(self, une=userName, pwd=passWord):
self.open("https://xxxx/login")
self.type('#normal_login_account', une)
self.type('#normal_login_password', pwd)
self.click("(//*[@class='ant-form-item-control-input']/div)[3]/button")
time.sleep(3)
"""判断是否已有cookie文件且cookie在失效时间内"""
def get_cookie(self):
if os.path.exists(cookie_path):
with open(cookie_path, 'r', encoding='utf-8') as f:
listCookies = json.loads(f.read().strip())
expiry = listCookies[0]['expiry']
localTime = time_shift(expiry)
if localTime - time.time() > 300:
print('token未过0点,未重置')
else:
self.login()
self.save_cookies()
else:
self.login()
self.save_cookies()
"""实现免密登陆 通过cookie进入任意界面,避免重复登陆进入界面操作"""
def password_free_login(self, url="https://xxx/job/myJobs"):
self.get_cookie()
self.open(url)
self.load_cookies(cookie_path)
self.open(url)
用例
class TestOrderGrabbingKanBan(LoginPage, orderGrabbingKanBan):
def setUp(self):
super().setUp()
def tearDown(self):
super().tearDown()
@allure.title('抢单')
@pytest.mark.flaky(reruns=3, reruns_delay=3)
@pytest.mark.run(order=1)
def test_snatch_orders(self):
self.password_free_login('https://xxx/grab-manage/grabOrder')
self.inputCondition(jobKeyWord='自动化测试1666167858', n=1)
self.search()
self.rushForOrderImmediately()
self.assert_text('暂无数据', '.ant-table-tbody>tr:nth-child(2)>td>div>div>div:nth-child(2)')
pageobject层
"""抢单看板"""
import time
from seleniumbase import BaseCase
class orderGrabbingKanBan(BaseCase):
"""筛选栏
n:7 condition:职位名称/ID
n:2 condition:品牌名称
"""
def inputCondition(self, jobKeyWord='', n=1):
self.type(f'form.ant-form>div:nth-child(7)>div:nth-child({n})>div>div>div>div>span>input', jobKeyWord)
"""选择行业"""
def select_industry(self, text='互联网+'):
self.click('form.ant-form>div:nth-child(7)>div:nth-child(3)>div>div>div>div>div>div>div>div:nth-child(2)>svg')
self.click('div.antd-fd-industry-header>div:nth-child(2)>span')
self.type('div.antd-fd-industry-header>div:nth-child(2)>span>input', text)
self.hover_and_click('div.antd-fd-industry-header>div:nth-child(2)>div>div>ul>li:nth-child(1)>span',
'div.antd-fd-industry-header>div:nth-child(2)>div>div>ul>li:nth-child(1)>span')
self.click('div.antd-fd-industry-data-result>div:nth-child(2)>button>span')
"""点击搜索"""
def search(self):
self.click(".ant-form-item-control-input>div>button.ant-btn.ant-btn-primary")
"""重置"""
def reset(self):
self.click(".ant-form-item-control-input>div>button.ant-btn.ant-btn-default")
"""立即抢单"""
def rushForOrderImmediately(self, m=2):
"""m>=2 2代表点击第一条进行抢单"""
self.js_click(f".ant-table-tbody>tr:nth-child({m})>td:nth-child(12)>button")
self.click('div.ant-modal-content>div:nth-child(4)>button:nth-child(2)>span')