Web自动化测试之十三、web自动化实战一:Page Object模式在真实项目中的演练

 企业微信web端自动化测试实战(一)

1.课程贴

https://ceshiren.com/t/topic/2795/2

2.课堂演示源码链接

https://github.com/ceshiren/HogwartsLG2

3.selenium官方网站

https://www.selenium.dev/

4.浏览器调试(复用)模式

调用之前

  1. 需要退出当前所有的谷歌浏览器(特别注意)
  2. 找到chrome的启动路径

windows:

 

 

 

 

 

 mac
/Applications/Google Chrome.app/Contents/MacOS/Google Chrome

3.配置环境变量:

image

4.启动命令windows:chrome --remote-debugging-port=9222
启动命令mac:Google\ Chrome --remote-debugging-port=9222

5.访问http://localhost:9222/

 

5.cookie登录

 6.代码

base_page

class BasePage:
    # 初始化Url为空
    _base_url = ""

    def __init__(self, base_driver: WebDriver = None):
        # 判断webdriver是否实例化,使用debug的方式登录
        # 若未实例化,则进行实例化
        if base_driver is None:
            option = Options()
            option.debugger_address = "localhost:9222"
            self._driver = webdriver.Chrome(options=option)

        # 否则复用driver
        else:
            self._driver = base_driver

        # 增加隐式等待
        self._driver.implicitly_wait(5)
        # 判断请求的url是否为空
        if self._base_url != "":
            self._driver.get(self._base_url)

    # 封装了查找单个元素的方法,并加入了显示等待
    def _find(self, *by):

        return WebDriverWait(self._driver, 10).until(expected_conditions.presence_of_element_located(*by))

    # 封装了查找多个元素的方法,并加入了显示等待
    def _finds(self, *by):
        return WebDriverWait(self._driver, 10).until(expected_conditions.presence_of_element_located(*by))

    # 资源回收,关闭浏览器
    def quit(self):
        self._driver.quit()
base_page
from selenium.webdriver.common.by import By

from web.page.add_member_page import AddMember
from web.page.base_page import BasePage
from web.page.connect_page import ConnectPage


class MainPage(BasePage):
    # 初始化url地址
    _base_url = "https://work.weixin.qq.com/wework_admin/frame#index"

    # 主页点击【导入通讯录】按钮,跳转到通讯录-->批量导入通讯录页面
    def goto_import_connect(self):
        self._find(By.CSS_SELECTOR, 'index_service_cnt_itemWrap:nth-child(1)').click()
        return AddMember(self._driver)

    # 主页点击【通讯录】tab页,跳转到通讯录页面
    def goto_connect(self):
        self._driver.find_element(By.ID, 'menu_contacts').click()
        return ConnectPage(self._driver)

    # 主页点击【添加成员】按钮,跳转到通讯录-->添加成员页面
    # def goto_addmember(self):
    #     self._find(By.CSS_SELECTOR, '.index_service>div:nth-child(2)>a:nth-child(1)').click()
    #     return Connect(self._driver)
main_page
from time import sleep

from selenium.webdriver.common.by import By

from web.page.add_member_page import AddMember
from web.page.base_page import BasePage


class ConnectPage(BasePage):
    # 添加成员
    def add_member(self):
        sleep(3)
        self._find(By.CSS_SELECTOR, '.js_add_member').click()
        return AddMember(self._driver)

    # 删除成员
    # 查找要删除的成员
    def get_delete_member(self):
        self._find(By.CSS_SELECTOR, '#member_list tr:nth-child(1) td:nth-child(1)').click()
        self._find(By.CSS_SELECTOR, '.js_has_member>div:nth-child(1)>a:nth-child(5)').click()
        self._find(By.CSS_SELECTOR, 'a[class="qui_btn ww_btn ww_btn_Blue').click()
        delete_alter = self._find(By.CSS_SELECTOR, '#js_tips')
        return delete_alter

    # 查找删除的成员
    # 因为成员的电话或者邮箱是必填且唯一的,所以可以用来定位成员,且可以用来断言


    # def get_mail_and_tel(self):
    #     tel = self._driver.find_element_by_css_selector(
    #         "#member_list>tr:nth-child(1)>td:nth-child(5)>span").get_property("innerText")
    #     mail = self._driver.find_element_by_css_selector(
    #         "#member_list>tr:nth-child(1)>td:nth-child(6)>span").get_property("innerText")
    #
    #     # 如果电话不为空,使用电话原来定位成员和断言
    #     if tel is not None:
    #         elememt = self._find(By.CSS_SELECTOR, '#member_list>tr:nth-child(1)>td:nth-child(5)')
    #         # 如果邮箱不为空,使用邮箱原来定位成员和断言
    #     elif mail is not None:
    #         pass
    #         # 如果电话和邮箱都为空时,异常提示
    #     else:
    #         raise Exception('请输入存在的电话号码或邮箱')
connect_page
from selenium.webdriver.common.by import By

from web.page.base_page import BasePage
from web.page.connect_page import ConnectPage


class AddMember(BasePage):
    # 创建添加人员的方法
    def add_member(self):
  
        self._find(By.ID, 'username').send_keys('笑笑')
        self._find(By.ID, 'memberAdd_acctid').send_keys('134321')
        self._find(By.ID, 'memberAdd_phone').send_keys('18321221234')

        self._find(By.CSS_SELECTOR, 'js_btn_save').click()
        return ConnectPage(self._driver)
add_member_page

 

posted @ 2020-09-18 15:49  到我碗里来吧  阅读(197)  评论(0编辑  收藏  举报