【没有所谓的运气🍬,只有绝对的努力✊】
目录
使用淘宝网 https://www.taobao.com/ ,完成下来v1-v4的版本。
今日目标:
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