Appium之Hybrid App自动化

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

元素识别工具 inspector:chrome

  chrome inspector for selendroid app

  UIautomatorviewer for UIautomator框架

 

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

Appium的配置、启动测试

配置:Automation Name:选择selendroidui

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

代码示例:spa

#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介绍 ***code

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

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

assertTrue(“” in “”)

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

 

*** END

相关文章
相关标签/搜索