Appium最常用的方法总结

发表于:2023-6-06 09:35

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:技术提高效率    来源:CSDN

  Appium常用到的方法
  1.click 点击
  element=driver.find_element_by_xpath('xxx')
  element.click()
  或者
  driver.find_element_by_xpath('xxx').click()
  2.输入键值 press_keycode
  driver.press_keycode(4)  #点击返回键
  除了press_keycode方法,还有其他方法可以输入键值,如keyevent()和long_press_keycode()
  keycode:键值,http://developer.android.com/reference/android/view/KeyEvent.html
  3.获取当前窗口大小get_window_size
     driver.get_window_size()[‘xx’] 
  width=driver.get_window_size()['width']  #获取屏幕的宽
  height=driver.get_window_size()['height']  #获取屏幕的高
  4.滑动 swip
  driver.swip(start_x,start_y,end_x,end_y,duration=None)
  按住A点(start_x,start_y)快速滑动至B点(end_x,end_y),参数是坐标值 duration表示移动的时间间隔。
  PS:以左上角为原点。
  5.启动另一个activity   start_activity()
  这个可以是启动同应用的另一个activity,也可以启动另一个应用的activity
  dirver.strat_activity(‘包名’,’activity名’)
  from appium import webdriver
  import time
  from selenium.common.exceptions import NoSuchElementException
  def android_driver():
      desired_cap={}
      desired_cap['platformName']="Android"
      desired_cap['platformVersion']='8.1.0'
      desired_cap['deviceName']='HONOR9X'
      desired_cap['udid']='5fb5c4cc'  #ip或者设备好
      desired_cap['appPackage']='com.ss.android.article.news'   #要启动的应用包名
      desired_cap['appActivity']='com.ss.android.article.news.searchIcon2'  #要启动的应用activity
      desired_cap['noReset']=True   #不需要重新登录
      driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)
   
      return driver
   
  if __name__=="__main__":
      driver=android_driver()
      time.sleep(10)
      # 跨应用,进入爱奇艺首页
      driver.start_activity('com.qiyi.video', 'com.qiyi.video.WelcomeActivity')
      time.sleep(10)
      # 同应用进入搜索
      driver.start_activity('com.ss.android.article.news', 'com.ss.android.article.base.feature.search.SearchActivity')
      time.sleep(10)
  6.截图 get_screenshot_as_file
  driver.get_screenshot_as_file(‘路径.png')
  driver.get_screenshot_as_file("1.png")  #保存到当前目录
  7.获取元素属性值
  element.text   #获取元素的text属性值
  element.content-desc  #获取元素content-desc属性值
  from appium import webdriver
  import time
  from selenium.common.exceptions import NoSuchElementException
  def android_driver():
      desired_cap={}
      desired_cap['platformName']="Android"
      desired_cap['platformVersion']='8.1.0'
      desired_cap['deviceName']='HONOR9X'
      desired_cap['udid']='5fb5c4cc'  #ip或者设备好
      desired_cap['appPackage']='com.ss.android.article.news'   #要启动的应用包名
      desired_cap['appActivity']='com.ss.android.article.news.searchIcon2'  #要启动的应用activity
      desired_cap['noReset']=True   #不需要重新登录
      driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)
   
      return driver
   
  if __name__=="__main__":
      driver=android_driver()
      time.sleep(10)
   
      try:
          element=driver.find_element_by_xpath('//*[@resource-id="com.ss.android.article.news:id/b8b"]')
      except NoSuchElementException:
          print("没有找到这个元素")
      else:
          print("已经找到这个元素了,提取他的text属性值")
          text_name=element.text
          print(text_name)  #输出首页
  8.查找元素使用智能等待 WebDriverWait
  element=WebDriverWait(driver,wait_num).until(lambda x:x.find_element_by_xpath(xxx))
  在查找元素时,如果页面还没有加载出来就去查找就会报没有找到元素的异常(NoSuchElementException),这时我们可以加入time.sleep()进行等待,等待过后再去查找元素,这样的就会导致自动化时间过长,每一步都要加一个等待。那么有没有一种比较智能的查找元素的方法呢,他能够智能的等待,如果找到元素了就直接返回,如果没有找到元素才等到,WebDriverWait可以实现这种功能。
  wait_num参数就是等待的最长时长,如果超过这个时长还没有找到元素,就会抛出一个超时的异常,如果找到了元素就直接返回,就不等待了,这样既能找到元素,又能节省时间。
  PS:在使用WebDriverWait之前,需要先导入
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.common.exceptions import TimeoutException
  from appium import webdriver
  import time
  from selenium.common.exceptions import NoSuchElementException
  from selenium.webdriver.support.ui import WebDriverWait
  from selenium.common.exceptions import TimeoutException
  def android_driver():
      desired_cap={}
      desired_cap['platformName']="Android"
      desired_cap['platformVersion']='8.1.0'
      desired_cap['deviceName']='HONOR9X'
      desired_cap['udid']='5fb5c4cc'  #ip或者设备好
      desired_cap['appPackage']='com.ss.android.article.news'   #要启动的应用包名
      desired_cap['appActivity']='com.ss.android.article.news.searchIcon2'  #要启动的应用activity
      desired_cap['noReset']=True   #不需要重新登录
      driver=webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_cap)
      
      return driver
   
  if __name__=="__main__":
      driver=android_driver()
      try:
          #element=driver.find_element_by_xpath('//*[@resource-id="com.ss.android.article.news:id/b8b"]')
          element=WebDriverWait(driver,10).until(lambda x:x.find_element_by_xpath('//*[@resource-id="com.ss.android.article.news:id/b8b"]'))
      except TimeoutException:
          print("超时了,没有找到这个元素")
      else:
          print("已经找到这个元素了,提取他的text属性值")
          text_name=element.text
          print(text_name)  #输出首页
  通过对比可以明显的发现时间缩短了,找到元素直接就返回。
  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号