[b]一.概述[/b]
在Tellurium中,所有的页面元素和方法都会写在groovy文件中,一个完整的groovy文件可以是这个样子的,以google首页为例:
public class GoogleHomeModule extends DslContext{
public void defineGoogleHomeModule(){
ui.Container(uid: "root", clocator: [tag: "center"]){
InputBox(uid: "input", clocator: [name: "q"])
SubmitButton(uid: "gSearch", clocator: [name: "btnG"])
SubmitButton(uid: "lucky", clocator: [name: "btnI"])
Image(uid: "logo", clocator: [id: "logo"])
}
}
public void doGoogleSearch(String input){
pause TelluriumConstant.PAUSELOW
type "root.input", input
pause TelluriumConstant.PAUSELOW
click "root.gSearch"
waitForPageToLoad TelluriumConstant.WAIT
}
}
其中ui.Container这部分是我们可以做优化的地方
[b]二.优化[/b]
首先,直接用Tellurium IDE产生出我们需要的代码,最初工具产生的代码是这样的:
ui.Container(uid: "Table", clocator: [tag: "table"]){
SubmitButton(uid: "Google", clocator: [tag: "input", type: "submit", class: "lsb", value: "Google 搜索", name: "btnG"])
Container(uid: "Section", clocator: [tag: "tr"]){
Container(uid: "Part", clocator: [tag: "td", direct: "true"]){
InputBox(uid: "Q", clocator: [tag: "input", title: "Google 搜索", class: "lst", name: "q"])
SubmitButton(uid: "Input", clocator: [tag: "input", type: "submit", class: "lsb", value: " 手气不错 ", name: "btnI"])
}
}
}
可以看到以上的Container中,我们所需要的InputBox在最底层,上面有3个父节点.如果要做一个搜索操作的话,写出来的代码会是这样:
type "Table.Selection.Part.Q", "james"
click "Table.Google"
这样的话,一个操作中对页面元素的依赖性很高,不容易维护.由于Tellurium也是(可以)用Xpath去解析一个元素,所以,比较理想的状况是这样去操作元素:
type "Table.Q", "james"
click "Table.Google"
所以,优化过的Container最好是这个样子:
ui.Container(uid: "Table", clocator: [tag: "Table"]){
InputBox(uid: "Q", clocator: [name: "q"])
SubmitButton(uid: "Google", clocator: [name: "btnG"])
SubmitButton(uid: "lucky", clocator: [name: "btnI"])
Image(uid: "logo", clocator: [id: "logo"])
}
在以上代码中,将所有中间的Container,多余的元素属性,通通删除掉.让我们对所需要的元素的寻找,仅仅依赖于该元素的name,以及最上层Container的属性(tag,name).
[b]三.总结[/b]
跟Selenium一样,我们的目标是要将页面元素的依赖性降低,最好的结果是仅仅依赖于元素的ID或者Name