tzhang
(tzhang)
3
zhige_1
(张志格)
7
Candice
(Candice)
10
jimmy2
(林遵明)
14
qingyi
16
Mokwing
(Mokwing)
18
1、base_page.py
# -*- encoding: UTF-8 -*-
"""
@File : base_page.py
@Time : 2020-6-15 15:41
@Author : Mokwing
@Email : 1010326277@qq.com
@Software: PyCharm
@Message :
"""
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class BasePage:
_base_url = ""
# 增加模式module,rb 代表浏览器复用,默认为空,使用基本模式
def __init__(self, driver: webdriver = None, mod=""):
self._driver = ""
if mod == "rb":
options = Options()
options.debugger_address = "localhost:9663"
self._driver = webdriver.Chrome(options=options)
else:
if driver is None:
self._driver = webdriver.Chrome()
else:
self._driver = driver
# url不为空才进行访问
if self._base_url != "":
self._driver.get(self._base_url)
self._driver.maximize_window()
self._driver.implicitly_wait(3)
# 封装显示等待
def _according_to_wait(self, time, locator):
while True:
# 刷新当前页面
self._driver.refresh()
# 查看是否存在返回值
res = WebDriverWait(self._driver, time).until(EC.presence_of_element_located(locator))
# 返回值为空,返回 False
if res is None:
return False
# 否则返回 True
return True
# 获取cookies
def _get_cookies(self):
return self._driver.get_cookies()
# 向浏览器输入cookie
def _add_cookie(self, cookie):
self._driver.add_cookie(cookie)
# 退出浏览器
def _quit_browser(self):
self._driver.quit()
2、login_page.py
# -*- encoding: UTF-8 -*-
"""
@File : login_page.py
@Time : 2020-6-15 16:00
@Author : Mokwing
@Email : 1010326277@qq.com
@Software: PyCharm
@Message :
"""
from time import sleep
from selenium.webdriver.common.by import By
from hogwarts.qywechat.page.base_page import BasePage
from hogwarts.qywechat.page.register_page import Register
class Login(BasePage):
_register_loc = ".login_registerBar_link" # 登录页面中企业注册按钮
_logout_loc = "logout" # 退出字样
_first_page_loc = ".frame_nav_item_title" # 断言页面 首页 字样
def go_to_register(self):
self._driver.find_element(By.CSS_SELECTOR, self._register_loc).click()
return Register(self._driver)
# 浏览器复用
def scan_code_login_of_browser_reuse(self):
# 进行显示等待
b = self._according_to_wait(10, (By.ID, self._logout_loc))
if b is False:
print("等待扫码!")
# 等待扫码
sleep(15)
# 断言登录成功
return self._driver.find_element(By.CSS_SELECTOR, self._first_page_loc).text
3、浏览器复用
# -*- encoding: UTF-8 -*-
"""
@File : test_qywechat_one.py
@Time : 2020-6-22 11:20
@Author : Mokwing
@Email : 1010326277@qq.com
@Software: PyCharm
@Message : 企业微信实战(一)
"""
# 浏览器复用
from hogwarts.qywechat.page.home_page import HomePage
class TestQyWechatByBrowser:
def setup(self):
self.home = HomePage(mod="rb")
def teardown(self):
pass
# 点击退出
# self.driver.find_element(By.ID, "logout").click()
# self.driver.quit()
def test_login_wechat(self):
first_page_text = self.home.go_to_login().scan_code_login_of_browser_reuse()
assert "首页" == first_page_text
4、cookie使用
# -*- encoding: UTF-8 -*-
"""
@File : test_qywechat_two.py
@Time : 2020-6-22 16:32
@Author : Mokwing
@Email : 1010326277@qq.com
@Software: PyCharm
@Message : 使用cookie进行登录
"""
import json
import time
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from hogwarts.qywechat.page.home_page import HomePage
class TestLoginByCookie:
def setup(self):
self.home = HomePage()
def teardown(self):
self.home._quit_browser()
def test_get_cookie(self):
self.home.go_to_login().scan_code_login_of_browser_reuse()
cookies = self.home._get_cookies()
with open("qywechat_cookie.json", "w") as f:
json.dump(cookies, f)
def test_login(self):
login_page = self.home.go_to_login()
with open("qywechat_cookie.json", "r") as f:
self.cookies = json.load(f)
for cookie in self.cookies:
self.home._add_cookie(cookie)
# print(cookie)
first_page_text = login_page.scan_code_login_of_browser_reuse()
assert "首页" == first_page_text
5、github地址
安洋-企业微信实战