appium+ios+python第一个例子

本文介绍了一个使用Appium进行iOS自动化测试的例子,涉及点击分享按钮后出现的授权网页(WebView),讲解了如何在Native和WebView之间切换以进行元素定位。通过Safari查看WebView源码定位元素,并提供了相关XPath语法链接。文章还提到了在Appium中获取和切换上下文的方法,以确保在不同环境中正确操作。
摘要由CSDN通过智能技术生成

配置好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()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值