zoukankan      html  css  js  c++  java
  • selenium实现绕过登录

    痛点:在做web自动化的时候,每一个测试类都要去进行登录,这实在是太耗费性能,想着在整个测试周期内,只用登录一次就好了,完美

    解决方案:我们一般是用的是用cookie来控制登录状态,然而在selenium中有针对cookie的方法,下面做下简单实现

    思路:

    1、做登录操作

    2、获取目前的cookies,将其存储在一个文件中

    3、删除目前所有的cookie

    4、在每一个测试类的类前置去拿到cookie并将其添加到driver中

    下面是实现代码(以课堂派来做个列子)

     1 import json
     2 from time import sleep
     3 from selenium import webdriver
     4 import pytest
     5 
     6 
     7 
     8 class TestLogin:
     9     def test_login(self):
    10 
    11         dir = webdriver.Chrome()
    12         dir.get("https://v4.ketangpai.com/")
    13         dir.maximize_window()
    14         sleep(2)
    15         dir.find_element_by_xpath('//a[text()="登录"]').click()
    16         sleep(1)
    17         dir.find_element_by_xpath('//input[contains(@placeholder,"手机号")]').send_keys('XXXXXX@163.com')
    18         dir.find_element_by_xpath('//input[contains(@placeholder,"密码")]').send_keys("XXXXXXX")
    19         sleep(1)
    20 
    21         dir.find_element_by_xpath('//a[contains(@class,"btn-btn")]').click()
    22         cookie = dir.get_cookies()   # 获取目前多有的cookies
    23 
    24         with open("cookie.txt","w+") as f:
    25             f.write(json.dumps(cookie))    # 将目前所有的cookies存储到文件中
    26         dir.delete_all_cookies()    # 删除目前内存中所有的cookies
    27 
    28 
    29 
    30 class TestA:
    31     def setup_class(self):
    32         self.driver = webdriver.Chrome()
    33         self.url = "https://v4.ketangpai.com/"
    34         self.driver.get(self.url)   # 这里必须调用下域名,让selenium知道你是想添加哪一个域名下的cookies
    35 
    36         with open("./cookie.txt") as f:
    37             data = json.loads(f.read())
    38             self.driver.delete_all_cookies()
    39             for i in data:
    40                 self.driver.add_cookie(i)     # 将文件中的cookie取出来添加到driver中
    41 
    42     def teardown_class(self):
    43         self.driver.close()
    44 
    45 
    46     def test_1(self):
    47         self.driver.get("https://v4.ketangpai.com/Resource/index.html")
    48         sleep(2)
    49         assert self.driver.find_element_by_xpath('//a[text()="个人备课"]').text == '个人备课'
    50 
    51 
    52 
    53 class TestB:
    54     def setup_class(self):
    55         self.driver = webdriver.Chrome()
    56         self.url = "https://v4.ketangpai.com/"
    57         self.driver.get(self.url)    # 这里必须调用下域名,让selenium知道你是想添加哪一个域名下的cookies
    58 
    59         with open("./cookie.txt") as f:
    60             data = json.loads(f.read())
    61             self.driver.delete_all_cookies()
    62             for i in data:
    63                 self.driver.add_cookie(i)    # 将文件中的cookie取出来添加到driver中
    64 
    65     def teardown_class(self):
    66         self.driver.close()
    67 
    68     def test_2(self):
    69         self.driver.get("https://v4.ketangpai.com/Mooc/Mooc/index.html")
    70         sleep(2)
    71         assert self.driver.find_element_by_xpath('//li[text()="我的在线课程"]').text == "我的在线课程"
    72 
    73 
    74 if __name__ == '__main__':
    75     pytest.main()

    从上面的代码执行结果我们可以发现,在做TestA和TestB的时候是没有做登录操作的,也可以实现页面的直接跳转进行测试

  • 相关阅读:
    JS---案例:tab切换效果
    .net core 使用MD5加密解密字符串
    c#实战开发:用.net core开发一个简单的Web以太坊钱包 (六)
    c#实战开发:以太坊Geth 命令发布智能合约 (五)
    c#实战开发:以太坊Geth 常用命令 (四)
    c#实战开发:以太坊钱包快速同步区块和钱包卡死解决方案 (三)
    c#实战开发:以太坊钱包对接私链 (二)
    c# API接受图片文件以文件格式上传图片
    c# API接受图片文件以Base64格式上传图片
    命令查看当前电脑安装所有版本.NET Core SKD
  • 原文地址:https://www.cnblogs.com/Pycainiao/p/14714636.html
Copyright © 2011-2022 走看看