day22——web自动化测试(4)——PO模式 和 数据驱动

【没有所谓的运气🍬,只有绝对的努力✊】

目录

今日目标:

1、PO 模式

1.1 PO是什么?

1.2 PO能解决什么问题?

1.3 PO如何做

使用淘宝网 https://www.taobao.com/ ,完成下来v1-v4的版本。

v1 版本(✅)

v2 版本——unittest(✅)

 v3 版本——unittest(✅)

v4 版本(base层、page层、script层)

【Base】

2、数据驱动

2.1 什么是数据驱动?

2.2 为什么要数据驱动?

2.3 数据驱动如何操作?

2.3.1、编写数据存储文件


今日目标:


1、PO 模式

1.1 PO是什么?

PO :Page Object(页面对象),将自动化涉及的页面或模块封装成对象。

1.2 PO能解决什么问题?

1、代码复用性

2、便于维护(脚本层与业务分离)——如果元素信息发生变化,也不用去修改脚本。

1.3 PO如何做

base层:存放所有页面公共方法。

page层:基于页面或模块单独封装当前页面要操作的对象。

script层:脚本层 + unittest

使用淘宝网 https://www.taobao.com/ ,完成下来v1-v4的版本。

v1 版本(✅)

不使用任何设计模式和单元测试框架。

每个文件里编写一个用例,完全的面向过程的编程方式。


【test_login_pwd_not_exist.py】

from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# 设置正确的驱动路径
service = ChromeService(executable_path="/usr/local/bin/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

# 1、打开12306网页
driver.get("https://www.12306.cn/index/")

# 2、点击右上角 "登录",进入登录页面
driver.find_element(By.ID, 'J-btn-login').click()
sleep(2)

# 3、输入用户名,密码为空
driver.find_element(By.ID, 'J-userName').send_keys("admin")
sleep(2)

# 4、点击"立即登录"按钮
driver.find_element(By.ID, 'J-login').click()

# 5、获取提示信息
err_msg = driver.find_element(By.CSS_SELECTOR, '#J-login-error span').text
expect_msg = "请输入密码!"
try:
    assert err_msg == expect_msg
except AssertionError:
    raise

sleep(2)
# 关闭
driver.quit()

【test_login_username_not_exist.py】

from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdriver.common.by import By

# 设置正确的驱动路径
service = ChromeService(executable_path="/usr/local/bin/chromedriver")
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(service=service, options=options)

# 1、打开12306网页
driver.get("https://www.12306.cn/index/")

# 2、点击右上角 "登录",进入登录页面
driver.find_element(By.ID, 'J-btn-login').click()
sleep(2)

# 3、用户名为空,输入密码
driver.find_element(By.ID, 'J-password').send_keys("123456")
sleep(2)

# 4、点击"立即登录"按钮
driver.find_element(By.ID, 'J-login').click()

# 5、获取提示信息
err_msg = driver.find_element(By.CSS_SELECTOR, '#J-login-error span').text
expect_msg = "请输入用户名!"
try:
    assert err_msg == expect_msg
except AssertionError:
    raise

sleep(2)
# 关闭
driver.quit()

v2 版本——unittest(✅)

        

【test_login_unittest.py】

import unittest
from time import sleep

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from selenium.webdri
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熙熙攘攘Re

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值