Tellurium Tutorial Series: Groovy or Java

16 views
Skip to first unread message

John.Ji...@gmail.com

unread,
Oct 9, 2008, 10:56:51 PM10/9/08
to tellurium-users
One of the obstacles for users to use Tellurium is that Tellurium uses
Groovy to define UI module. Some users may be scared by the usage of
Groovy since Groovy is not so popular at the current stage. In fact,
that is really some misunderstand and you can simply use Java syntax
to write your methods in the UI module class. The only exception is
that the UI definitions such as

ui.Container(uid: "GoogleBooksList", clocator: [tag: "table",
id: "hp_table"], group: "true"){
TextBox(uid: "category", clocator: [tag: "div", class:
"sub_cat_title"])
List(uid: "subcategory", clocator: [tag: "div",
class:"sub_cat_section"], separator: "p"){
UrlLink(uid: "all", locator: "/a")
}
}

ui.Container(uid: "NewGoogleBooksList", clocator: [tag:
"table", id: "hp_table"], group: "true"){
TextBox(uid: "category", clocator: [tag: "div", class:
"sub_cat_title"])
List(uid: "subcategory", clocator: [tag: "div",
class:"sub_cat_section"]){
Container(uid: "all", clocator: [tag: "p"]){
UrlLink(uid: "link", clocator: [:])
}
}
}

You have to write it in that way. Other than that, you can write the
rest in Java syntax.
For example, for the methods in the NewGoogleBooksList.groovy look as
follows,

String getCategory(){
getText "GoogleBooksList.category"
}

int getListSize(){
getListSize "GoogleBooksList.subcategory"
}

public UiObject getUiObject(String uid){
return getUiElement(uid)
}

def getAllObjectInList(){
int size = getListSize()
List list = new ArrayList()
for(int i=1; i<=size; i++){
list.add(getUiElement("GoogleBooksList.subcategory[${i}]"))
}

return list
}

def clickList(int index){
click "GoogleBooksList.subcategory[${index}]"
waitForPageToLoad 30000
}

def clickNewList(int index){
click "NewGoogleBooksList.subcategory[${index}].link"
waitForPageToLoad 30000
}

String getText(int index){
getText "GoogleBooksList.subcategory[${index}]"
}

You can write them in Java syntax as follows,

public String getCategory(){
return getText("GoogleBooksList.category");
}

public int getListSize(){
return getListSize("GoogleBooksList.subcategory");
}

public UiObject getUiObject(String uid){
return getUiElement(uid);
}

public List getAllObjectInList(){
int size = getListSize();
List list = new ArrayList();
for(int i=1; i<=size; i++){
list.add(getUiElement("GoogleBooksList.subcategory[" + i +
"]"));
}

return list;
}

public void clickList(int index){
click("GoogleBooksList.subcategory[" + index + "]");
waitForPageToLoad(30000);
}

public void clickNewList(int index){
click("NewGoogleBooksList.subcategory[" + index + "].link");
waitForPageToLoad(30000);
}

public String getText(int index){
return getText("GoogleBooksList.subcategory[" + index + "]");
}

You may noticed that the differences when use Java syntax include

1) You cannot omit ";" at the end of the line
2) You cannot omit () for methods
3) You cannot omit return for non-void method
4) You cannot use GString like GoogleBooksList.subcategory[${index}],
must use String concatenation.
5) You cannot omit "public" for methods
6) You should not use optional type "def", you have to specify the
type in Java syntax.

The rest are very much the same.

From the above, you can see that you can totally write the UI module
class in Java syntax. But be aware that
you still need to create the class as a Groovy class, i.e.,
"xxx.groovy", not "xxx.java". Because the UI parser must use Groovy
feature.

However, if you are familiar with Groovy, you are encouraged to use
Groovy directly.

Thanks.
Reply all
Reply to author
Forward
0 new messages