def get_logger(): import logging import logging.handlers import datetime logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) rf_handler = logging.handlers.TimedRotatingFileHandler('all.log', when='midnight', interval=1, backupCount=7, atTime=datetime.time(0, 0, 0, 0)) rf_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")) f_handler = logging.FileHandler('error.log') f_handler.setLevel(logging.ERROR) f_handler.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(filename)s[:%(lineno)d] - %(message)s")) logger.addHandler(rf_handler) logger.addHandler(f_handler) return logger |
from util import util logger=util.get_logger() logger.info('123') |
from util import util |
self.logger = util.get_logger() self.logger.info('用户登录用例') |
self.logger.debug('输入用户名称:%s ',username) |
assert expect == self.driver.title |
try: assert expect == self.driver.title except AssertionError as ae: self.logger.error("验证错误:%s", "报错了", exc_info=1) |
import time import pytest from selenium import webdriver #将webdriver驱动导入selenium框架中 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from util.util import * class Test_admin_login(object): def setup_class(self): options = webdriver.ChromeOptions() # 添加无界面参数 options.add_argument('--headless') self.driver=webdriver.Chrome(options=options) self.driver.get('http://localhost:8888/jpress/admin/login') self.driver.maximize_window() login_data=[('wx','123456','666','验证码不正确,请重新输入'), @pytest.mark.dependency(name='admin_login') @pytest.mark.parametrize('username,pwd,captcha,expect',login_data) def test1(self,username,pwd,captcha,expect): #测试数据 #正确用户名 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) if captcha!='666': # 正确的验证码 js = "document.getElementsByTagName('img')[0].setAttribute('id','captchaimg')" self.driver.execute_script(js) captcha = get_code(self.driver, id='captchaimg', uname='luoditao', pwd='xinsheng2', ) 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-flat').click() WebDriverWait(self.driver, 5).until(EC.title_is(expect)) assert expect == self.driver.title time.sleep(3) else: #错误的验证码 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-flat').click() WebDriverWait(self.driver,20).until(EC.alert_is_present()) alert=self.driver.switch_to.alert assert alert.text==expect alert.accept() time.sleep(2) if __name__ == '__main__': pytest.main(['test_admin_login.py']) |
import time import pytest from selenium import webdriver #将webdriver驱动导入selenium框架中 from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from util import util from util.util import get_code class Test_admin_login(object): def setup_class(self): options = webdriver.ChromeOptions() # 添加无界面参数 options.add_argument('--headless') self.driver=webdriver.Chrome(options=options) self.driver.get('http://localhost:8888/jpress/admin/login') self.driver.maximize_window() self.logger = util.get_logger() self.logger.info('测试admin登录') login_data=[('wx','123456','666','验证码不正确,请重新输入'), @pytest.mark.dependency(name='admin_login') @pytest.mark.parametrize('username,pwd,captcha,expect',login_data) def test1(self,username,pwd,captcha,expect): #测试数据 #正确用户名 self.driver.find_element_by_name('user').clear() self.driver.find_element_by_name('user').send_keys(username) self.logger.debug('输入用户名称:%s ',username) # 正确密码 self.driver.find_element_by_name('pwd').clear() self.driver.find_element_by_name('pwd').send_keys(pwd) self.logger.debug('输入密码:%s ', pwd) if captcha!='666': # 正确的验证码 js = "document.getElementsByTagName('img')[0].setAttribute('id','captchaimg')" self.driver.execute_script(js) captcha = get_code(self.driver, id='captchaimg', uname='luoditao', pwd='xinsheng2', ) self.driver.find_element_by_name('captcha').clear() self.driver.find_element_by_name('captcha').send_keys(captcha) self.logger.debug('输入验证码:%s ', captcha) # 登录按钮 self.driver.find_element_by_class_name('btn-flat').click() self.logger.debug('点击登录按钮') WebDriverWait(self.driver, 5).until(EC.title_is(expect)) # 验证 try: assert expect == self.driver.title except AssertionError as ae: self.logger.error("验证错误:%s", "报错了", exc_info=1) time.sleep(3) else: #错误的验证码 self.driver.find_element_by_name('captcha').clear() self.driver.find_element_by_name('captcha').send_keys(captcha) self.logger.debug('输入验证码:%s ', captcha) #登录按钮 self.driver.find_element_by_class_name('btn-flat').click() self.logger.debug('点击登录按钮') WebDriverWait(self.driver,20).until(EC.alert_is_present()) alert=self.driver.switch_to.alert try: assert alert.text==expect except AssertionError as ae: self.logger.error("验证错误:%s", "报错了", exc_info=1) alert.accept() time.sleep(2) if __name__ == '__main__': pytest.main(['test_admin_login.py']) |
欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) | Powered by Discuz! X3.2 |