import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestLogin:
def test_login_without_username_and_password(self):
"""测试没有用户名和密码的情况。
尽量通过测试用例的函数名称表示测试的测试点。
梳理测试步骤:、
1、打开浏览器
2、访问测试页面
3、点击提交
4、断言
"""
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.maximize_window()
url = "https://v4.ketangpai.com/User/login.html"
driver.get(url)
user = "13566666669"
driver.find_element(By.XPATH,'//input[@placeholder="邮箱/账号/手机号"]').send_keys(user)
password = "wt66666644"
driver.find_element(By.XPATH,'//input[@placeholder="密码"]').send_keys(password)
# 定位登录按钮
yx_login = driver.find_element(By.XPATH,'//div[@class="padding-cont pt-login"]//a[@class="btn-btn"]')
yx_login.click()
# 断言页面标题
time.sleep(1)
assert driver.current_url == "https://v4.ketangpai.com/Main/index.html"
user_elem = driver.find_element(By.XPATH,'//a[@id="user"]//img[@class="avatar"]')
assert user_elem.get_attribute("title") == "十七"
============================================================
函数封装
PO 模式:Page Object, Page Object Model, POM
page object model : 把一个页面转化成对象
page.login() 完成登录操作
page.clear() 完成用户名和密码清空操作
page.load() 访问页面操作
为什么用 po 模式?po 模式的好处在哪里
- 可读性强, 通过方法名称表示页面操作
- 可扩展性,
- 可维护性。前端发生变化和用例发生变化。
- 可复用性。重复代码共用。
from selenium import webdriver
from selenium.webdriver.common.by import By
def get_driver():
# 启动
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.maximize_window()
return driver
# 封装登录过程
def login(driver,username,password):
# 登录过程
driver.find_element(By.XPATH, '//input[@placeholder="邮箱/账号/手机号"]').send_keys(username)
driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys(password)
# 定位登录按钮
yx_login = driver.find_element(By.XPATH, '//div[@class="padding-cont pt-login"]//a[@class="btn-btn"]')
yx_login.click()
# 封装URL
def loan(driver,url):
driver.get(url)
def clear(driver):
"""清空用户名和密码"""
driver.find_element(By.XPATH, '//input[@placeholder="邮箱/账号/手机号"]').clear()
driver.find_element(By.XPATH, '//input[@placeholder="密码"]').clear()
======================================================================
类封装
PO 模式:Page Object, Page Object Model, POM
page object model : 把一个页面转化成对象
page.login() 完成登录操作
page.clear() 完成用户名和密码清空操作
page.load() 访问页面操作
为什么用 po 模式?po 模式的好处在哪里
- 可读性强, 通过方法名称表示页面操作
- 可扩展性,
- 可维护性。前端发生变化和用例发生变化。
- 可复用性。重复代码共用。
class Login:
def __init__(self,driver):
self.driver = driver
def login(self, username, password):
# 登录过程
self.driver.find_element(By.XPATH, '//input[@placeholder="邮箱/账号/手机号"]').send_keys(username)
self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys(password)
# 定位登录按钮
yx_login = self.driver.find_element(By.XPATH, '//div[@class="padding-cont pt-login"]//a[@class="btn-btn"]')
yx_login.click()
# 封装URL
def loan(self, url):
self.driver.get(url)
def clear(self):
"""清空用户名和密码"""
self.driver.find_element(By.XPATH, '//input[@placeholder="邮箱/账号/手机号"]').clear()
self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').clear()
==================================================================
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class TestLogin:
def test_login_without_username_and_password(self):
"""测试没有用户名和密码的情况。
尽量通过测试用例的函数名称表示测试的测试点。
梳理测试步骤:、
1、打开浏览器
2、访问测试页面
3、点击提交
4、断言
"""
driver = get_driver()
url = "https://v4.ketangpai.com/User/login.html"
driver.get(url)
# 登录
login(driver,"13560746669","w6666664")
# 断言页面标题
time.sleep(1)
assert driver.current_url == "https://v4.ketangpai.com/Main/index.html"
user_elem = driver.find_element(By.XPATH,'//a[@id="user"]//img[@class="avatar"]')
assert user_elem.get_attribute("title") == "十七"
==================================================================
conftest 前置
import pytest
from selenium import webdriver
@pytest.fixture()
def get_driver():
# 启动
driver = webdriver.Firefox()
driver.implicitly_wait(10)
driver.maximize_window()
yield driver
driver.quit()
==================================================================
pages 模块中 login 页面
from selenium.webdriver.common.by import By
class Login:
url = "https://v4.ketangpai.com/User/login.html"
def __init__(self,driver):
self.driver = driver
def login(self, username, password):
# 登录过程
self.driver.find_element(By.XPATH, '//input[@placeholder="邮箱/账号/手机号"]').send_keys(username)
self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').send_keys(password)
# 定位登录按钮
yx_login = self.driver.find_element(By.XPATH, '//div[@class="padding-cont pt-login"]//a[@class="btn-btn"]')
yx_login.click()
return self
# 封装URL
def loan(self):
"""访问页面"""
self.driver.get(self.url)
return self
def clear(self):
"""清空用户名和密码"""
self.driver.find_element(By.XPATH, '//input[@placeholder="邮箱/账号/手机号"]').clear()
self.driver.find_element(By.XPATH, '//input[@placeholder="密码"]').clear()
def get_elem(self):
"""断言"""
# 方法一:
actual = self.driver.find_elements(By.XPATH, '//div[@class="atsx-form-explain"]')
actual_value = []
for el in elem :
actual_value.append(el.text)
text = actual_value
return text
# 方法二:
# elem = self.driver.find_elements(*self.elem_msg)
# text = [el.text for el in elem]
# return text
==================================================================
pages 模块中 home 页面
from selenium.webdriver.common.by import By
class HomePage:
url = "https://v4.ketangpai.com/Main/index.html"
def __init__(self,driver):
self.driver = driver
def loan(self):
self.driver.get(self.url)
return self
def get_username(self):
"""获取头像中的用户名"""
user_elem = self.driver.find_element(By.XPATH, '//a[@id="user"]//img[@class="avatar"]')
return user_elem.get_attribute("title")
==================================================================
课堂派成登录的用例
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from pages.login import Login
from pages.home import HomePage
class TestLogin:
def test_login_without_username_and_password(self,get_driver):
"""测试没有用户名和密码的情况。
尽量通过测试用例的函数名称表示测试的测试点。
梳理测试步骤:、
1、打开浏览器
2、访问测试页面
3、点击提交
4、断言
"""
driver = get_driver
login_page = Login(driver)
home_page = HomePage(driver)
login_page.loan()
# 登录
login_page.login("13560748553","666666")
# 断言页面标题
time.sleep(1)
assert driver.current_url == home_page.url
user_elem = home_page.get_username()
assert user_elem == "十七"