def mouse_pull(self): self.driver = HomePage.getDriver() try: js = "var q=document.documentElement.scrollTop=10000" self.driver.execute_script(js) except: raise u"鼠标下滑失败"
def testEnquiry(self): u"""询价""" #打开首页并且登陆账号 HomePage.HomePage(self) operate.sleep(2) operate.move_click("linktext",u"场景合作") operate.element_click("xpath","//a[@href='/landing/shop.html']") self.driver= HomePage.getDriver() #立即进入 operate.element_click("linktext",u"立即进入") operate.sleep(2) #断言是否进入到首页 try: self.assertEqual("http://test.spb.riskeys.com/shop/index.html", self.driver.current_url, "page is error") except : log.debug(u"进入首页失败") #登陆 # self.login() #进入我要询价 # 将页面下来,再次询价 # operate.mouse_pull() operate.element_click("id","begin-buy") operate.sleep(2) #调用ask方法,进行询价 QueryPrice.ask(self)
def test_Pay(self): u"""支付""" #判断是否是支付页面 operate.sleep(2) self.driver= HomePage.getDriver() element=operate.getelement("xpath","//a[@class='logo']") try: if element.is_displayed()==True: #页面加载出来 operate.sleep(1) operate.element_click("xpath",".//div[@title='模拟网银']") #确认网银支付 operate.element_click("xpath",".//div[@class='action']/a") result=operate.getelement("tagname","h3") #如果支付失败:则print出原因 if result.is_displayed()==True: reason=operate.getelement("tagname","p") print(result.text) print(reason.text) operate.screenshot("orderpay") #关不标签页 HomePage.closePage() else: HomePage.closePage() else: log.debug(u"进入支付页面失败") operate.screenshot("payPage") HomePage.closePage() except: log.warning(u"支付失败")
def move_click(self, type, str): self.driver = HomePage.getDriver() try: ele1 = self.getelement(type, str) ActionChains(self.driver).move_to_element(ele1).perform() except WebDriverException as e: log.debug(u"WebDriver,error", e) self.screenshot("WebDriverError") except TimeoutException as e: log.error(u"超时", e) self.screenshot("TimeException") except NoSuchElementException as e: log.debug(u"元素异常", e) self.screenshot("NoSuchElementException")
def clear_input(self, str): self.driver = HomePage.getDriver() try: element = WebDriverWait( self.driver, 10).until(lambda driver: self.driver.find_element_by_id(str)) element.clear() except WebDriverException as e: log.debug(u"WebDriver,error", e) self.screenshot("WebDriverError") except TimeoutException as e: log.error(u"超时", e) self.screenshot("TimeException") except NoSuchElementException as e: log.debug(u"元素异常", e) self.screenshot("NoSuchElementException")
def test_Insure(self): u"""开始投保""" #默认选择企业 #输入投保机构,联系人,手机号码 insure_dict={'shopName':'TaiPingYang','contacts':'testone','insurer-phone':'18207278423'} operate.dict_input("id",insure_dict) #传入员工名单 #获取excel路径 fpath=operate.read_file("testData","mingdan.xlsx") operate.element_input("id","form-field-attach",fpath) operate.sleep(2) #鼠标下滑到最下面 operate.mouse_pull() #选择发票类型 xpath="//input[@type='radio' and @value='normal']" operate.element_click("xpath",xpath) dict_receiver={"receiverName":"Hanxiao","receiverTel":"18207278423","receiverPost":"200000"} operate.dict_input("id",dict_receiver) operate.element_input("id","receiverAddress",u"上海市浦东新区") #确认投保 operate.mouse_pull() check_radio="//div[@class='button-box']/div[@class='notice']/span[@class='my-check']" operate.element_click("xpath",check_radio) operate.element_click("id","buy-btn") #确认投保信息,并判断是否是投保页面,是则确认投保,不是则刷新页面 operate.sleep(2) try: #判断"投保确认"元素是否存在于页面 text= HomePage.getDriver().find_element_by_xpath("//div[@class='block-title']") if(text.is_displayed()==True): if(text.text==u'确认投保'): print(u"成功进入投保确认页面") else: operate.refresh() else: operate.refresh() except: log.debug(u"页面元素未找到") #确认页面之后确认投保 operate.mouse_pull() operate.is_Selected("xpath","//div[@class='button-box']/div[@class='notice']/span[@class='my-check']") operate.element_click("id","buy-btn") self.login() operate.element_click("id","buy-btn") operate.sleep(3)
def dict_select(self, dict): self.driver = HomePage.getDriver() try: for k, v in dict.items(): element = WebDriverWait( self.driver, 20, 0.5).until(lambda driver: driver.find_element_by_id(k)) Select(element).select_by_index(v) time.sleep(1.5) except WebDriverException as e: log.debug(u"WebDriver,error", e) self.screenshot("WebDriverError") except TimeoutException as e: log.error(u"超时", e) self.screenshot("TimeException") except NoSuchElementException as e: log.debug(u"元素异常", e) self.screenshot("NoSuchElementException")
def elements_list(self, type, str): self.driver = HomePage.getDriver() try: if type == "id": ele_sum = WebDriverWait(self.driver, 10).until( lambda driver: self.driver.find_elements_by_id(str)) elif type == "classname": ele_sum = WebDriverWait( self.driver, 10).until(lambda driver: self.driver. find_elements_by_class_name(str)) else: ele_sum = WebDriverWait(self.driver, 20, 0.5).until( lambda driver: driver.find_elements_by_xpath(str)) return ele_sum except WebDriverException as e: log.debug(u"WebDriver,error", e) self.screenshot("WebDriverError") except TimeoutException as e: log.error(u"超时", e) self.screenshot("TimeException") except NoSuchElementException as e: log.debug(u"元素异常", e) self.screenshot("NoSuchElementException")
def move_to_element(self, str1, str2): self.driver = HomePage.getDriver() try: #定位到鼠标移动到上面的元素 element1 = WebDriverWait( self.driver, 10).until(lambda driver: driver.find_element_by_xpath(str1)) element2 = WebDriverWait( self.driver, 10).until(lambda driver: driver.find_element_by_xpath(str2)) #鼠标悬停在该元素 ActionChains(self.driver).move_to_element(element1).perform() time.sleep(2) ActionChains(self.driver).move_to_element(element2).perform() element2.click() except WebDriverException as e: log.debug(u"WebDriver,error", e) self.screenshot("WebDriverError") except TimeoutException as e: log.error(u"超时", e) self.screenshot("TimeException") except NoSuchElementException as e: log.debug(u"元素异常", e) self.screenshot("NoSuchElementException")
def element_selected(self, type, str, input): self.driver = HomePage.getDriver() try: if type == 'index': element = WebDriverWait(self.driver, 20, 0.5).until( lambda driver: driver.find_element_by_xpath(str)) Select(element).select_by_index(input) elif type == 'value': element = WebDriverWait(self.driver, 20, 0.5).until( lambda driver: driver.find_element_by_xpath(str)) Select(element).select_by_value(input) elif type == 'visibletext': element = WebDriverWait(self.driver, 20, 0.5).until( lambda driver: driver.find_element_by_xpath(str)) Select(element).select_by_visible_text(input) except WebDriverException as e: log.debug(u"WebDriver,error", e) self.screenshot("WebDriverError") except TimeoutException as e: log.error(u"超时", e) self.screenshot("TimeException") except NoSuchElementException as e: log.debug(u"元素异常", e) self.screenshot("NoSuchElementException")
def screenshot(self, filename): self.driver = HomePage.getDriver() print(files) self.driver.get_screenshot_as_file( os.path.join(files, 'screenshot', filename + now + '.png'))