企业微信实战(一)作业帖

  • 分别使用浏览器复用和cookie登录企业微信。
  • 将所有的死等都封装成显式等待
  • 将源代码回复到这个帖子里
  • 录播课要看完PageObject

https://github.com/Xiaodan666/lagou_test.git![image|800x217](upload://7akefeLgXFr5hbreMs2AEHRXCWt.png)

张涛企业微信01作业:https://github.com/tzhang-web/pyzt1/tree/master/qywx0102

马杰雄实战01作业:https://github.com/majiexiong1997/study_test/tree/master/selenium_demo

https://github.com/jb5881/TestDevlopProject/tree/master/homework0616/test_selenium

李国彬-企业微信实战作业1


https://github.com/liguob/pytest.git

企业微信实践一作业_张志格
https://github.com/github-zhi/demo_20200606/tree/master/test_selenium0617

企业微信实战(一)作业_叶婷
https://github.com/Sunshine-Mia/PythonCode/tree/master/test_selenium

企业微信实战(一)作业_陈智斌
https://github.com/archerckk/PyTest/tree/master/Hogworts/selenium_test

https://github.com/CandiceDiao/lagouhomework/tree/master/webtest

https://github.com/tanya931117/TestDev

https://github.com/anny2020/hogwarts.git

李新新- 企业微信实战(一)作业

https://github.com/lixinxin2019/EnterpriseWechat20200621

企业微信实战一作业-林遵明
https://github.com/jimmylinz/LagouProject.git

https://github.com/ljwaaa/lagou2qiproject.git

企业微信实战一:pythoncode_one

https://github.com/Staupo/Lagou2QiProject.git

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地址

安洋-企业微信实战

作业1(https://github.com/wzz2019/mystudynote/tree/master/homework/seleniumwork1)

企业微信实战一作业 董淑琳
https://github.com/dongshulin222/LagouPython/tree/master/web/web_work/test_debug_work

https://github.com/wu757/hogwartsStudy/tree/master/企业微信实战一