Robotium API 翻译(五)——web控件测试方法,WebElement和By类
2014-02-13 11:53
477 查看
在Robotium中,可以通过id、index、控件名称或者对应的text正则表达式来定位一个本地的控件。那么对于外部的web控件来说,在你的程序里面无法像本地控件一样找到id、index什么的,怎么来定位呢?
其实Robotium提供了定位web控件的类和方法,分别在By和Solo里面。其中By是专门用来定位web属性的,Solo里面部分方法是可以用于web控件的,部分方法是专门为了web控件设置的。
大家可以使用PC端的浏览器模拟User-Agent为android手机的方式,来查看web控件的属性。Chrome有自带的User-Agent模拟,FireFox需要下载插件才可以。我就是用的FireFox的插件,如下图:
![](https://oscdn.geek-share.com/Uploads/Images/Content/201403/6bc2506fd4825ee31b986fb836b8bf4f)
By里面的方法介绍
By id(String id)
通过id来定位一个WebElement,最常用的方法,一般来说是首选。
参数:
id-WebElement的id
返回值
By-定位的WebElement
By name(String name)
通过name来定位一个WebElement,也很常用。
参数:
name-WebElement的name
返回值
By-定位的WebElement
By className(String ClassName)
通过className来定位一个WebElement,也很常用。
参数:
className-WebElement的className
返回值
By-定位的WebElement
By tagName(String tagName)
通过tagName来定位一个WebElement,也很常用。
参数:
tagName-WebElement的tagName
返回值
By-定位的WebElement
By xpath(String xpath)
通过xpath来定位一个WebElement,定位搜索结果的不二选择。
参数:
xpath-WebElement的xpath
返回值
By-定位的WebElement
By cssSelector(String sccSelector)
通过cssSelector来定位一个WebElement。据说是速度最快的识别方法,但我对css没那么熟,不经常使用。
参数:
cssSelector-WebElement的cssSelector
返回值
By-定位的WebElement
By textContent(String [b]textContent)[/b]
通过textContent来定位一个WebElement,可以使用正则表达式。据说是最慢的识别方式,不推荐使用。
参数:
textContent-WebElement的textContent
返回值
By-定位的WebElement
String getValue()
返回属性的值
参数:
无
返回值:
该属性的值
Solo里面的WebElement专有方法,由于前面已经有介绍,在此不加赘述了
void clearTextInWebElement (By by)
清除WebElement中的值
void clickOnWebElement(By by [, int match, boolean scroll])
点击WebElement,通过web属性识别
void clickOnWebElement(WebElement webElement)
点击WebElement,通过webElement名称识别
void enterTextInWebElement (By by, String Text)
输入内容,通过web属性识别webElement
void typeTextInWebElement (By by | WebElement webElement , String Text [, int match])
输入内容,通过web属性识别webElement
ArrayList<WebElement> getCurrentWebElements([By by])
获取所有WebElement,并存到ArrayList中
WebElement getWebElement (By by, int index)
获取WebElement,通过web属性识别
boolean waitForWebElement (By by [, int minimumNumberOfMatches, int timeout , boolean scroll})
等待WebElement响应
Solo里面的WebElement和Native控件共有的方法
void clickOnText(String text [, int match , boolean scroll])
点击文字,通过text识别,支持正则表达式
void clickLongOnText(String text [, int match , int time|boolean scroll])
长按文字,通过text识别,支持正则表达式
其实Robotium提供了定位web控件的类和方法,分别在By和Solo里面。其中By是专门用来定位web属性的,Solo里面部分方法是可以用于web控件的,部分方法是专门为了web控件设置的。
大家可以使用PC端的浏览器模拟User-Agent为android手机的方式,来查看web控件的属性。Chrome有自带的User-Agent模拟,FireFox需要下载插件才可以。我就是用的FireFox的插件,如下图:
By里面的方法介绍
By id(String id)
通过id来定位一个WebElement,最常用的方法,一般来说是首选。
参数:
id-WebElement的id
返回值
By-定位的WebElement
By name(String name)
通过name来定位一个WebElement,也很常用。
参数:
name-WebElement的name
返回值
By-定位的WebElement
By className(String ClassName)
通过className来定位一个WebElement,也很常用。
参数:
className-WebElement的className
返回值
By-定位的WebElement
By tagName(String tagName)
通过tagName来定位一个WebElement,也很常用。
参数:
tagName-WebElement的tagName
返回值
By-定位的WebElement
By xpath(String xpath)
通过xpath来定位一个WebElement,定位搜索结果的不二选择。
参数:
xpath-WebElement的xpath
返回值
By-定位的WebElement
By cssSelector(String sccSelector)
通过cssSelector来定位一个WebElement。据说是速度最快的识别方法,但我对css没那么熟,不经常使用。
参数:
cssSelector-WebElement的cssSelector
返回值
By-定位的WebElement
By textContent(String [b]textContent)[/b]
通过textContent来定位一个WebElement,可以使用正则表达式。据说是最慢的识别方式,不推荐使用。
参数:
textContent-WebElement的textContent
返回值
By-定位的WebElement
String getValue()
返回属性的值
参数:
无
返回值:
该属性的值
Solo里面的WebElement专有方法,由于前面已经有介绍,在此不加赘述了
void clearTextInWebElement (By by)
清除WebElement中的值
void clickOnWebElement(By by [, int match, boolean scroll])
点击WebElement,通过web属性识别
void clickOnWebElement(WebElement webElement)
点击WebElement,通过webElement名称识别
void enterTextInWebElement (By by, String Text)
输入内容,通过web属性识别webElement
void typeTextInWebElement (By by | WebElement webElement , String Text [, int match])
输入内容,通过web属性识别webElement
ArrayList<WebElement> getCurrentWebElements([By by])
获取所有WebElement,并存到ArrayList中
WebElement getWebElement (By by, int index)
获取WebElement,通过web属性识别
boolean waitForWebElement (By by [, int minimumNumberOfMatches, int timeout , boolean scroll})
等待WebElement响应
Solo里面的WebElement和Native控件共有的方法
void clickOnText(String text [, int match , boolean scroll])
点击文字,通过text识别,支持正则表达式
void clickLongOnText(String text [, int match , int time|boolean scroll])
长按文字,通过text识别,支持正则表达式
相关文章推荐
- Ubuntu + Apache 403 错误
- 通过安装samba服务器可以实现在linux ubuntu与windows之间共享文件
- 参数化测试--sheet表的应用
- 使用HTML 5/CSS3五步快速制作便签贴特效
- 使用Go客户端访问MongoDB
- SDUT 数据结构上机实验之二分查找
- Java设计模式-代理模式
- 迷宫
- omap4 linux中断
- error: expected ‘)’ before ‘PRId64’
- grunt让Nodejs规范起来
- 工厂方法(Factory Method)模式
- 源码推荐(1月20日):加载解析RSS的Objective-C框架 Pininterest 弹出菜单的方式
- 类簇——Class Cluster
- python - __str__ 和 __repr__
- 获取 未安装包 版本号 和已安装包版本号
- 简单工厂(Simple Factory)模式
- the SQL statement is closed和 the cursor is closed,两个关闭的概念不一样
- LeetCode--online portal for IT interview
- java多线程总结