Web自动化测试-5

本次内容:

Web自动化测试-5_第1张图片

1.V2

import json
import time
import unittest
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from parameterized import parameterized


# V2解决了V1不能批量执行的问题,但V2还有一个致命的问题,
# 业务脚本与页面对象没有分开(即[name='username']这种)
def build_data():
    with open('V2_data.json', encoding='utf-8') as f:
        result = json.load(f)
        data = []
        for i in result:
            # 这里一定要注意,要把每次添加的数据转化为元组,不然会报错
            # 报错的原因是因为parameterized只能支持每组数据是元组
            data.append((i.get('username'), i.get('password'), i.get('loginver'), i.get('expect_result')))
    return data


# unittest无法实现参数化,需要安装parameterized
class Test01(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Edge()
        cls.driver.get("https://www.tp-shop.cn/")
        cls.driver.maximize_window()
        cls.driver.implicitly_wait(10)
        cls.driver.find_element(By.PARTIAL_LINK_TEXT, "登录").click()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    @parameterized.expand(build_data())
    def test001(self, username, password, loginver, expect_result):
        driver = self.driver
        sleep(1)
        user = driver.find_element(By.CSS_SELECTOR, "[name='username']")
        user.clear()
        user.send_keys(username)
        sleep(1)
        pwd = driver.find_element(By.CSS_SELECTOR, "[name='password']")
        pwd.clear()
        pwd.send_keys(password)
        sleep(1)
        lv = driver.find_element(By.CSS_SELECTOR, "#login-ver")
        lv.clear()
        lv.send_keys(loginver)
        sleep(1)
        # 这里不知道为啥.login-btn对,input[value=''登录]错
        driver.find_element(By.CSS_SELECTOR, ".login-btn").click()
        sleep(1)
        result = driver.find_element(By.CSS_SELECTOR, ".layui-layer-content").text
        print(result)
        # expect_result = "验证码错误!!"
        try:
            self.assertEqual(result, expect_result)

            driver.find_element(By.CSS_SELECTOR,".layui-layer-btn0").click()
        except AssertionError:
            # driver.get_screenshot_as_file("../image/%s.png" % (time.strftime("%Y_%m_%d_%H_%M_%S")))
            # {}与.format()
            driver.get_screenshot_as_file("../image/{}.png".format(time.strftime("%Y_%m_%d_%H_%M_%S")))

V2_data.json 

[
  {
    "username": "13800001111",
    "password": "123456",
    "loginver": "xx",
    "expect_result" : "验证码错误"
  },
    {
    "username": "138000",
    "password": "123456",
    "loginver": "xxx",
    "expect_result" : "账号格式不匹配!"
  }
]

2.V3

Web自动化测试-5_第2张图片

2.1page

# 页面对象层
import time

from selenium import webdriver
from selenium.webdriver.common.by import By


class PageLogin:
    def __init__(self):
        self.driver = webdriver.Edge()
        self.driver.get("https://www.tp-shop.cn/")
        self.driver.maximize_window()
        self.driver.implicitly_wait(10)
        # self.pag_click_login_link()

    # 外页面的登录链接,进入登录页面
    def pag_click_login_link(self):
        self.driver.find_element(By.PARTIAL_LINK_TEXT, "登录").click()

    def login_username(self,username):
        user = self.driver.find_element(By.CSS_SELECTOR, "[name='username']")
        # 没有实例化是用不了clear方法的
        user.clear()
        return user.send_keys(username)

    def login_password(self,password):
        pwd = self.driver.find_element(By.CSS_SELECTOR, "[name='password']")
        pwd.clear()
        return pwd.send_keys(password)

    # 验证码
    def login_ver(self,loginver):
        lv = self.driver.find_element(By.CSS_SELECTOR, "#login-ver")
        lv.clear()
        return lv.send_keys(loginver)

    # 登录界面的登录按钮
    def login_click(self):
        self.driver.find_element(By.CSS_SELECTOR, ".login-btn").click()

    def login_get_text(self):
        return self.driver.find_element(By.CSS_SELECTOR, ".layui-layer-content").text

    def login_err(self):
        self.driver.find_element(By.CSS_SELECTOR,".layui-layer-btn0").click()

    def login_err_get_scree(self):
        self.driver.get_screenshot_as_file("../image/{}.png".format(time.strftime("%Y_%m_%d_%H_%M_%S")))

    def page_login(self, username, password, loginver):
        time.sleep(1)
        self.login_username(username)
        time.sleep(1)
        self.login_password(password)
        time.sleep(1)
        self.login_ver(loginver)
        time.sleep(1)
        self.login_click()

2.2script

import json
import time

from selenium import webdriver
import unittest
from parameterized import parameterized
from script04.V3.page.page_login import PageLogin


def build_data():
    with open('../data/V3_data.json', encoding='utf-8') as f:
        result = json.load(f)
        data = []
        for i in result:
            # 这里一定要注意,要把每次添加的数据转化为元组,不然会报错
            # 报错的原因是因为parameterized只能支持每组数据是元组
            data.append((i.get('username'), i.get('password'), i.get('loginver'), i.get('expect_result')))
    return data


class TestLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.login = PageLogin()
        cls.login.pag_click_login_link()

    @classmethod
    def tearDownClass(cls):
        cls.login.driver.quit()

    @parameterized.expand(build_data())
    def test001(self, username, password, loginver, expect_result):
        # 登录页面的内容输入
        self.login.page_login(username, password, loginver)
        # 获取弹窗的文本内容
        msg = self.login.login_get_text()
        try:
            # 点击弹窗的按钮
            # 注意:不先关闭弹窗的话,该方法可能无法实行
            self.login.login_err()
            # 断言
            self.assertEqual(msg, expect_result)

        except AssertionError:
            # 截图
            self.login.login_err_get_scree()

3.V4

V4为三层架构,base类实现,page页面层,script业务层

3.1base:

import time
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait


class Base:
    def __init__(self):
        self.driver = webdriver.Edge()
        self.driver.get("https://www.tp-shop.cn/")
        self.driver.maximize_window()

    def base_find_element(self, loc, timeout=30, poll=0.5):
        # WebDriverWait为显示等待,*loc中的*是解包的意思,可以将元组解除,
        # 负责定位
        return WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll).until(lambda x: x.find_element(*loc))

    def base_click(self, loc):
        self.base_find_element(loc).click()

    def base_input(self, loc, value):
        el = self.base_find_element(loc)
        el.clear()
        el.send_keys(value)

    def base_get_text(self, loc):
        return self.base_find_element(loc).text

    def base_get_screen(self, loc):
        self.driver.get_screenshot_as_file("../image/{}.png".format(time.strftime("%Y_%m_%d_%H_%M_%S")))

