seleniumBase-token登陆

登陆

场景: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:   #未过0点
                    print('token未过0点,未重置')
                else:   #过了0点,token,重新登陆
                    self.login()
                    self.save_cookies()    #保存登陆后的cookie

        else:
            self.login()
            self.save_cookies()
	"""实现免密登陆  通过cookie进入任意界面,避免重复登陆进入界面操作"""
    def password_free_login(self, url="https://xxx/job/myJobs"):
        self.get_cookie()   #获取cookie
        self.open(url)
        self.load_cookies(cookie_path)    #解析cooKie并使用
        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(1)')
        # self.type(
        #     'form.ant-form>div:nth-child(7)>div:nth-child(3)>div>div>div>div>div>div>div>div:nth-child(1)>div>div>div>div>input',
        #     text)
        # self.hover_and_click(
        #     'div.ant-select-dropdown>div>div:nth-child(2)>div:nth-child(1)>div>div>div:nth-child(1)>div',
        #     'div.ant-select-dropdown>div>div:nth-child(2)>div:nth-child(1)>div>div>div:nth-child(1)>div')
        # #   第二种   通过筛选
        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-content>div:nth-child(1)>div:nth-child(2)>div:nth-child(10)>span')   #这个是直接点击
        self.click('div.antd-fd-industry-data-result>div:nth-child(2)>button>span')  # 确认按钮

    # def inputBrandName(self, brandName=''):
    #     self.type('.ant-form-item-control-input-content>span>input#brandName', brandName)  # 品牌名称

    """点击搜索"""

    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')  # 点击确定

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值