Jpress 工程目录
一、用户注册
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from Utils import Utils
from time import sleep
# 用户注册
class TestUserRigster(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get("xxx/jpress/user/register")
self.driver.maximize_window()
def userRegister_code_err(self):
username = 'test01'
email = 'test@163.com'
pwd = '123456'
confirmPwd = '123456'
captcha = '666'
expected ='验证码不正确'
self.driver.find_element_by_name('username').send_keys(username)
self.driver.find_element_by_name('email').send_keys(email)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_name('confirmPwd').send_keys(confirmPwd)
self.driver.find_element_by_name('captcha').send_keys(captcha)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
assert alert.text == expected
alert.accept()
sleep(5)
def userRegister_code_sucess(self):
username = 'test01'
email = 'test@163.com'
pwd = '123456'
confirmPwd = '123456'
captcha = ''
expected = '注册成功,点击确定进行登录。'
self.driver.find_element_by_name('username').clear()
self.driver.find_element_by_name('username').send_keys(username)
self.driver.find_element_by_name('email').clear()
self.driver.find_element_by_name('email').send_keys(email)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_name('confirmPwd').clear()
self.driver.find_element_by_name('confirmPwd').send_keys(confirmPwd)
captcha = Utils.get_code(self.driver,'captchaimg')
self.driver.find_element_by_name('captcha').clear()
self.driver.find_element_by_name('captcha').send_keys(captcha)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
assert alert.text == expected
alert.accept()
sleep(5)
二、管理员登录
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from Utils import Utils
from time import sleep
# 管理员登录
class TestAdminLogin(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get("xxx/jpress/admin/login")
self.driver.maximize_window()
def test_admin_login_err(self):
username = 'admin'
pwd = '123456'
captcha = '666'
excepted = '验证码不正确,请重新输入'
self.driver.find_element_by_name('username').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_name('captcha').send_keys(captcha)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver,5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
assert alert.text == excepted
alert.accept()
sleep(3)
def test_admin_login_success(self):
username = 'admin'
pwd = 'admin'
captcha = '666'
excepted = 'JPress后台'
self.driver.find_element_by_name('username').clear()
self.driver.find_element_by_name('username').send_keys(username)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
# 登录验证码的识别应该修改为Xpath //*[@id="form"]/div[3]/img
captcha = Utils.get_code(self.driver, 'captchaimg')
self.driver.find_element_by_name('captcha').clear()
self.driver.find_element_by_name('captcha').send_keys(captcha)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver, 5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
assert excepted == self.driver.title
sleep(3)
self.driver.quit()
三、分类管理
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from time import sleep
# 分类管理
class TestCatefory(object):
# 单点登录
def __init__(self, login):
self.login = login
# 测试新增分类失败
def test_addCatefory_err(self):
Caname = ''
parent = 'selenium'
slug = 'test'
ca_content = '测试添加分类'
ca_summary = '测试摘要'
ca_flag = '测试flag'
# 点击菜单栏-文章
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击文章下的二级菜单-分类
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a').click()
sleep(1)
# 分类名称
self.login.driver.find_element_by_name('category.title').send_keys(Caname)
# 分类的父分类
ca_parent = self.login.driver.find_element_by_name('category.pid')
Select(ca_parent).select_by_visible_text(parent)
# 输入slug
self.login.driver.find_element_by_name('category.slug').send_keys(slug)
# 排序
self.login.driver.find_element_by_name('category.order_number').send_keys(2)
# 内容
self.login.driver.find_element_by_name('category.content').send_keys(ca_content)
# 摘要
self.login.driver.find_element_by_name('category.summary').send_keys(ca_summary)
# 图标
# 标识
self.login.driver.find_element_by_name('category.flag').send_keys(ca_flag)
# 提交
self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
# 验证提示
expected = '分类名称不能为空'
loc = (By.CLASS_NAME, 'toast-message')
WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
msg = self.login.driver.find_element(*loc).text
assert msg == expected
# 测试新增分类成功
def test_addCatefory_sucess(self):
ca_name = 'seTest'
parent = 'selenium'
slug = 'test'
ca_content = '测试添加分类'
ca_summary = '测试摘要'
ca_flag = '测试flag'
# 点击菜单栏-文章
# self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击文章下的二级菜单-分类
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a').click()
sleep(1)
# 分类名称
self.login.driver.find_element_by_name('category.title').clear()
self.login.driver.find_element_by_name('category.title').send_Keys(ca_name)
# 分类的父分类
ca_parent = self.login.driver.find_element_by_name('category.pid')
Select(ca_parent).select_by_visible_text(parent)
# 输入slug
self.login.driver.find_element_by_name('category.slug').clear()
self.login.driver.find_element_by_name('category.slug').send_Keys(slug)
# 排序
self.login.driver.find_element_by_name('category.order_number').clear()
self.login.driver.find_element_by_name('category.order_number').send_Keys(2)
# 内容
self.login.driver.find_element_by_name('category.content').clear()
self.login.driver.find_element_by_name('category.content').send_Keys(ca_content)
# 摘要
self.login.driver.find_element_by_name('category.summary').clear()
self.login.driver.find_element_by_name('category.summary').send_Keys(ca_summary)
# 图标
# 标识
self.login.driver.find_element_by_name('category.flag').clear()
self.login.driver.find_element_by_name('category.flag').send_Keys(ca_flag)
# 提交
self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
# 验证提示
expected = None
assert 1 == 1
四、文章管理
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select
from time import sleep
# 文章管理
class TestArticle(object):
def __init__(self, login):
self.login = login
# 测试添加文章
def test_addArticle_success(self):
ar_title = '测试新增文章'
ar_connect = '文章内容'
expected = '添加成功'
# 点击菜单栏-文章
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
sleep(1)
# 点击新增文章 /html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i
self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i').click()
sleep(1)
# 输入标题
self.login.driver.find_element_by_id('article.title').send_keys()
frame1 = self.login.driver.find_element_by_xpath('')
self.login.driver.swich_to.frame(frame1)
# 输入内容 /html/body
self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
self.login.driver.find_element_by_xpath('/html/body').clear()
sleep(1)
self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
# 退出iframe
self.login.driver.swich_to.default_content()
# 发布
self.login.driver.find_element_by_xpath('//*[@id="form"]/div/div[2]/div[1]/div/button[1]').click()
# 验证提示
loc = (By.CLASS_NAME, 'toast-message')
WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
msg = self.login.driver.find_element(*loc).text
assert msg == expected
# 测试添加文章失败
def test_addArticle_err(self):
self.login.driver.refresh()
sleep(1)
ar_title = ''
ar_connect = '文章内容'
ar_summary = '文章摘要'
ar_seo_kw = '文章seo'
expected = '标题不能为空'
# 点击菜单栏-文章
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
sleep(1)
# 点击新增文章 /html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i
self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[1]/div/div/a/i').click()
sleep(1)
# 输入标题
self.login.driver.find_element_by_id('article.title').send_keys()
frame1 = self.login.driver.find_element_by_xpath('')
self.login.driver.swich_to.frame(frame1)
# 输入内容 /html/body
self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
self.login.driver.find_element_by_xpath('/html/body').clear()
sleep(1)
self.login.driver.find_element_by_xpath('/html/body').send_keys(ar_connect)
# 添加文章摘要
self.login.driver.find_element_by_name('article.summary').send_keys(ar_summary)
# 添加文章seo
self.login.driver.find_element_by_name('article.meta_keywords').send_keys(ar_seo_kw)
# 退出iframe
self.login.driver.swich_to.default_content()
# 发布
self.login.driver.find_element_by_xpath('//*[@id="form"]/div/div[2]/div[1]/div/button[1]').click()
# 验证提示
loc = (By.CLASS_NAME, 'toast-message')
WebDriverWait(self.login.driver, 5).until(EC.visibility_of_element_located(loc))
msg = self.login.driver.find_element(*loc).text
assert msg == expected
# 删除文章
def test_delAcicle(self):
self.login.driver.refresh()
expected = ' '
# 点击菜单栏-文章
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
sleep(1)
# 找到此条文章, 鼠标再移动到垃圾箱
ac_link = self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[2]/td[2]/strong/a')
ActionChains(self.login.driver).move_to_element(ac_link).perform()
sleep(1)
# 查看文章列表数目
ac_sum = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
# 删除文章
ac_select = self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[2]/td[2]/div/div/a[3]')
ac_select.click()
# 测试删除后文章数
ac_del_num =len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
assert ac_del_num == ac_sum+1
def test_delAllActicle(self):
expected = ' '
# 点击菜单栏-文章
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(1)
# 点击文章下的二级菜单-文章管理 //*[@id="sidebar-menu"]/li[4]/ul/li[1]/a
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
sleep(1)
# 勾选全部
ac_link = self.login.driver.\
find_element_by_xpath('/html/body/div/div/section[3]/div/div/div/div[2]/table/tbody/tr[1]/th[1]/input')
ac_link.click()
# 点击批量删除 //*[@id="batchDel"]
self.login.driver.find_element_by_xpath('//*[@id="batchDel"]').click()
WebDriverWait(self.login.driver, 5).until(EC.alert_is_present())
alert = self.login.driver.swich_to.alert
alert.accept()
sleep(1)
ac_del_num = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
assert ac_del_num == 0
五、测试运行
from Cases.Basic.test_userRigster import TestUserRigster
from Cases.Basic.test_admin_login import TestAdminLogin
from Cases.Basic.test_catefory import TestCatefory
from Cases.Basic.test_article import TestArticle
if __name__ == '__main__':
uR = TestUserRigster() # 用户注册
aR = TestAdminLogin() # 管理员登录
tC = TestCatefory(aR) # 分类管理
tA = TestArticle(aR) # 文章管理
user_Register_err = uR.userRegister_code_err() # 用户注册失败
user_Register_sucess = uR.userRegister_code_sucess() # 用户注册成功
ALogin_err = aR.test_admin_login_err() # 管理员登录失败
ALogin_success = aR.test_admin_login_success() # 管理员登录成功
tC.test_addCatefory_err() # 添加分类失败
tC.test_addCatefory_sucess() # 添加分类成功
tA.test_addArticle_err() # 添加文章失败
tA.test_addArticle_success() # 添加文章成功
tA.test_delAcicle() # 删除单条文章
tA.test_delAllActicle() # 删除全部文章