3.2page

# 页面对象层
import time
from script04.V4.base.base import Base
from script04.V4 import page


class PageLogin(Base):
    # 外页面的登录链接,进入登录页面
    def page_click_login_link(self):
        self.base_click(page.login_link)

    # 用户名
    def page_login_username(self, username):
        return self.base_input(page.login_unme, username)

    # 密码
    def page_login_password(self, password):
        return self.base_input(page.login_pwd, password)

    # 验证码
    def page_login_ver(self, loginver):
        return self.base_input(page.login_verify, loginver)

    # 登录界面的登录按钮
    def page_login_click(self):
        self.base_click(page.login_cli)

    # 获取弹窗文本
    def page_login_get_text(self):
        return self.base_get_text(page.login_text)

    # 点击弹窗
    def page_login_err_info(self):
        self.base_click(page.login_err)

    # 截屏
    def page_login_err_get_scree(self):
        self.base_get_screen()

    # 组合
    def page_login(self, username, password, loginver):
        self.page_login_username(username)
        self.page_login_password(password)
        self.page_login_ver(loginver)
        time.sleep(1)
        self.page_login_click()

3.3script

# V4为三层架构,base类实现,page页面层,script业务层

# V1没用
# V2垃圾
# V3乱
# V4重点
# V5在v4上的优化
# V6在v4上的深度优化

import json
import unittest
from parameterized import parameterized
from script04.V4.page.page_login import PageLogin


def build_data():
    with open('../data/V4_data.json', encoding='utf-8') as f:
        result = json.load(f)
        data = []
        for i in result:
            # 这里一定要注意,要把每次添加的数据转化为元组,不然会报错
            # 报错的原因是因为parameterized只能支持每组数据是元组
            data.append((i.get('username'), i.get('password'), i.get('loginver'), i.get('expect_result')))
    return data


class TestLogin(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.login = PageLogin()
        cls.login.page_click_login_link()

    @classmethod
    def tearDownClass(cls):
        cls.login.driver.quit()

    @parameterized.expand(build_data())
    def test001(self, username, password, loginver, expect_result):
        # 登录页面的内容输入
        self.login.page_login(username, password, loginver)
        # 获取弹窗的文本内容
        msg = self.login.page_login_get_text()
        try:
            # 点击弹窗的按钮
            # 注意:不先关闭弹窗的话,该方法可能无法实行
            self.login.page_login_err_info()
            # 断言
            self.assertEqual(msg, expect_result)

        except AssertionError:
            # 截图
            self.login.page_login_err_get_scree()

你可能感兴趣的:(前端,python,开发语言)