def setUp(self): self.filename = '账号密码.xls' self.filenames = '账号密码1.xls' self.users = readcvs(self.filename) # 读取excle文件中的所以用户,得到字典 self.user = self.users.get('user') # 从字典中取出用户名 self.pwd = self.users.get('pwd') # 从字典中取出密码 driver = webdriver.Chrome() self.user_active = login(driver=driver) print('start')
def problem_test(driver, problem_id='1000', answer=DEFAULT_ANSWER, user='******', pwd='123456'): """ 网站问题页面提交 :param driver: 浏览器驱动 :param problem_id: 问题地址,默认1000。区间[1000-1653] :return: """ username = driver.find_element_by_id('profile') # 验证是否登陆 if username.text == '登录': url = 'http://zwu.hustoj.com/loginpage.php' loginorsign.login(driver=driver, url=url, user=user, pwd=pwd) driver.get('http://zwu.hustoj.com/problem.php?id=' + problem_id) driver.find_element_by_link_text('提交').click() sleep(1) frame_element = driver.find_element_by_id('frame_source') driver.switch_to.frame(frame_element) driver.find_element_by_id('textarea').send_keys(answer) driver.switch_to.default_content() driver.find_element_by_id('Submit').click() sleep(1) try: message_element = driver.find_element_by_xpath( '/html/body/div[1]/div/div[2]/table/tbody/tr[1]/td[4]/span') while message_element.text == '等待' or message_element.text == '编译中': # print(message_element.text) sleep(0.8) message_element = driver.find_element_by_xpath( '/html/body/div[1]/div/div[2]/table/tbody/tr[1]/td[4]/span') except NoSuchElementException: try: message_element = driver.find_element_by_xpath( '/html/body/div[1]/div/div[2]/table/tbody/tr[1]/td[4]/a') except NoSuchElementException: message_element = driver.find_element_by_xpath( '/html/body/div[1]/div/div[2]/table/tbody/tr[1]/td[4]/span') except Exception as e: return '获取结果超时, erroe %s' % e message = message_element.text return message
def force(nodelist, urls, url, user, pwd, i, MP, lock, dtime, loginmsg): """ :param nodelist: 分布式驱动 :param urls: 网站各页面地址 :param url: 网站登录地址 :param user: 登录用户名 :param pwd: 登录密码 :param i: 线程号 :param MP: 主线程号 :param lock: 进程锁 :param message: 测试时间 :param loginmsg: 登录信息 :return: """ print('主进程', MP, '下的压力程序', i, '开始运行') driver = driverremote.browser(nodelist.get('host'), nodelist.get('DesiredCapabilities')) dtime['start' + str(i)] = time.time() try: loginmsg['force' + str(i)] = loginorsign.login(driver, url, user, pwd) if loginmsg['force' + str(i)] in 'login: success': loginmsg['force' + str(i)] = 1 for page in urls: driver.get(page) driver.quit() else: raise loginError(login) except loginError: loginmsg['force' + str(i)] = 0 finally: dtime['end' + str(i)] = time.time() lock.acquire() setForceTime( site_ip='http://' + url.split('/')[2] + '/', username=user, password=pwd, login_status=loginmsg['force' + str(i)], urls_len=len(urls), start_end_time=int( round( (dtime['end' + str(i)] - dtime['start' + str(i)]) * 1000))) lock.release() print('主进程', MP, '下的压力程序', i, '结束运行')
def login(nodelist, url, user, pwd, i, MP, lock, message): print('主进程', MP, '下的登录程序', i, '开始运行') driver = driverremote.browser(nodelist.get('host'), nodelist.get('DesiredCapabilities')) try: message['login' + str(i)] = loginorsign.login(driver, url, user, pwd) if message['login' + str(i)] in 'login: success': message['login' + str(i)] = 1 driver.quit() else: raise loginError(message['login' + str(i)]) except loginError as e: print(message['login' + str(i)]) message['login' + str(i)] = 0 finally: lock.acquire() setLoginAccout(site_ip='http://' + url.split('/')[2] + '/', username=user, password=pwd, test_login=message['login' + str(i)]) lock.release() print('主进程', MP, '下的登录程序', i, '结束运行')
def problem(nodelist, user, pwd, i, MP, lock, dtime, problemmsg, loginmsg): print('主进程', MP, '下的问题执行程序', i, '开始运行') driver = driverremote.browser(nodelist.get('host'), nodelist.get('DesiredCapabilities')) dtime['start' + str(i)] = time.time() url = 'http://zwu.hustoj.com/loginpage.php' try: loginmsg['problem' + str(i)] = loginorsign.login( driver, url, user, pwd) if loginmsg['problem' + str(i)] in 'login: success': loginmsg['problem' + str(i)] = 1 problemmsg['problem' + str(i)] = submitproblem.problem_test(driver) if '获取结果超时' in problemmsg['problem' + str(i)]: raise problemError(problemmsg['problem' + str(i)]) driver.quit() else: raise loginError(login) except loginError: loginmsg['problem' + str(i)] = 0 problemmsg['problem' + str(i)] = '登录失败,未测试问题' except problemError: problemmsg['problem' + str(i)] = '未知错误,获取结果超时' finally: dtime['end' + str(i)] = time.time() lock.acquire() setLoginProblem( site_ip='http://zwu.hustoj.com/', username=user, password=pwd, login_status=loginmsg['problem' + str(i)], problem_id=1000, problem_res=problemmsg['problem' + str(i)], start_end_time=int( round( (dtime['end' + str(i)] - dtime['start' + str(i)]) * 1000))) lock.release() print('主进程', MP, '下的问题执行程序', i, '结束运行')