访问链接然后进行用户注册
1.先输入验证码固定值然后点击注册 验证码肯定不正确,然后处理
2.输入正确的验证码通过前面的方法来把验证码通过就注册成功了
访问的链接:http://localhost:8080/jpress/user/register
在testcase目录下新建一个basic目录然后下面再存放注册的文件代码
为了后期的迭代
然后上代码:
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 seelenium_project.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('password').send_keys(pwd)
self.driver.find_element_by_name('confirm_password').send_keys(confirmPwd)
#验证码写死的
self.driver.find_element_by_name('_register_captcha').send_keys(captcha)
self.driver.find_element_by_xpath('/html/body/div[1]/div/form/div[7]/input').click()
sleep(3)
#因为有弹窗所以需要切换到弹窗上
WebDriverWait(self.driver, 5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
# python 的断言
#获取弹窗上的文字来比较
assert alert.text == expected
sleep(3)
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)
# email
self.driver.find_element_by_name('email').clear()
self.driver.find_element_by_name('email').send_keys(email)
# 密码
self.driver.find_element_by_name('password').send_keys(pwd)
self.driver.find_element_by_name('password').send_keys(pwd)
# 确认密码
self.driver.find_element_by_name('confirm_password').clear()
self.driver.find_element_by_name('confirm_password').send_keys(confirmPwd)
# 自动识别验证码
captcha = util.get_code(self.driver)
# 输入验证码
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()
# 等待alert出现
WebDriverWait(self.driver, 5).until(EC.alert_is_present())
alert = self.driver.switch_to.alert
# 验证
assert alert.text == expected
alert.accept()
self.driver.close()
self.driver.quit()
main函数中的代码就是调用:
from seelenium_project.testcases import testcase1,testcase02
import pytesseract
from selenium import webdriver
from seelenium_project.util import util
from seelenium_project.testcases.basic.test_user_register import TestUserRegister
if __name__ == '__main__':
# 用户注册 验证码不正确
case01=TestUserRegister()
case01.test_register_code_error()
#用户注册成功 验证码正确
case01.test_register_ok()
这样就实现了注册用户。后期会完善,敬请期待!!!!