背景:

部分模块代码会出现重复使用的情况,为了解决代码重复编写,故将此模块代码直接生成一个模块(module),便于其他模块调用

Appium用例分模块

调用子模块的脚本语法:

From 包名.文件名 import方法1,方法2,方法

示例:将初始化脚本单独形成一个模块

步骤一:Capability.py文件的脚本

#!urs/bin/python

#!_*_ coding:UTF-8 _*_

from selenium.common.exceptions import NoSuchElementException

from appium import webdriver

desired_caps={}

desired_caps['platformName']='Android'

desired_caps['platformVersion']='4.4.2'

desired_caps['deviceName']='127.0.0.1:62001'

desired_caps['app']=r'C:\\Users\\JMC\\Desktop\\Study\\****.apk'    #r后面跟apk的存放路径

desired_caps['packageName']='此处填写包名'

desired_caps['appActivity']='com.********.wifipartner.activity.MainActivity'  #此处需要填写正确的activity

desired_caps['unicodekeyboard']='True'

desired_caps['resetkeyboard']='True'

 

步骤二:test.py文件脚本

#!urs/bin/python

#!_*_ coding:UTF-8 _*_

from find_element.capability import webdriver,NoSuchElementException,desired_caps   #调用capability.py文件内的方法

#检测按钮是否存在函数

def check_imagebutton():  #定义函数名称

    print("check_imagebutton"#输出文本简要说明函数的作用

    try:                        #为元素设置变量名

        imagebutton=driver.find_element_by_id('com.mydream.wifi:id/riv_head')

    except NoSuchElementException:   #若元素不存在,则调用捕获异常的方法

        print("没有这个元素")         #输出文本说明,元素不存在

    else:                            #若元素存在,则对元素执行相应的操作

        imagebutton.click()



driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)

driver.find_element_by_id('com.mydream.wifi:id/tvShare').click()  #点击头条新闻

driver.implicitly_wait(5#以秒为单位

driver.find_element_by_class_name('android.widget.ImageButton').click()  #点击头条新闻页面的返回按钮

driver.find_element_by_xpath("/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.FrameLayout/android.widget.FrameLayout/android.widget.LinearLayout/android.widget.TabHost/android.widget.LinearLayout/android.widget.TabWidget/android.widget.RelativeLayout[3]"

).click()  #点击我页面tab

driver.implicitly_wait(5#以秒为单位

#调用点击登陆入口的按钮的函数

check_imagebutton()

driver.implicitly_wait(10#以秒为单位

driver.find_element_by_id('com.mydream.wifi:id/title_third_login_button').click()  #点击更多按钮

driver.find_element_by_id('com.mydream.wifi:id/cacetMobile').send_keys("18059869253") #输入账号

driver.implicitly_wait(5#以秒为单位

driver.find_element_by_id('com.mydream.wifi:id/cacetPwd').send_keys("123456"#输入密码

driver.implicitly_wait(5#以秒为单位

driver.find_element_by_id('com.mydream.wifi:id/cbtnLogin').click()  #点击登陆按钮

步骤三:

在步骤一和二编辑完成后,在test.py文件页面,按ctrl+shift+F10执行代码即运行

相关文章:

  • 2021-10-01
  • 2021-12-25
  • 2021-11-23
  • 2022-02-01
  • 2022-12-23
  • 2021-11-10
  • 2021-08-21
  • 2022-01-14
猜你喜欢
  • 2022-12-23
  • 2021-12-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
相关资源
相似解决方案