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

企业微信实战一: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/企业微信实战一

企业微信实战一,罗海龙
https://github.com/lhlgxyf33/Lagou2QiHomework.git

王明哲企业微信实战一
https://github.com/WMZ5123/webdriver/tree/master/test_selenium

刘伟-企业微信01-作业
https://github.com/huanxingshi01/python04.git

企业微信01-作业-钱语歌
https://github.com/echoqyg/lagouproject/tree/master/test_selenium

https://github.com/yanbin0424/test_cookie

王小刚作业:
https://github.com/yuguo100tianqing/lagou2qiprj/commit/fc3562abb25b2f067fd75fff526e591ebf6c3340

张立雨087646作业
https://github.com/en-one/cekai_pythoncode/tree/master/web%20企业微信实战一

刘羽 企业微信实战一
https://github.com/elsa-liu/lagou2QProject/tree/master/pythoncode/work_weixin_homework/workweixin_1

https://github.com/haiqing999/test_selenium01.git

https://github.com/zhangwanli-marker/Lagouzuoye/tree/master/web/Testcase_wx

贾潇冰-作业

作业一,test_cookie.py
https://github.com/Cat-Zhou/pythonTest/blob/master/selenium_test/test_cookie.py
和test_login_debug.py
https://github.com/Cat-Zhou/pythonTest/blob/master/selenium_test/test_login_debug.py

企业微信实战一
https://github.com/sidneyharris/untitled

窦晖_web自动化作业1
https://github.com/latesir/lagouLessonPython/tree/master/autoTest/work_webAutoTest

import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class TestWechatLogin():
    def test_debug_login(self):
        self.option = Options()
        self.option.debugger_address = "localhost:9222"
        self.driver = webdriver.Chrome(options=self.option)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        WebDriverWait(self.driver, 5).until(expected_conditions.element_to_be_clickable((By.ID, 'menu_index')))

    def test_get_cookies(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        WebDriverWait(self.driver, 15).until(expected_conditions.element_to_be_clickable((By.ID, 'menu_index')))
        with open('wxCookies.json', 'w') as f:
            json.dump(self.driver.get_cookies(), f)
        self.driver.quit()

    def test_cookie_login(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        with open('wxCookies.json', 'r') as f:
            cookieList = json.load(f)
            for cookie in cookieList:
                self.driver.add_cookie(cookie)
        while True:
            self.driver.refresh()
            if WebDriverWait(self.driver, 5).until(expected_conditions.element_to_be_clickable((By.ID, 'menu_index'))):
                break
        self.driver.quit()

浏览器复用:
https://github.com/xiaotang0531/xiaotanghomework/blob/master/test_selenium/test_copybrowser.py

使用cookie登录:
https://github.com/xiaotang0531/xiaotanghomework/blob/master/test_selenium/test_cookies.py