# coding:utf-8
from selenium import webdriver
from time import sleep
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://120.25.209.187:12306/jpress/admin/login')
self.driver.maximize_window()
# 测试 admin 登录成功
def test_admin_login_success(self):
username = 'admin'
pwd = '123456'
# title
expect = 'JPress后台'
# 页面刷新
self.driver.refresh()
# 获取验证码
code = util.get_code(self.driver, 'captchaImg')
print(code)
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(code)
self.driver.find_element_by_class_name('btn').click()
WebDriverWait(self.driver, 5).until(EC.title_is(expect))
assert self.driver.title == expect
sleep(3)
self.driver.quit()
def test_admin_login_code_error(self):
username = 'admin'
pwd = '123456'
code = '555'
# alert 弹窗
expect = '验证码不正确,请重新输入'
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(code)
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 == expect
alert.accept()
sleep(3)
if __name__ == '__main__':
test = TestAdminLogin()
test.test_admin_login_code_error()
test.test_admin_login_success()