20220731-web自动化实战2

PPT

https://pdf.ceshiren.com/book/web_auto_testing/ppt/[训练营]知名产品web自动化测试实战2.html

作业讲解

:clap:送给同学们!

作业题目:

  1. 通讯录页面:添加成员
  2. 通讯录页面:添加部门

PO设计模式

  • 概念
  • 原则

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
from selenium import webdriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    """
    与浏览器driver交互
    """

    # 构造方法
    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            # 实例化driver
            self.driver = webdriver.Chrome()
            self.driver.implicitly_wait(5)
            self.driver.maximize_window()

    def do_find(self, by):
        """获取单个元素"""
        return self.driver.find_element(*by)

    def do_finds(self, by):
        """获取多个元素"""
        return self.driver.find_elements(*by)

    def do_send_keys(self, value, by):
        """输入文本"""
        ele = self.do_find(by)
        ele.clear()
        ele.send_keys(value)

    def do_quit(self):
        """退出浏览器"""
        self.driver.quit()

    def wait_visible(self, locator):
        """显示等待元素可见"""
        return WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(locator))
"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
import yaml

from page.base_page import BasePage


class LoginPage(BasePage):
    """登录页"""

    def login(self):
        """扫码登录"""
        # 访问企业微信首页
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 获取本地的cookie
        with open("../cookies.yaml") as f:
            cookies = yaml.safe_load(f)
        # 植入cookie
        for ck in cookies:
            self.driver.add_cookie(ck)
        # 访问企业微信首页
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        from page.home_page import HomePage
        return HomePage(self.driver)
"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
from selenium.webdriver.common.by import By

from page.base_page import BasePage


class HomePage(BasePage):
    """首页"""

    __BTN_ADD = (By.LINK_TEXT, "添加成员")
    __BTN_CONTACT = (By.ID, "menu_contacts")

    def click_add_member(self):
        """点击添加成员"""
        self.do_find(self.__BTN_ADD).click()
        from page.contact_page import ContactPage
        return ContactPage(self.driver)

    def click_contact(self):
        """点击通讯录"""
        self.do_find(self.__BTN_CONTACT).click()
        from page.contact_page import ContactPage
        return ContactPage(self.driver)

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
import time

from selenium.webdriver.common.by import By

from page.base_page import BasePage


