首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Ui功能自动化测试:重新封装 Webdriver Api

selenium 做自动化测试过程中发现,有些元素定位比较麻烦,有些元素定位写脚本特别的繁琐,所以重新封装了下Api 。

* find 寻找单个元素

* find_elements 寻找多个元素

* clear_keys 清除按钮

* send_keys 输入

* click_button 点击按钮

* script 输入js脚本

* wait 智能等待

* set_window 设置window大小

def find(self,loc):

? ? ? '''

? ? ? find element.

? ? ? Usage:

? ? ? driver.find((By.XPATH,"//a"))

? ? ? '''

? ? ? try:

? ? ? ? ?WebDriverWait(self.driver,self.timeout).until(lambda drive:drive.find_element(*loc).is_displayed())

? ? ? except Exception,e:

? ? ? ? ? print u"%s 页面中超时%ds未能找到 %s 元素%s" %(self,self.timeout,loc,e)

? def find_elements(self,loc):

? ? ? '''

? ? ? find elements.

? ? ? Usage:

? ? ? driver.find_elements((By.XPATH,"//a"))

? ? ? '''

? ? ? try:

? ? ? ? ? WebDriverWait(self.driver,self.timeout).until(lambda drive:drive.find_elements(*loc).is_displayed())

? ? ? except Exception,e:

? ? ? ? ? print u"%s 页面中超时%ds未能找到 %s 元素%s" %(self,self.timeout,loc,e)

? def click_keys(self,loc):

? ? ? '''

? ? ? click keys

? ? ? Usage:

? ? ? driver.click_keys((By.XPATH,"//a"))

? ? ? '''

? ? ? self.find(loc).click()

? def clear_keys(self,loc):

? ? ? '''

? ? ? clear keys

? ? ? Usage:

? ? ? driver.clear_keys((By.XPATH,"//a"))

? ? ? '''

? ? ? self.find(loc).clear()

? def send_keys(self,loc,value):

? ? ? '''

? ? ? send keys

? ? ? Usage:

? ? ? send_keys((By.XPATH,"//a"),'a')

? ? ? '''

? ? ? sleep(3)

? ? ? self.clear_keys(loc)

? ? ? self.find(loc).send_keys(value)

? def click_button(self,loc):

? ? ? '''

? ? ? click button

? ? ? Usage:

? ? ? click_button((By.XPATH,"//a"))

? ? ? '''

? ? ? sleep(3)

? ? ? self.find(loc).click()

? def script(self,src):

? ? ? '''

? ? ? execute_script

? ? ? Usage:

? ? ? script(src)

? ? ? '''

? def wait(self, secs):

? ? ? '''

? ? ? Implicitly wait.All elements on the page.

? ? ? Usage:

? ? ? driver.wait(10)

? ? ? '''

? def set_window(self, wide, high):

? ? ? '''

? ? ? Set browser window wide and high.

? ? ? Usage:

? ? ? driver.set_window(wide,high)

? ? ? '''

? def right_click(self, loc):

? ? ? '''

? ? ? Right click element.

? ? ? Usage:

? ? ? driver.right_click((By.XPATH,"//a"))

? ? ? '''

? ? ? el = self.find(loc)

? ? ? ActionChains(self.driver).context_click(el).perform()

? def move_to_element(self, loc):

? ? ? '''

? ? ? Mouse over the element.

? ? ? Usage:

? ? ? driver.move_to_element((By.XPATH,"//a"))

? ? ? '''

? ? ? el = self.find(loc)

? ? ? ActionChains(self.driver).move_to_element(el).perform()

? def double_click(self, loc):

? ? ? '''

? ? ? Double click element.

? ? ? Usage:

? ? ? driver.double_click((By.XPATH,"//a"))

? ? ? '''

? ? ? el = self.find(loc)

? ? ? ActionChains(self.driver).double_click(el).perform()

? def drag_and_drop(self, loc1, loc2):

? ? ? '''

? ? ? Drags an element a certain distance and then drops it.

? ? ? Usage:

? ? ? driver.drag_and_drop((By.XPATH,"//a"),(By.XPATH,"//b"))

? ? ? '''

? ? ? element? = self.find(loc1)

? ? ? target? = self.find(loc2)

? ? ? ActionChains(self.driver).drag_and_drop(element , target).perform()

? def get_display(self, loc):

? ? ? '''

? ? ? Gets the element to display,The return result is true or false.

? ? ? Usage:

? ? ? driver.get_display(By.XPATH,"//a")

? ? ? '''

? ? ? el = self.find(loc)

? ? ? return el.is_displayed()

? def isElement(self,identifyBy,c):

? ? ? '''

? ? ? Determine whether elements exist

? ? ? Usage:

? ? ? isElement(By.XPATH,"//a")

? ? ? '''

? ? ? sleep(1)

? ? ? flag=None

? ? ? try:

? ? ? ? ? if identifyBy == "id":

? ? ? ? ? elif identifyBy == "xpath":

? ? ? ? ? elif identifyBy == "class":

? ? ? ? ? elif identifyBy == "link text":

? ? ? ? ? elif identifyBy == "partial link text":

? ? ? ? ? elif identifyBy == "name":

? ? ? ? ? elif identifyBy == "tag name":

? ? ? ? ? elif identifyBy == "css selector":

? ? ? ? ? flag = True

? ? ? except NoSuchElementException,e:

? ? ? ? ? flag = False

? ? ? finally:

? ? ? ? ? return flag

? def refresh(self):

? ? ? '''

? ? ? refresh? page

? ? ? Usage:

? ? ? driver.refresh

? ? ? '''

? def list_to_str(self,string):

? ? ? '''

? ? ? string to unicode

? ? ? Usage:

? ? ? list_to_str(str)

? ? ? '''

? ? ? str_symptom = str(string).replace('u\'','\'')

? ? ? return str_symptom.decode("unicode-escape")

? def get_text(self,loc):

? ? ? '''

? ? ? get text

? ? ? Usage:

? ? ? get_text(By.XPATH,"//a")

? ? ? '''

? ? ? sleep(3)

? ? ? return self.find(loc).text

? def get_url(self,url):

? ? ? '''

? ? ? get url

? ? ? Usage:

? ? ? get_url(url)

? ? ? '''

? ? ? sleep(3)

? def get_title(self,url):

? ? ? '''

? ? ? get title

? ? ? Usage:

? ? ? get_title(url):

? ? ? '''

* right_click 右击

* move_to_element 移动到某个元素

* double_click 双击

* drag_and_drop 拖动

* get_display 获取元素显示属性

* isElement 判断元素是否存在

* refresh 刷新页面

* list_to_str str转换到unicode

* get_text 获取元素text

* get_url 切换url

* get_title 获取当前title

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20200614A0BPJ600?refer=cp_1026
  • 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码

添加站长 进交流群

领取专属 10元无门槛券

私享最新 技术干货

扫码加入开发者社群
领券
http://www.vxiaotou.com