import time
from selenium import webdriver
from selenium.webdriver.common.by import By
class Test:
# 输入正确的:账号和密码,登录成功
def test_login_01(self):
driver = webdriver.Chrome()
driver.implicitly_wait(8)
driver.maximize_window()
url = "https://v4.ketangpai.com/User/login.html"
driver.get(url)
# 元素:用户名
user_name = (By.XPATH,'//input[@name="account"]')
driver.find_element(*user_name).send_keys('19142706666')
# 元素:密码
pass_word = (By.XPATH, '//input[@name="pass"]')
driver.find_element(*pass_word).send_keys('666666666')
# 元素:登录
up_dl = (By.XPATH, '//a[@class="btn-btn"]')
driver.find_element(*up_dl).click()
# 断言页面标题
time.sleep(0.5)
up_url = "https://v4.ketangpai.com/Main/index.html"
assert driver.current_url == up_url
img_name = (By.XPATH,'//a[@id="user"]//img[@class="avatar"]')
user_elem = driver.find_element(*img_name)
assert user_elem.get_attribute("title") == '十八'
# 账号不能为空,密码不能为空。
def test_login(self):
driver = webdriver.Chrome()
driver.implicitly_wait(8)
driver.maximize_window()
url = "https://v4.ketangpai.com/User/login.html"
driver.get(url)
# 元素:用户名
user_name = (By.XPATH,'//input[@name="account"]')
driver.find_element(*user_name).click()
# 元素:密码
pass_word = (By.XPATH, '//input[@name="pass"]')
driver.find_element(*pass_word).click()
# 元素:登录
up_dl = (By.XPATH, '//a[@class="btn-btn"]')
driver.find_element(*up_dl).click()
expect = ["账号不能为空","密码不能为空"]
# 元素:"账号不能为空","密码不能为空"
name_word =(By.XPATH,'//p[@class="error-tips"]')
elem = driver.find_elements(*name_word)
actual_value = []
# for el in elem:
# actual_value.append(el.text)
# text = actual_value
text = [el.text for el in elem]
assert expect == text
===============================================================
import pytest
from selenium import webdriver
@pytest.fixture()
# 登录浏览器
def login_get():
driver = webdriver.Chrome()
driver.implicitly_wait(8)
driver.maximize_window()
yield driver
driver.quit()
========================================================
from selenium.webdriver.common.by import By
class Login:
url = "https://v4.ketangpai.com/User/login.html"
""" 元素:用户名 """
user_name = (By.XPATH, '//input[@name="account"]')
""" 元素:密码 """
pass_word = (By.XPATH, '//input[@name="pass"]')
""" 元素:登录 """
up_dl = (By.XPATH, '//a[@class="btn-btn"]')
""" "账号不能为空","密码不能为空" """
user_word = (By.XPATH, '//p[@class="error-tips"]')
def __init__(self, driver):
self.driver = driver
""" 访问 URL 页面 """
def load(self):
self.driver.get(self.url)
return self
""" 账号、密码、登录 """
def up_login(self,username,password):
self.driver.find_element(*self.user_name).send_keys(username)
self.driver.find_element(*self.pass_word).send_keys(password)
self.driver.find_element(*self.up_dl).click()
return self
def clear(self): # 清空用户名和密码
self.driver.find_element(By.XPATH, '//input[@name="account"]').clear()
self.driver.find_element(By.XPATH, '//input[@name="pass"]').clear()
return self
def actual(self):
"""获取错误信息:"账号不能为空","密码不能为空" """
elem = self.driver.find_elements(*self.user_word)
text = [el.text for el in elem]
return text
# actual_value = []
# for el in elem:
# actual_value.append(el.text)
# text = actual_value
===================================================================
from selenium.webdriver.common.by import By
class Home:
url = "https://v4.ketangpai.com/Main/index.html"
""" 元素: 获取头像"""
win = (By.XPATH, '//a[@id="user"]//img[@class="avatar"]')
def __init__(self,driver):
self.driver = driver
def load(self):
self.driver.get(self.url)
return self
""" 获取头像中的登录用户名 """
def get_win(self):
get_img = self.driver.find_element(*self.win)
return get_img.get_attribute("title")
============================================================
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from pages.home_page import Home
from pages.login_page import Login
class Test:
# 输入正确的:账号和密码,登录成功
def test_login_01(self,login_get):
# 前置条件调用
driver = login_get
# 重新赋值导入模块的实例化
login = Login(driver)
home = Home(driver)
# 访问 URL
login.load()
login.up_login("19142706666","666666666")
# 断言页面标题
time.sleep(0.5)
assert driver.current_url == home.url
actual = home.get_win()
assert actual == "十八"
# 账号不能为空,密码不能为空。
def test_login(self,login_get):
driver = login_get
login = Login(driver)
login.load()
login.up_login("","")
expect = ["账号不能为空","密码不能为空"]
text = login.actual()
assert expect == text
=============================================================
import pytest
import time
from pages.home_page import Home
from pages.login_page import Login
class Test:
# 输入正确的:账号和密码,登录成功
@pytest.mark.parametrize("info",[("19142706666","666666666","十八")])
def test_login_01(self,login_get,info):
# 赋值用例 info,方便调用
username,password,expect = info
# 前置条件调用
driver = login_get
# 重新赋值导入模块的实例化
login = Login(driver)
home = Home(driver)
# 访问 URL
login.load()
login.up_login(username,password)
# 断言页面标题
time.sleep(0.5)
assert driver.current_url == home.url
actual = home.get_win()
assert actual == expect
# 账号不能为空,密码不能为空。
@pytest.mark.parametrize("info", [("","",["账号不能为空","密码不能为空"])])
def test_login(self,login_get,info):
username,password,expect = info
driver = login_get
login = Login(driver)
login.load()
login.up_login(username,password)
text = login.actual()
assert text == expect
=====================================================================
# 登录成功
list_win = [ ("19142706666","666666666","十八")]
list_no = [("","",["账号不能为空","密码不能为空"])]
list_username = [("","6666666",["账号不能为空"]),
("6666666","",["密码不能为空"]),
("666", "wtg44444",["用户不存在"]),
("135607483", "wtg",["密码有效长度是6到30个字符"])
]
======================================================
import pytest
import time
from pages.home_page import Home
from pages.login_page import Login
from data import login_test
class Test:
# 输入正确的:账号和密码,登录成功
@pytest.mark.parametrize("info",login_test.list_win)
def test_login_01(self,login_get,info):
# 赋值用例 info,方便调用
username,password,expect = info
# 前置条件调用
driver = login_get
# 重新赋值导入模块的实例化
login = Login(driver)
home = Home(driver)
# 访问 URL
login.load()
login.up_login(username,password)
# 断言页面标题
time.sleep(0.5)
assert driver.current_url == home.url
actual = home.get_win()
assert actual == expect
# 账号不能为空,密码不能为空。
@pytest.mark.parametrize("info", login_test.list_username)
def test_login(self,login_get,info):
username,password,expect = info
driver = login_get
login = Login(driver)
login.load()
login.up_login(username,password)
text = login.actual()
assert text == expect
===================================
host: "https://v4.ketangpai.com"
user1:
username: "19142706666"
password: "666666666"
===============================================================
## 管理项目下的路径
import os
# 获取当前文件的路径
config_path = os.path.abspath(__file__)
print(config_path)
# config 目录
config_dir = os.path.dirname(config_path)
print(config_dir)
# 项目路径
root_dir = os.path.dirname(config_dir)
print(root_dir)
# 获取 data 目录路径
data_dir = os.path.join(root_dir,"config")
print(data_dir)
if not os.path.exists(data_dir):
os.mkdir(data_dir)
# yaml 配置文件的路径
config_yaml_path = os.path.join(data_dir, 'config.yaml')
print(config_yaml_path)
====================================
import yaml
from common.path import config_yaml_path
def read_yaml(fpath):
"""fpath: yaml 文件的路径"""
with open(fpath, encoding='utf-8') as f:
# 读取yaml 当中的数据
data = yaml.safe_load(f)
return data
# 获取yaml配置项
yaml_config = read_yaml(config_yaml_path)
print(yaml_config)
======================================================================
host = yaml_config["host"]
url = host + "/User/login.html"
print(url)