class ContactPage(BasePage):
    """通讯录界面"""

    # 添加成员属性
    __INPUT_USERNAME = (By.ID, "username")
    __INPUT_ACCTID = (By.ID, "memberAdd_acctid")
    __INPUT_MOBILE = (By.ID, "memberAdd_phone")
    __BTN_SAVE = (By.CSS_SELECTOR, "a.qui_btn.ww_btn.js_btn_save")
    __TEXT_MEMBER_INFO = (By.CSS_SELECTOR, "#member_list>tr>td:nth-child(2)>span")

    # 添加部门属性
    __BTN_ADD = (By.XPATH, "//i[@class='member_colLeft_top_addBtn']")
    __BTN_ADD_PARTY = (By.XPATH, "//a[text()='添加部门']")
    __INPUT_PARTY_NAME = (By.XPATH, "//input[@name='name']")
    __BTN_PARENT_PARTY_NAME = (By.XPATH, "//span[@class='js_parent_party_name']")
    __BTN_PARTY_NAME = (By.XPATH, "//div[@class='inputDlg_item']//a[text()='印度游戏有限公司']")
    __BTN_PARTY_NAME_2 = (By.XPATH, "//div[@class='inputDlg_item']//a[text()='梁利']")
    __BTN_PARTY_NAME_3 = (By.XPATH, "//a[text()='梁利']/../ul/li/a")
    __BTN_SUBMIT = (By.XPATH, "//a[text()='确定']")
    __TEXT_PARTY_INFO = (By.CSS_SELECTOR, "ul[role='group'] ul[role='group']>li>a")

    # 弹条属性
    __TEXT_TIPS = (By.ID, "js_tips")

    def write_member_info(self, username, acctid, mobile):
        """填写成员信息"""
        # 输入用户名
        self.do_send_keys(username, self.__INPUT_USERNAME)
        # 输入acctid
        self.do_send_keys(acctid, self.__INPUT_ACCTID)
        # 输入手机号
        self.do_send_keys(mobile, self.__INPUT_MOBILE)
        # 点击保存
        self.do_finds(self.__BTN_SAVE)[0].click()
        return self

    def write_party_info(self, name):
        """填写部门信息"""
        # 点击+号
        self.do_find(self.__BTN_ADD).click()
        # 点击添加部门
        self.do_find(self.__BTN_ADD_PARTY).click()
        # 填写部门名称
        self.do_send_keys(name, self.__INPUT_PARTY_NAME)
        # 选择所属部门
        self.do_find(self.__BTN_PARENT_PARTY_NAME).click()
        self.do_find(self.__BTN_PARTY_NAME).click()
        # 点击确定
        self.do_find(self.__BTN_SUBMIT).click()
        return self

    def write_party_2_info(self, name, party_name):
        party_name_lo = (By.XPATH, f"//div[@class='inputDlg_item']//a[text()='{party_name}']")
        """填写部门信息"""
        # 点击+号
        self.do_find(self.__BTN_ADD).click()
        # 点击添加部门
        self.do_find(self.__BTN_ADD_PARTY).click()
        # 填写部门名称
        self.do_send_keys(name, self.__INPUT_PARTY_NAME)
        # 选择所属部门
        self.do_find(self.__BTN_PARENT_PARTY_NAME).click()
        self.do_find(party_name_lo).click()
        # 点击确定
        self.do_find(self.__BTN_SUBMIT).click()
        return self

    def get_tips(self):
        """获取tips"""
        self.wait_visible(self.__TEXT_TIPS)
        value = self.do_find(self.__TEXT_TIPS).text
        return value

    def get_members(self):
        """获取成员列表的所有姓名"""
        list_member_info = self.do_finds(self.__TEXT_MEMBER_INFO)
        list_members = []
        for i in list_member_info:
            list_members.append(i.text)
        return list_members

    def get_partys(self):
        """获取部门列表的所有名称"""
        time.sleep(2)
        list_party_info = self.do_finds(self.__TEXT_PARTY_INFO)
        list_parts = []
        for i in list_party_info:
            list_parts.append(i.text)
        return list_parts

    def get_party_2(self, party_name):
        """获取二级部门的下一级部门名称"""
        party_name_lo = (By.XPATH, f"//a[text()='{party_name}']/../ul/li/a")
        time.sleep(2)
        list_party_info = self.do_finds(party_name_lo)
        list_parts = []
        for i in list_party_info:
            list_parts.append(i.text)
        return list_parts

"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨,请访问:https://ceshiren.com/t/topic/15860'
"""
import pytest
from faker import Faker

from page.login_page import LoginPage


class TestContact:
    def setup_class(self):
        faker1 = Faker("zh_Cn")
        self.username = faker1.name()
        self.acctid = faker1.ssn()
        self.mobile = faker1.phone_number()
        # 打开登录页
        self.login_page = LoginPage()

    def teardown_class(self):
        self.login_page.do_quit()

    def test_add_member(self):
        """添加成员用例"""
        contact_page = self.login_page.login().click_add_member().write_member_info(self.username, self.acctid,
                                                                                    self.mobile)
        value = contact_page.get_tips()
        list_members = contact_page.get_members()
        assert "保存成功" == value
        assert self.username in list_members

    def test_add_party(self):
        """添加部门用例"""
        contact_page = self.login_page.login().click_contact().write_party_info(self.username)
        value = contact_page.get_tips()
        list_partys = contact_page.get_partys()
        assert "新建部门成功" == value
        assert self.username in list_partys

    @pytest.mark.parametrize("party_name", ["梁利", "陈梅"])
    def test_add_party2(self, party_name):
        """添加二级部门用例"""
        contact_page = self.login_page.login().click_contact().write_party_2_info(self.username, party_name)
        value = contact_page.get_tips()
        list_partys_2 = contact_page.get_party_2(party_name)
        assert "新建部门成功" == value
        assert self.username in list_partys_2


课堂实战

  • 目标:首页的添加成员场景的线性代码
  • 要求:进行 PO 设计模式的改造
    • 使用类去封装每个页面的元素和操作细节
    • 创建 BasePage 提供 driver 相关方法
    • 用例中链式调用和断言

实战讲解

image

课后作业

  • 使用 PO 设计模式改造作业代码
    • 通讯录页面:添加成员
    • 通讯录页面:添加部门