Appium之Hybrid App自动化

appium的原理:针对于hybrid的app,appium基于selendroid框架实现。而selendroid框架又是基于instrumentation框架实现的 可见,appium本身是借助于其它框架控制app

元素识别工具 inspector:

  chrome inspector for selendroid

  UIautomatorviewer for UIautomator

自动化脚本实现:在页面里搜索一个关键词,并验证和预期一致

Appium的配置、启动

配置:Automation Name:选择selendroid

配置界面第1行:App Path:通过文件路径选择被测试的包

代码示例:

#coding:utf8
import time,unittest
from appium import webdriver

class MyTestCase(unittest.TestCase):
    def setUp(self):
        #定义初始化的属性信息,这个和Native APP测试的区别是多了1个“automationName”
        self.desired_caps={}
        self.desired_caps['platformName']=''#指定平台
        self.desired_caps['platformVersion']=''#和Appium里设置的一样
        self.desired_caps['deviceName']=''#指定需要控制的设备,在控制台中输入adb devices 就会出现deviceName
        self.desired_caps['appPackage']=''#被测试程序的packageName,在控制台中输入adb logcat | grep(findstr) START
        self.desired_caps['appActivity']=''#packageName中/后面的就是这个
        self.desired_caps['unicodeKeyboard']='True'#更改测试机的输入法
        self.desired_caps['resetKeyboard']='True'#将更改后的输入法还原为机器原来的输入法,值为false则不还原
        self.desired_caps['automationName']='Selendroid'# 相对于native_app多了这一个配置项
        #获得操作程序的句柄,用Remote实现
        #4723可以在启动APPium时,看到
        self.driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
        
    def test_Search(self):
        #Locate定位输入框
        input=self.driver.find_element_by_id('url')
        #Operate操作元素
        input.send_keys('http://wap.sougou.com')
        searchbutton=self.driver.find_element_by_id('searchbutton')
        searchbutton.click()
        time.sleep(5)
        
        # Switch 切换上下文,由于此时界面变成了一个HTML5页面,所以需要切换
        # 打印出当前上下文所包含的内容,结果:[u'NATIVE_APP',u'WEBVIEW_0']
        print self.driver.contexts
        self.driver.switch_to.context('WEBVIEW_0')#切换到WEBVIEW,切换后需要用WEBVIEW的定位方式,和WEB系统定位是一样的
        print self.driver.current_context#打印结果:WEBVIEW_0
        time.sleep(5)
        
        #定为WEB输入框
        webinput=self.driver.find_element_by_xpath('balba')
        webinput.click()
        webinput.send_keys('mooc')
        websearchbutton=self.driver.find_element_by_xpath('sldkflj')
        websearchbutton.click()
        time.sleep(5)
        
        #检验查询结果
        #验证第一条结果显示了
        firstresult=self.driver.find_element_by_xpath('balabala')
        print firstresult
        self.assertTrue(u'中国大学' in firstresult.text) # 验证“中国大学”这几个字包含在firstresult.text里面
        #切换会NATIVE状态下
        self.driver.switch_to.context('NATIVE_APP')
        
    def tearDown(self):
        self.driver.quit()

*** 常用API介绍 ***

contexts        输出当前上下文,用于切换操作对象

switch_to.context()   入参是contexts返回的其中一个值,用于选择操作对象

assertTrue(“” in “”)

current_context    print self.driver.current_context 打印当前操作对象

*** END

原文地址:https://www.cnblogs.com/liuyun66535309/p/8729416.html