20220724-web自动化实战1

课堂ppt

霍格沃兹测试开发

知识点回顾

课堂内容

  • selenium架构

  • selenium运行机制

实战练习

  1. 植入 cookie 登录(参见【Cookie 复用】)

要求:

  • 首次登录,人工扫码,登录成功保存cookie到本地
  • 后续用例,直接读取本地cookie登录

代码形式:可以是一个测试用例,包含2个方法,(1)获取并保存cookie的测试方法;(2)读取并植入cookie的测试方法。

或者自己封装成函数亦可。

窗口冻结命令

setTimeout(function(){debugger},3000)

参考视频:
image

参考代码:

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

import yaml
from faker import Faker
from selenium import webdriver
from selenium.webdriver.common.by import By


class TestWeworkLogin:

    def setup_class(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()

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


    def test_save_cookies(self):
        """
        保存cookies
        :return:
        """
        # 1.访问企业微信的登录页面
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 2.手机扫码
        time.sleep(10)
        # 3.获取浏览器的cookies
        cookies = self.driver.get_cookies()
        print(cookies)
        # 4.保存cookies
        with open("./cookies.yaml", "w") as f:
            yaml.safe_dump(data=cookies, stream=f)


    def test_get_cookie(self):
        """
        植入cookie跳过登录
        :return:
        """
        # 1.访问企业微信首页
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 2.获取本地的cookie
        with open("./cookies.yaml", "r") as f:
            cookies = yaml.safe_load(f)
        print(cookies)
        # 3.植入cookie
        for c in cookies:
            self.driver.add_cookie(c)
        # 4.访问企业微信首页
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        time.sleep(5)

    def test_add_member(self):
        """
        添加成员
        :return:
        """
        faker1 = Faker("zh_Cn")
        usnrname = faker1.name()
        acctid = faker1.ssn()
        mobile = faker1.phone_number()
        print(usnrname, acctid, mobile)

        # 1.访问企业微信首页
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 2.获取本地的cookie
        with open("./cookies.yaml", "r") as f:
            cookies = yaml.safe_load(f)
        print(cookies)
        # 3.植入cookie
        for c in cookies:
            self.driver.add_cookie(c)
        # 4.访问企业微信首页
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")

        # 5.点击添加成员按钮
        self.driver.find_element(By.LINK_TEXT, "添加成员").click()
        # 6.输入用户名
        self.driver.find_element(By.ID, "username").send_keys(usnrname)
        # 7.输入acctid
        self.driver.find_element(By.ID, "memberAdd_acctid").send_keys(acctid)
        # 8.输入手机号
        self.driver.find_element(By.ID, "memberAdd_phone").send_keys(mobile)
        # 9.点击保存
        self.driver.find_elements(By.CSS_SELECTOR, "a.qui_btn.ww_btn.js_btn_save")[0].click()
        time.sleep(2)
        # 10.断言结果
        tips_text = self.driver.find_element(By.ID, "js_tips").text
        assert "保存成功" == tips_text

  1. 等待揭晓

课后作业

  • 实现从通讯录页面添加成员的操作
  • 实现添加部门的操作

调查问卷

今天的课程结束了,请同学们完成课后调查表单:20220724-汤达人- web自动化实战训练营1 - 课后调查问卷
selenium.zip (51.3 KB)

python

# author: YLXB
# project:pythonProject2
# name: test_cookies.py
# date: 2022/7/24
import time

import yaml

from test_cookies.base import Base


class TestCookies(Base):
    # 保存cookies
    def test_save_cookies(self):
        # 打开企业微信网页
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 最大化网页
        self.driver.maximize_window()
        # 强制等待10秒,手机扫描登录
        time.sleep(10)
        # 获取Cookies
        cookies = self.driver.get_cookies()
        # 将获取的cookies保存为yaml文件
        with open("./cookies.yaml", mode="w", encoding="utf-8") as f:
            yaml.dump(data=cookies, stream=f)

        time.sleep(10)

    # 添加cookies
    def test_add_cookies(self):
        # 打开企业微信网页
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 最大化网页
        self.driver.maximize_window()
        # 从yaml中读取cookies,转为python语句
        cookies = yaml.safe_load(open("./cookies.yaml"))
        # 植入cookies
        for c in cookies:
            self.driver.add_cookie(c)
        # 再次打开企业微信网页
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 最大化网页
        self.driver.maximize_window()


if __name__ == '__main__':
    test_demo = TestCookies
    test_demo.test_save_cookies()
    test_demo.test_add_cookies()

import time
import yaml
from selenium import webdriver

class TestWechat:
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(3)
        self.driver.maximize_window()
    def teardown(self):
        self.driver.quit()

    def test_get_cookie(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        time.sleep(20)
        cookies = self.driver.get_cookies()
        with open('cookies.yaml','w',encoding='utf-8') as f:
            yaml.safe_dump(data=cookies,stream=f)

    def test_login(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        with open('cookies.yaml','r') as f:
            cookie = yaml.safe_load(f)
        for i in cookie:
            self.driver.add_cookie(i)
        print(cookie)
# author : writer
# project : 
# name : test_web.py
# date : 2022/7/24
import time

import pytest
import yaml
from selenium import webdriver


class TestQiWei():
    def setup(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(10)
        self.driver.maximize_window()
    def teardown(self):
        self.driver.quit()

    def test_get_cookie(self):
        # 打开企业微信,然后手动扫码登录
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        time.sleep(20)
        # 登录成功后,调用获取cookie
        cookies = self.driver.get_cookies()
        print(cookies)
        # 将cookie存入一个可持久存储的地方,文件
        # 打开文件的时候,添加写入w权限
        with open("./cookies.yaml", 'w',)as f:
            yaml.safe_dump(data=cookies, stream=f)


    def test_add_cookie(self):
        # 1、访问企业微信
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 2、定义cookie,先粘贴打印出来的cookie,cookie信息从yaml文件中获取
        with open("./cookies.yaml", 'r', ) as f:
            cookie = yaml.safe_load(f)
        print(cookie)
        # 植入cookie
        for c in cookie:
            self.driver.add_cookie(c)
        time.sleep(3)
        # 4、再次访问企业微信,无需扫码
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")


 # 添加成员,导入faker库,自动生成姓名电话等
    def test_add_member(self):
        fake1 = Faker("zh_cn")
        username = fake1.name()
        accid = fake1.ssn()
        mobile = fake1.phone_number()
        print(username, accid, mobile)

        # 1、访问企业微信
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 2、定义cookie,先粘贴打印出来的cookie,cookie信息从yaml文件中获取
        with open("./cookies.yaml", 'r') as f:
            cookie = yaml.safe_load(f)
        print(cookie)
        # 3、植入cookie
        for c in cookie:
            self.driver.add_cookie(c)

         #4、查找添加成员按钮
        self.driver.find_element(By.LINK_TEXT, '添加成员').click()
        # 5、输入姓名
        self.driver.find_element(By.ID, 'username').send_keys(username)
        # 6、输入账号acctid
        self.driver.find_element(By.ID, 'memberAdd_acctid').send_keys(accid)
        #7、输入手机号
        self.driver.find_element(By.ID, 'memberAdd_phone').send_keys(mobile)
        # 8、点击保存,保存有多个class ,用a.class ,然后选取第一个
        self.driver.find_elements(By.CSS_SELECTOR, "a.qui_btn.ww_btn.js_btn_save")[0].click()
        time.sleep(3)
        #9、获取弹窗文本,断言
        tips_text = self.driver.find_element(By.ID, 'js_tips').text
        assert "保存成功" == tips_text





if __name__ == '__main__':
    pytest.main('-v', '-s', 'test_web.py')
    def test_add_member(self):
        faker1 = Faker("zh-Cn")
        username = faker1.name()
        id = faker1.ssn()
        num = faker1.phone_number()
        # 登录
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        with open('cookies.yaml', 'r') as f:
            cookie = yaml.safe_load(f)
        for i in cookie:
            self.driver.add_cookie(i)
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")

        # 定位
        WebDriverWait(self.driver,5).until(
            expected_conditions.element_to_be_clickable((By.CLASS_NAME,"index_service_cnt_item_title"))
        ).click()
        self.driver.find_element(By.ID,"username").send_keys(username)
        self.driver.find_element(By.ID,"memberAdd_acctid").send_keys(id)
        self.driver.find_element(By.ID,"memberAdd_phone").send_keys(num)
        self.driver.find_elements(By.CSS_SELECTOR,".js_btn_save")[0].click()
       
        # 断言
            res = WebDriverWait(self.driver,10).until(
            expected_conditions.visibility_of_element_located((By.ID,"js_tips"))
        ).text
        res = self.driver.find_element(By.ID,"js_tips").text
        assert "保存成功" == res
    # 添加成员
    def test_add_member(self):
        # 打开企业微信网页
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 最大化网页
        self.driver.maximize_window()
        # 从yaml中读取cookies,转为python语句
        with open("./cookies.yaml", "r") as f:
            cookies = yaml.safe_load(open("./cookies.yaml"))
        # 植入cookies
        for c in cookies:
            self.driver.add_cookie(c)
        # 再次打开企业微信网页
        self.driver.get("https://work.weixin.qq.com/wework_admin/loginpage_wx")
        # 最大化网页
        self.driver.maximize_window()
        # 点击添加成员按钮
        self.driver.find_element(By.XPATH, "//*[text()='添加成员']").click()
        # 输入姓名
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("一路向北")
        # 输入账号
        self.driver.find_element(By.CSS_SELECTOR, "#memberAdd_acctid").send_keys("1234567890")
        # 输入手机号
        self.driver.find_element(By.CSS_SELECTOR, ".qui_inputText.ww_inputText.ww_telInput_mainNumber").send_keys("18712345678")
        # 点击保存按钮
        self.driver.find_element(By.XPATH, "//*[text()='保存']").click()
        # 获取弹窗的文本信息
        save_text = self.driver.find_element(By.ID, "js_tips").text
        # 断言,判断是否保存成功
        assert "保存成功" == save_text
# 从通讯录页面添加成员
    def test_addmember_from_addressbook(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        with open("cookies.yaml", "r") as f:
            cookies = yaml.safe_load(f)
        print(cookies)
        for c in cookies:
            self.driver.add_cookie(c)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "#menu_contacts"))
        )
        self.driver.find_element(By.CSS_SELECTOR, "#menu_contacts").click()
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "div.js_has_member>div:nth-child(1)>a:nth-child(2)"))
        )
        self.driver.find_element(By.CSS_SELECTOR, "div.js_has_member>div:nth-child(1)>a:nth-child(2)").click()
        # 调用添加成员公共方法
        Test_Web_Demo.public_method(self)

    # 添加部门
    def test_adddepartment(self):
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        with open("cookies.yaml", "r") as f:
            cookies = yaml.safe_load(f)
        print(cookies)
        for c in cookies:
            self.driver.add_cookie(c)
        self.driver.get("https://work.weixin.qq.com/wework_admin/frame")
        # 点击通讯录
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "#menu_contacts"))
        )
        self.driver.find_element(By.CSS_SELECTOR, "#menu_contacts").click()
        # 点击添加+按钮
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, ".member_colLeft_top_addBtn"))
        )
        self.driver.find_element(By.CSS_SELECTOR, ".member_colLeft_top_addBtn").click()
        # 点击添加部门按钮
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, ".js_create_party"))
        )
        self.driver.find_element(By.CSS_SELECTOR, ".js_create_party").click()
        # 部门名称输入框输入测试组01
        self.driver.find_element(By.CSS_SELECTOR, "form.form>div:nth-child(1)>input:nth-child(2)").send_keys("测试组03")
        # 点击下拉选择按钮
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "form.form>div:nth-child(3)>a:nth-child(2)>span:nth-child(2)"))
        )
        self.driver.find_element(By.CSS_SELECTOR, "form.form>div:nth-child(3)>a:nth-child(2)>span:nth-child(2)").click()
        # 点击下拉选列表中的所属部门
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(
                (By.XPATH, "//div[@class='inputDlg_item']//a[text()='selenium_test']"))
        )
        self.driver.find_element(By.XPATH, "//div[@class='inputDlg_item']//a[text()='selenium_test']").click()
        # 点击确定按钮
        WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(
                (By.CSS_SELECTOR, ".qui_btn.ww_btn.ww_btn_Blue"))
        )
        self.driver.find_element(By.CSS_SELECTOR, ".qui_btn.ww_btn.ww_btn_Blue").click()
        # self.driver.find_element(By.CSS_SELECTOR, "div.qui_dialog_foot.ww_dialog_foot>a:nth-child(1)").click()

        WebDriverWait(self.driver, 5).until(
            expected_conditions.visibility_of_element_located((By.ID, "js_tips"))
        )
        data = self.driver.find_element(By.ID, "js_tips").text
        assert "新建部门成功" == data


    def public_method(self):
        faker1 = Faker("zh_Cn")
        username = faker1.name()
        acctid = faker1.ssn()
        mobile = faker1.phone_number()
        print(username, acctid, mobile)

        self.driver.find_element(By.CSS_SELECTOR, "[id='username']").send_keys(username)
        self.driver.find_element(By.ID, "memberAdd_acctid").send_keys(acctid)
        self.driver.find_element(By.ID, "memberAdd_phone").send_keys(mobile)
        self.driver.find_element(By.XPATH, "//form[@class='js_member_editor_form']//div[last()-2]//a[last()-1]").click()
        WebDriverWait(self.driver, 5).until(
            expected_conditions.visibility_of_element_located((By.ID, "js_tips"))
        )
        data = self.driver.find_element(By.ID, "js_tips").text
        assert "保存成功" == data

image

代码可以做个优化,减少代码量。显示等待后面直接.click()就可以,不需要再去定位一下,会占用运行时间