一.用户注册测试用例
from time import sleep
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from util import util
class TestUserRegister(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://localhost:8080/jpress/user/register')
self.driver.maximize_window()
def test_register_code_error(self):
username = 'test001'
email = 'test001@qq.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 test_register_ok(self):
username = util.gen_random_str()
email = username + '@qq.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 = util.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()
问题
-
showapi报错:不是有效的图片文件。 该api需要图片url地址或者图片的base64编码,可利用base64模块将图片转换为base64编码解决该问题。 -
同时测试多个用例时,测试下一个前要将上一个的数据清空。否则会报错。
二.用户登录测试用例
from time import sleep
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
class TestUserLogin(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get('http://localhost:8080/jpress/user/login')
self.driver.maximize_window()
def test_user_login_username_error(self):
username = ''
pwd = '123456'
expected = '账号不能为空'
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver, 5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
sleep(3)
assert alert.text == expected
alert.accept()
def test_user_login_username_ok(self):
username = 'admin'
pwd = 'zq740208'
expected = '用户中心'
self.driver.find_element_by_name('user').clear()
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver, 5).until(EC.title_is(expected))
sleep(3)
assert self.driver.title == expected
self.driver.quit()
三.管理员登录测试用例
from time import sleep
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from util import util
class TestAdminLogin(object):
def __init__(self):
self.driver = webdriver.Chrome()
self.driver.get("http://localhost:8080/jpress/admin/login")
self.driver.maximize_window()
def test_admin_login_code_error(self):
username = 'admin'
pwd = 'zq740208'
captcha = '666'
expected = '验证码不正确,请重新输入'
self.driver.find_element_by_name('user').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 == expected
alert.accept()
sleep(5)
def test_admin_login_code_ok(self):
username = 'admin'
pwd = 'zq740208'
captcha = ''
expected = 'JPress后台'
self.driver.find_element_by_name('user').clear()
self.driver.find_element_by_name('user').send_keys(username)
self.driver.find_element_by_name('pwd').clear()
self.driver.find_element_by_name('pwd').send_keys(pwd)
captcha = util.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.title_is(expected))
assert expected == self.driver.title
sleep(5)
四.文章分类测试用例
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep
class TestCategory(object):
def __init__(self, login):
self.login = login
def test_add_category_error(self):
name = ''
parent = 'python'
slug = 'test'
expected = '×\n操作失败\n分类名称不能为空'
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()
self.login.driver.find_element_by_name('category.title').send_keys(name)
parent_category_elem = self.login.driver.find_element_by_name('category.pid')
Select(parent_category_elem).select_by_visible_text(parent)
self.login.driver.find_element_by_name('category.slug').send_keys(slug)
self.login.driver.find_element_by_xpath(
'/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
loc = (By.ID, 'toast-container')
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_add_category_ok(self):
name = 'test'
parent = 'python'
slug = 'test'
expected = None
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[3]/a').click()
self.login.driver.find_element_by_name('category.title').clear()
self.login.driver.find_element_by_name('category.title').send_keys(name)
parent_category_elem = self.login.driver.find_element_by_name('category.pid')
Select(parent_category_elem).select_by_visible_text(parent)
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_xpath(
'/html/body/div/div/section[2]/div/div[1]/div/form/div[2]/div/div/button').click()
assert 1 == 1
五.添加删除文章测试用例
调试程序过程中,sleep的设置很重要,需要等待网页完成相关操作再进入下一步,否则运行可能出错。
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 time import sleep
class TestArticle(object):
def __init__(self, login):
self.login = login
def test_add_ok(self):
title = '我的文章'
content = '我的文章内容'
expected = '×\n文章保存成功。'
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(5)
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[2]/a').click()
self.login.driver.find_element_by_id('article-title').send_keys(title)
sleep(5)
frame1 = self.login.driver.find_element_by_xpath('//*[@id="cke_1_contents"]/iframe')
self.login.driver.switch_to.frame(frame1)
sleep(1)
self.login.driver.find_element_by_xpath('/html/body').send_keys(content)
self.login.driver.switch_to.default_content()
self.login.driver.find_element_by_xpath('//*[@id="form"]/div/div[2]/div[1]/div/button[1]').click()
loc = (By.ID, 'toast-container')
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_delete_one_article_ok(self):
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/a/span[1]').click()
sleep(2)
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
sleep(1)
article_num = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
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(link).perform()
sleep(1)
del_elem = 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]')
del_elem.click()
sleep(1)
article_num2 = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
assert article_num == article_num2 + 1
def test_delete_all_article_ok(self):
self.login.driver.find_element_by_xpath('//*[@id="sidebar-menu"]/li[4]/ul/li[1]/a').click()
sleep(1)
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').click()
self.login.driver.find_element_by_id('batchDel').click()
WebDriverWait(self.login.driver, 5).until(EC.alert_is_present())
alert = self.login.driver.switch_to.alert
alert.accept()
sleep(5)
article_num = len(self.login.driver.find_elements_by_class_name('jp-actiontr'))
assert article_num == 0
|