问题:在做UI自动化测试时,打开网站会遇到需要登录的问题,有没有什么方法能获取cookie存起来, 在执行用例之前把cookie种到浏览器中呢?
解决思路:
1.登录网站,用get_cookies()方法获取cookie
2.将cookie写入yaml文件里
3.读yaml文件的cookie
4.用add_cookie(cookie)植入cookie
以企业微信管理后台扫码登录为例~
第一步:打开企业微信首页
第二步:执行test_get_cookie方法,扫码登录后,获取cookie,将cookie存入yaml文件中
第三步:后续如果退出了浏览器,执行test_add_cookie方法,就可以读yaml文件中cookie,种cookie
from time import sleep
import yaml
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
class TestCookie:
"""
种cookie
"""
def setup_class(self):
self.driver = webdriver.Chrome()
# 进入企业微信首页
self.driver.get('https://work.weixin.qq.com/wework_admin/frame')
# 等待20s,人工扫码操作的时间
sleep(20)
def teardown_class(self):
self.driver.quit()
def test_get_cookie(self):
"""
获取cookie
:return:
"""
# 登录成功,拿到cookie
cookie = self.driver.get_cookies()
# print(cookie)
# 将cookie写入yaml文件
with open('cookie.yml', 'w') as f:
yaml.safe_dump(cookie, f)
def test_add_cookie(self):
"""
种cookie
:return:
"""
# 定义cookie
# cookie = [{'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.logined', 'path': '/', 'secure': False, 'value': 'true'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.cs_ind', 'path': '/', 'secure': False, 'value': ''}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.d2st', 'path': '/', 'secure': False, 'value': 'a6561410'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.vst', 'path': '/', 'secure': False, 'value': '5dawZ7HPqfTIi4CJ8VKngYWLBGEBzk8ni_Ui0MV4hQAYLa6VCgMEjuFU6FNdPZlVCAX8KpSjl_FuQv5trnXfLdynQFSj2oIbBEmvi2HX0HoilikUQb9mqQCRy8TPrt5x8xsT5f4OJ-rZfsgDExhbDkjdDzpvSRI_hI8YWf59yMi3Pn26GFDOVuS2qWYLbwPhsnGwfYCNzUISY2SOAN4ZKiejnSSOlULaV2O_HLRQx3TWB-6CvM9A8IKEHpdUrE1Rda0nqvjZc_uOydc4KcQIow'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.vid', 'path': '/', 'secure': False, 'value': '1688855235343641'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wxpay.corpid', 'path': '/', 'secure': False, 'value': '1970325970978757'}, {'domain': '.work.weixin.qq.com', 'httpOnly': False, 'name': 'wwrtx.vid', 'path': '/', 'secure': False, 'value': '1688855235343641'}, {'domain': '.qq.com', 'expiry': 2147385600, 'httpOnly': False, 'name': 'pgv_pvid', 'path': '/', 'secure': False, 'value': '9942284417'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ref', 'path': '/', 'secure': False, 'value': 'direct'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.ltype', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.refid', 'path': '/', 'secure': False, 'value': '16669699573425234'}, {'domain': '.work.weixin.qq.com', 'httpOnly': True, 'name': 'wwrtx.sid', 'path': '/', 'secure': False, 'value': 'K9yhXG92RVAHVa1tnUYnv2WY-8SitSp-a-xaxPaOSaHVg2dqsYy4lHh3te_F30MR'}, {'domain': '.qq.com', 'httpOnly': False, 'name': 'pgv_info', 'path': '/', 'secure': False, 'value': 'ssid=s6863689265'}, {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'ptcz', 'path': '/', 'secure': False, 'value': '03abb3d3e45ca3572ebae8c9758a950ccc2afcce941880ddafbedc73c7bbe116'}, {'domain': '.qq.com', 'expiry': 2147483647, 'httpOnly': False, 'name': 'RK', 'path': '/', 'secure': False, 'value': 'rm98LZMEXT'}, {'domain': '.work.weixin.qq.com', 'expiry': 1653808734, 'httpOnly': False, 'name': 'wwrtx.i18n_lan', 'path': '/', 'secure': False, 'value': 'zh'}, {'domain': '.work.weixin.qq.com', 'expiry': 1681293715, 'httpOnly': False, 'name': 'wwrtx.c_gdpr', 'path': '/', 'secure': False, 'value': '0'}]
# 获取cookie
cookie = yaml.safe_load(open('./cookie.yml'))
# 种cookie
for c in cookie:
self.driver.add_cookie(c)
sleep(2)