appium+ios+python第一个例子

来源:互联网 发布:阵面网络和云母组关系 编辑:程序博客网 时间:2024/05/14 08:57

配置好appium环境后的第一个例子,大概流程是:

1、点击分享按钮后,弹出授权页面,授权页面是个新浪的webview

2、然后输入用户名、密码,点击登录,授权成功跳回到原应用

3、点击分享


很简单的操作,但这里面涉及到一个webview,对于初学者来说,纠结了半天,下面跟大家分享下,主要讲webview。

在一个app里用到webview算是混合应用了,appium无法直接和webview互相调用,所以有个切换模式的概念,只有切换到

webview之后才能获取到webview上的元素,而且inspector提供了定位元素的方法,但是它定位webview的元素的xpath是没法

用的,因为它定位的是在Native context下的,但是我们要用的是webview context下的xpath;

重要的是当完成webview上操作后,我们要记得切换到native context,才能继续定位原应用的元素进行操作;

http://testerhome.com/topics/513 该链接里介绍了如何用Safari来查看webview的源码,从而用xpath定位元素;

http://www.w3school.com.cn/xpath/xpath_syntax.asp 该链接是xpath的语法,就是如何根据html使用xpath定位元素;


下面解释为啥有切换的概念以及如何切换,我就不做翻译了,直接贴出来了,下面的代码中我们可以 print driver.contexts 看看都有哪些context,且每个context的名字

下面代码链接:https://github.com/appium/python-client(搜webview就能看到相应代码)

Switching between 'Native' and 'Webview'

For mobile testing the Selnium methods for switching between windows was previously commandeered for switching between native applications and webview contexts. Methods explicitly for this have been added to the Selenium 3 specification, so moving forward these 'context' methods are to be used.

To get the current context, rather than calling driver.current_window_handle you use

current = driver.current_context

The available contexts are not retrieved using driver.window_handles but with

driver.contexts

Finally, to switch to a new context, rather than driver.switch_to.window(name), use the comparable context method

context_name = "WEBVIEW_1"driver.switch_to.context(context_name)

最后附上一小段代码

self.driver.find_element_by_name('分享样式1').click()        self.driver.find_element_by_name('UMS sina icon').click()        sleep(2)        print self.driver.contexts        context_name = 'WEBVIEW_1'        self.driver.switch_to.context(context_name)        sleep(2)        self.driver.find_elements_by_xpath('//input[@id="userId"]')[0].send_keys('test@126.com')        self.driver.find_elements_by_xpath('//input[@id="passwd"]')[0].send_keys('test')        self.driver.find_elements_by_xpath('//a[@class="btnP"]')[0].click()        sleep(2)        self.driver.switch_to.context('NATIVE_APP')        self.driver.find_element_by_name('UMS nav button send').click()

0 0
原创粉丝点击