def test_case(self, logindata): logger.info("正确账号进行登录用例") filename = "ScreemshotVerfycode.png" self.driver.save_screenshot(filename) ele = self.driver.find_element_by_xpath( '''//*[@id="form"]/ul[3]/li[3]/img''') left = ele.location["x"] top = ele.location["y"] print(ele.location) right = left + ele.size["width"] bottom = top + ele.size["height"] print(ele.size) im = Image.open(filename) im1 = im.crop((left, top, right, bottom)) im1.save(filename) pic = open(filename, "rb").read() code = VerCode.PostPic(pic, 1902)["pic_str"] account, passwd = tuple(logindata.strip().split("||")) self.driver.implicitly_wait(10) try: self.driver.find_element_by_xpath( '''//*[@id="M_username"]''').send_keys(account) self.driver.find_element_by_xpath( '''//*[@id="M_password"]''').send_keys(passwd) self.driver.find_element_by_xpath('''//*[@id="code"]''').send_keys( code) self.driver.find_element_by_xpath( '''//*[@id="form"]/input''').click() time.sleep(1) self.assertIn("肖的测试企业", self.driver.page_source) except NoSuchElementException as e: logger.error("没有发现元素") except AssertionError as e: logger.error("结果错误" + str(e)) raise AssertionError
def test_case(self): logger.info("用户导入用例") self.driver.implicitly_wait(10) try: self.driver.find_element_by_xpath( '''/html/body/div[2]/div[2]/ul/li/a''').click() self.driver.find_element_by_xpath( '''//*[@id="tab"]/li[3]/a''').click() self.driver.find_element_by_xpath( '''//*[@id="tab"]/li[3]/ul/li[2]/a''').click() self.driver.switch_to.frame("content") self.driver.find_element_by_xpath( '''//*[@id="list"]/div[1]/button[2]''').click() self.driver.find_element_by_id("file").send_keys( r"G:\testfile\userImport.xlsx") locator = (By.XPATH, '''//*[@id="form"]/div/div[2]/button''') WebDriverWait(self.driver, 10, 0.5).until(EC.presence_of_element_located(locator)) time.sleep(1) self.driver.find_element_by_xpath( '''//*[@id="form"]/div/div[2]/button''').click() self.assertIn("肖测试", self.driver.page_source) except NoSuchElementException as e: logger.error("没有发现元素" + str(e)) except AssertionError as e: logger.error("测试结果错误" + str(e)) raise AssertionError
def test_case(self): logger.info("发放积分用例") self.driver.implicitly_wait(10) try: self.driver.find_element_by_xpath('''/html/body/div[2]/div[2]/ul/li/a''').click() self.driver.find_element_by_xpath('''//*[@id="tab"]/li[4]/a/span/b''').click() self.driver.find_element_by_xpath('''//*[@id="tab"]/li[4]/ul/li[1]/a''').click() self.driver.switch_to.frame("content") self.driver.find_element_by_xpath('''//*[@id="tree"]/ul/li/div/div[2]/div[1]/button''').click() self.driver.find_element_by_xpath('''//*[@id="monery"]''').send_keys(100) self.driver.find_element_by_xpath('''//*[@id="homePage"]/div[2]/div/div/div[5]/button''').click() self.driver.find_element_by_xpath('''//*[@id="Recharge"]/div[2]/div/div[1]/div[1]/span[2]/div/div[2]/div/label/div[1]/span/input''').click() self.driver.find_element_by_xpath('''//*[@id="quanxian1"]/div/label/div[1]/span/input''').click() self.driver.find_element_by_xpath('''//*[@id="firstStepsBtn"]''').click() time.sleep(3) location0=(By.XPATH,'''//*[@id="Recharge1"]/div[2]/div/div/div[1]/div/div/div/button[1]''') WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(location0)) self.driver.find_element_by_xpath('''//*[@id="Recharge1"]/div[2]/div/div/div[1]/div/div/div/button[1]''').click() self.driver.find_element_by_id("uploadFile").send_keys(r"C:\Users\admin\Desktop\pic\tim1g.jpg") self.driver.find_element_by_xpath('''//*[@id="form"]/div/div[2]/div/button[1]''').click() time.sleep(1) except NoSuchElementException as e: logger.error("没有发现元素"+str(e)) except AssertionError as e: logger.error("测试结果错误"+str(e)) raise AssertionError
def test_addEmp(self): '''添加员工用例''' self.driver.implicitly_wait(10) try: self.driver.switch_to.frame("content") self.driver.find_element_by_xpath( '''//*[@id="TheSix"]/div[2]/img''').click() self.driver.find_element_by_xpath( '''//*[@id="list"]/div[1]/button[1]''').click() self.driver.find_element_by_xpath( '''//*[@id="username"]''').send_keys("吓跑") self.driver.find_element_by_xpath( '''//*[@id="jobNumber"]''').send_keys("741852") self.driver.find_element_by_xpath( '''//*[@id="idNumber"]''').send_keys("513101199208302119") self.driver.find_element_by_xpath( '''//*[@id="phone"]''').send_keys("18227101093") self.driver.find_element_by_xpath('''//*[@id="tijiao"]''').click() self.assertIn("导入员工", self.driver.page_source) except NoSuchElementException as e: logger.error("没有发现元素" + str(traceback.format_exc())) except AssertionError as e: logger.error("添加员工失败") raise AssertionError else: logger.info("添加成功")
def test_case01(self, value): print(value) testdata, expectdata = tuple(value.strip().split("||")) self.driver.implicitly_wait(10) try: self.driver.find_element_by_id("kw").send_keys(testdata) self.driver.find_element_by_id("u").click() time.sleep(2) self.assertIn(expectdata, self.driver.page_source) except NoSuchElementException as e: logger.info("页面元素不存在" + str(traceback.format_exc())) except AssertionError as e: logger.error("搜索%s,期望%s,失败" % (testdata, expectdata)) raise AssertionError else: logger.info("搜索%s,期望%s,通过" % (testdata, expectdata))
def test_case(self, data): testdata, exceptdata = tuple(data) self.driver.implicitly_wait(10) try: self.driver.find_element_by_id("kw").send_keys(testdata) self.driver.find_element_by_id("su").click() time.sleep(1) self.assertIn(exceptdata, self.driver.page_source) except NoSuchElementException as e: logger.error("查找页面元素失败" + str(traceback.format_exc())) except AssertionError as e: logger.info("搜索%s,期望%s失败" % (testdata, exceptdata)) raise AssertionError except Exception as e: logger.error("未知信息错误" + str(traceback.format_exc())) else: logger.info("搜索%s,期望%s通过" % (testdata, exceptdata))
def test_dataDrivernByObj(self, testdata, expectdata): self.driver.get("http://www.baidu.com") self.driver.implicitly_wait( 10) # 设置全局等待时间,之后出现的元素在10s内每隔05s检查一次,最大时间超过10s才会报错 try: self.driver.find_element_by_id("kw").send_keys(testdata) self.driver.find_element_by_id("su").click() time.sleep(3) self.assertIn(expectdata, self.driver.page_source) except NoSuchElementException as e: logger.error("页面元素不存在:" + str(traceback.format_exc())) except AssertionError as e: logger.info("搜索,%s期望,%s失败" % (testdata, expectdata)) except Exception as e: logger.error("未知错误:" + str(traceback.format_exc())) else: logger.info("搜索:%s期望:%s通过" % (testdata, expectdata))
def test_case(self): logger.info("用户激活用例") self.driver.implicitly_wait(10) #设置隐式等待 try: self.driver.find_element_by_xpath( '''/html/body/div[2]/div[2]/ul/li/a''').click() self.driver.find_element_by_xpath( '''//*[@id="tab"]/li[3]/a''').click() self.driver.find_element_by_xpath( '''//*[@id="tab"]/li[3]/ul/li[3]/a''').click() self.driver.switch_to.frame("content") self.driver.find_element_by_xpath( '''//*[@id="ListContent"]/tr[1]/td[8]/a[1]''').click() sql = "SELECT is_open FROM t_app_user WHERE username='******'" cursor.execute(sql) is_open = cursor.fetchall() db.close() print(is_open) self.assertEqual(is_open[0][0], 2) except NoSuchElementException as e: logger.error("没有发现元素:" + str(e)) except AssertionError as e: logger.error("测试结果错误:" + str(e)) raise AssertionError
def test_case(self, data): name, jobnumber, phone = tuple(data) self.driver.implicitly_wait(10) try: self.driver.switch_to.frame("content") self.driver.find_element_by_xpath( '''//*[@id="TheSix"]/div[2]/img''').click() time.sleep(1) self.driver.find_element_by_xpath( '''//*[@id="list"]/div[1]/button[1]''').click() self.driver.find_element_by_xpath( '''//*[@id="username"]''').send_keys(name) self.driver.find_element_by_xpath( '''//*[@id="jobNumber"]''').send_keys(jobnumber) self.driver.find_element_by_xpath( '''//*[@id="phone"]''').send_keys(phone) self.driver.find_element_by_xpath('''//*[@id="tijiao"]''').click() self.assertIn("导入员工", self.driver.page_source) except NoSuchElementException as e: logger.error("没有发现元素" + str(traceback.format_exc())) except AssertionError as e: logger.error("添加失败") else: logger.info("添加成功")
from LogCommon import logger logger = logger.mylog('test').getlog() try: logger.debug("这是debug信息") logger.info('开始测试...') r = 10 / 0 logger.info('result:', r) except ZeroDivisionError as e: logger.error('tests', exc_info=1) logger.info('end')