20221030 web自动化测试实战一 (Python)

实战目标

  • 企业微信的用户管理

课堂ppt

PPT地址

源码地址(思维导图在源码内)

内容

课件练习

  • 构造PO模型
    • 通讯录页面
    • 首页
    • 添加成员页面
  • 构造测试用例
    • 从首页添加成员
    • 从通讯录页面添加成员
  • 注意:实现先设置为空

页面1

页面2

用例1

test_login.py

import yaml
from selenium import webdriver
from time import  sleep

"""获取cookie"""
# driver = webdriver.Chrome()
# driver.get("https://work.weixin.qq.com/")
# sleep(20)
# driver.refresh()
# login_cookies = driver.get_cookies()
# print(login_cookies)
# driver.quit()

class TestLogin:
    def setup_class(self):
        self.driver = webdriver.Chrome()

    def teardown_class(self):
        self.driver.quit()

    def test_get_cookies(self):
        self.driver.get("https://work.weixin.qq.com/")
        sleep(30)
        cookies = self.driver.get_cookies()
        with open("../datas/cookies_data.yaml", mode="w") as f:
            yaml.safe_dump(cookies, f)

    def test_add_cookies(self):
        self.driver.get("https://work.weixin.qq.com/")
        with open("../datas/cookies_data.yaml") as f:
            cookies = yaml.safe_load(f)

        for cookie in cookies:
            self.driver.add_cookie(cookie)

        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        self.driver.refresh()
        sleep(30)

test_add_member.py

import logging

import yaml
from selenium import webdriver
from time import  sleep

from selenium.common import StaleElementReferenceException
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class TestLogin:
    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.get("https://work.weixin.qq.com/")
        with open("../datas/cookies_data.yaml") as f:
            cookies = yaml.safe_load(f)

        for cookie in cookies:
            self.driver.add_cookie(cookie)

        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        self.driver.implicitly_wait(5)

    def teardown_class(self):
        self.driver.quit()

    def test_add_member(self):
        self.driver.find_element(By.ID, "menu_contacts").click()

        wait = WebDriverWait(self.driver, 10, ignored_exceptions=[StaleElementReferenceException])
        ele = wait.until(expected_conditions.element_to_be_clickable(
            (By.CSS_SELECTOR, ".js_has_member .js_add_member")))
        ele.click()

        self.driver.find_element(By.ID, "username").send_keys("Lilei1")
        self.driver.find_element(By.ID, "memberAdd_acctid").send_keys("19901")
        self.driver.find_element(By.ID, "memberAdd_phone").send_keys("13388886666")

        self.driver.find_element(By.CSS_SELECTOR, ".js_btn_save").click()

        ele = self.driver.find_elements(By.CSS_SELECTOR, "[title='Lilei1']")
        assert ele != []