Java Tea User Guide

User Manual:

Open the PDF directly: View PDF PDF.
Page Count: 75

DownloadJava Tea User Guide
Open PDF In BrowserView PDF
End-to-end Testing Framework

JavaTea
User Guide
Revision History
Version Date
0.0.1
5/1/2019

Author
Masayuki Otoshi

Description
Document Created

Table of Contents
1.

Introduction ...............................................................................................................................2

2.

Installation .................................................................................................................................4

3.

Getting Started ...........................................................................................................................5

4.

Examples ....................................................................................................................................9

5.

Tea Script Language Specifications ...........................................................................................30

6.

Preprocessor ............................................................................................................................39

7.

TeaBase defined variables and methods ..................................................................................39

8.

Custom Shift Methods ..............................................................................................................41

9.

Properties File – Multiple Languages .......................................................................................43

10.

Template Transformation .....................................................................................................45

11.

Debugging Tips .....................................................................................................................55

12.

Event Listener .......................................................................................................................58

13.

Command Usage ..................................................................................................................60

14.

Troubleshooting ...................................................................................................................61

15.

Pairwise Testing ....................................................................................................................64

16.

JavaTea as a Demonstration Tool .........................................................................................68

1. Introduction
Selenium is a tool widely used to code tests in test automation. It is very efficient to make sure
all functions work as we expected. However, in order to make our tests reusable and
maintainable, for example, applying Page Object Model (POM), some amount of programming
is required. With this approach, you need to create page classes and define properties that
represent elements to be displayed on target web page. This concept works fine while your
web application works stable. But, in real word, we need to continuously change the code to
enhance features and fix issues. The changes break existing tests and you need to spend time
to fix. Because of this, developers spends a lot of time to manage tests as well as application
code.
To reduce the cost of test automation, testing tool must be highly flexible and describable.
JavaTea is designed to capture web elements based on text strings shown on the web page to
provide an intuitive and easy way to point the target element.

0

First Name

John

-2

Last Name

Smith

-1

Email

your@email

Date of Birth

12

/

2

31

1
/

1911
4

3

Suppose we have the above web page and want to populate values in each input element.
Now you can get an element of the 'Email' label with the expression below:
'Email'
Tea script finds a text element from the page by using the text 'Email'. Also it allows you to
access other input elements around the text element by using the index number from the text.
Since we are on the Email label, the index number is now numbered as shown below:

John

Smith

Email

-2

-1

0

'Email'<<

'Email'<

your@email
1
'Email'>

12

31

1911

2

3

4

'Email'>>

2

To move the index, '>' and '<'operators can be used. The > moves the index to the right and <
operator moves to the left. Likewise, '>>' and '<<' operators move by two input elements to
the right and left, respectively.
If you want to enter an email address in the Email input box, you can describe the script using
the > operator:
'Email'> = 'your@email'
If you want to enter your date of birth (for example, December 31st, 1911), describe this:
'Email'>> = 12 31 1911
This expression tries to access the second input element on the right direction from the Email
label, which is the input box next to 'Date of Birth' label on the right. After the first value '12' is
entered, the index is automatically counted up and the index becomes 3, which is now points
to the second input box for the Date of Birth. So, the number '31' is populated into the second
input box, and the index is also counted up again. The last number '1911' is populated into the
third input box.
Next example is using < operator. If you want to enter first and last names (first name: John,
last name: Smith), use this expression:
'Email'< = 'Smith' 'John'
The '<' operator sets index to -1 from the current position, thus, the first value 'Smith' is set to
Last name input box. This time, the index is decreased and the value becomes '-2', which
points to the First name input box. Thus, the next value 'John' is populated into the First name
input box.
In order to show example to use < operator, I accessed starting from the Email label, however,
in real world, we usually gets the first label on the page and simply enter the values from the
top to bottom.
'First name'> = 'John' 'Smith'
'your@email' // Email
12 31 1911 // Date of birth
Or you can also specify label text for each element in order to make your script robustness for
future changes:
'First name'> = 'John' 'Smith'
'Email'> = 'your@email'
'Date of Birth'> = 12 31 1911
The tea script is described in Java code, and it is compiled as a Java class. Thus, you can easily
integrate existing other Java libraries. Also you can debug the Java code compiled from Tea
script using your favorite IDE.

3

JavaTea also supports pairwise testing with using 2 and 3-wise algorithm to reduce the
number of combinations. A test script created for a single test scenario can be easily extended
to the script for pairwise testing by adding possible values to each element.
For more details, please see chapter Pairwise Testing.

2. Installation
Dependencies
JavaTea requires the following software:





Java SE Version 8 or above
https://www.oracle.com/technetwork/java/javase/
Maven Version 3.1.6 or above
https://maven.apache.org/
Chrome browser
https://www.google.com/chrome/
ChromeDriver (WebDriver for Chrome)
http://chromedriver.chromium.org/

In this document, we assume that Maven and ChromeDriver are installed in the following
folder structure:

/ (Root)
├── apache-maven-3.6.1
│
└── ...
│
├── webdrivers
│
└── chromedriver.exe
└── ...

The apache-maven and webdrivers directories should be placed on your system PATH.
SET PATH=%PATH%;/apache-maven-3.6.1;/webdrivers

4

3. Getting Started
Download, Compile and Run
JavaTea samples are available to get from the site:
https://github.com/teafarm/javatea/tree/master/examples/
We here show one of easiest samples, GoogleSearchTest:
Step 1. Download the following files:
 pom.xml
https://github.com/teafarm/javatea/tree/master/examples/GoogleSearch/pom.xml


GoogleSearchTest.javat
https://github.com/teafarm/javatea/tree/master/examples/GoogleSearch/src/test/G
oogleSearchTest.javat

Store the files in the following folder structure:

. (Current)
├── pom.xml
├── src
│
└── test
│
└── javat
│
└── GoogleSearchTest.javat
└── ...

Step 2. Compile and Run tests
C:> SET PATH=/apache-maven-3.6.1/bin;/webdrivers;C:/Windows/System32
C:> mvn exec:java
C:> mvn test
A chrome browser will be opened and show a Google site. And a keyword search will be
executed automatically.

5

Basic Syntax
To understand basic syntax on JavaTea and Tea script, go back to previous chapter and see the
sample code.
import org.junit.jupiter.api.Test;
public class GoogleSearchTest extends tea.TeaBase {
@Test
public void test() {
createDriver('chrome');
driver.get('http://www.google.com');
#
'name:q' = 'Test Tool'
true
#
}
}
There are the following JavaTea specific rules:

Rule 1: A test class must inherit from tea.TeaBase class:
public class GoogleSearchTest extends tea.TeaBase {

Rule 2: createDriver() must be called with a browser name before starting to access target
web pages. Once you call this, a WebDriver object is created internally and available to use
through a property ‘driver’.
createDriver('chrome');

Rule 3: driver.get() must be called to display the target web page.
driver.get('http://www.google.com');

Rule 4: Tea script must be described between # and #.
#'name:q' = 'Test Tool'#
Multiple statements can be described between # and #.
#
'Name'> = 'John'
'Email'> = 'jon@email'
#
6

The closing # can be omitted if the script ends with ‘,’ or ‘)’.
assertEquals(#'NAME'@>, 'John', 'Name entered');
hideElement(#'NAME'@>);
If the script starts with ‘= #’ and ends with ‘;’, closing # can be also omitted.
TeaElement name = #'NAME'>;
If you want to invoke a method or access a property of the TeaElement, the method or
property name can be described by ending with ‘#’.
// Call getText method
String nameText = #'NAME'>#.getText();
// Access element property
builder.doubleClick(#'id:clickBtn'#.element).perform();

Rule 5: Java code in Tea script must be described between { and } or {% and %}.
#
'Name'> = 'John'
{ TeaElement emailInput = blurElement(#'Email'>); }
emailInput.'john@email'
#
Example 1: Switch to Tea script mode by # and #
private void login(String userId, String password) {
#
'User ID'> = userId password true
#
}
Example 2: Switch to Java mode by ( and )
assertEquals function is called in TeaScript mode (between # and #),
but the parameters are peocessed as Java code.
Hence, you need to use # notation to describe Tea script code (#'Name'>) in the parentheses.
The Tea script mode ends with ',' and the second and third parameters are treated as Java
String objects.
#
'Name'> = 'John'
assertEquals(#'Name'>, 'John', 'Name entered');
#

7

Mode switch between Java and Tea script code
In javat file, Tea script can be inserted in Java code. By default, described code is processed as
Java code, and code written between # and # is treated as Tea script. Even in the Tea script,
you can explicitly change back to Java mode by { } or {% %} notations.
Also, code between ( and ), [ and ] are also processed as Java.
Explicit mode switch: { }, {% %}
Implicit mode switch: ( ), [ ]
Those mode switches can be nested. For example, you can change back to Tea script mode
with # notation in a { and } as shown below:
public void test() {
createDriver('chrome');
driver.get(new java.io.File('../Wizard/Page1.html').toURI().toString());
#
Java mode
'Name'> = 'John'
'Name'>.balloonAndWait('Enter your name.');

Java mode

{TeaElement emailInput = blurElement(#'Email'>);}
'Email'> = 'john@email'

Tea script
mode

Tea script
'Next'.balloonAndWait('Click Next.',
'font-size:14pt;',
{onhide:() -> #'Next' = true});
#

Java mode

Tea script
driver.quit();
}

8

4. Examples
This chapter shows some useful examples to understand how to compile and run JavaTea tests
and how to describe Tea scripts in it.

Locations Example
https://github.com/teafarm/javatea/tree/master/examples/Locators
The first example is Locators. This example accesses the web page shown below and shows
various ways to find element objects:

Text locator (text:)
Text locator is the most-used locator in JavaTea. It finds a web element by a text displayed on
the page. The text must exact match with the body text of a tag. For example, if you want to
find ‘Name’, it has to be defined as Name. If it may contains extra spaces such
as  Name , use partial text locator instead.
To use a text locator, add ‘text:’ prefix on the target text string. For example, if you want to
find a text element of label ‘Name’, describe as shown below:
'text:Name'
Or you can also simply specify the label text only (‘text:’ is optional)
'Name'
If you access the text element in a function parameter, use # , or # ) syntax.
assertEquals(#'Name', 'Name', 'text locator');
hideElement(#'Name');

9

Partial Text locator (partial:)
Partial text locator finds an element by using a part of text string.
With specifying the partial: prefix, you can find Name text element by using a partial string, for
example, ‘ame’.
'partial:ame'
The ‘partial:ame’ matches all displayed text elements which contains a text ‘ame’, e.g. ‘Name’,
‘frame’, ‘america’
XPath locator (xpath:)
XPath locator finds an element by using a XPath expression. The expression below returns an
input element whose id attribute is ‘name’:
'xpath://input[@id="name"]'
ID locator (id:)
ID locator finds an element by using an id attribute value. The expression below returns a tag
whose id attribute is ‘name’:
'id:name'
Name locator (name:)
Name locator finds an element by using a name attribute value. The expression below returns
a tag whose name attribute is ‘option-value’:
'name:option-value'
Link Text locator (linkText:)
Link Text locator finds an element by using a text string of a hyper link. The expression below
returns an anchor link tag whose body text exacts match with the given text ‘Visit JavaTea
site’:
'linkText:Visit JavaTea site'
Partial Link Text locator (partialLinkText:)
Partial Link Text locator finds an element by using a text string of a hyper link with using partial
match condition. The expression below returns an anchor link tag whose body text contains
the given text ‘JavaTea site’:
'partialLinkText:JavaTea site'
Class Name locator (className:)
Class Name locator finds an element by using a class attribute value. The expression below
returns a tag whose class is ‘label’, class=”label”:

10

'className:label'
Tag Name locator (tagName:)
Tag Name locator finds an element by using a tag name. The expression below returns a tag
whose tag name is h2:
'tagName:h2'
CSS Selector locator (cssSelector:)
CSS Selector locator finds an element by using a CSS Selector expression. The expression below
returns a tag whose id attribute is ‘name’:
'cssSelector:#name'

This Locators example contains a sample code to obtain a TeaElement object by using =# ;
syntax.
TeaElement name = #'id:name';
The TeaElement implements WebElement interface, so you can get the element information
through the APIs.
name.getTagName();
name.getAttribute('id');
name.getText();
name.getCssValue('font-family');
name.getLocation();
name.getSize();
name.clear();
name.click();
name.findBy(By.id('id'));
etc.
If the element found is Select box, ISelect interface is also available to be invoked on the
TeaElement object.
select.getFirstSelectedOption();
select.getOptions();
select.selectByIndex(1);
select.selectByValue('ny');
select.selectByVisibleText('New York');
etc.

11

ArraySuffix (and Shift) Example
https://github.com/teafarm/javatea/tree/master/examples/ArraySuffix
Locator returns the first element if it found more than one element with the given condition. If
you want to access another element, for example the second element, use array suffix to
specify the index. The index number starts with zero.

'Name'[0]
'Name'[1]
'Name'[2]

// returns the first Name text element
// returns the second Name text element
// returns the third Name text element

Once you obtained an element object, you can move to another form element (input, button,
select, etc) by using shift operator ‘>’.

'Name'> // returns the first input element displayed right next to the Name element
'Name'>> // returns the second input element
'Name'2> // returns the second input element
'Name'>>> // returns the third input element
'Name'3> // returns the third input element

12

Elements Example
https://github.com/teafarm/javatea/tree/master/examples/Elements
This sample shows ways to set a value into various type of elements.
Input element
Enter a value ‘John Smith’ into an input element displayed right next to ‘Name’ text element.
Name
1) Clear and enter the given value.
'Name'> = 'John Smith'
2) Enter the given value
Keep the original value and append the given value (Use += assignment)
'Name'> += ' suffix'

Select element - single selection
There are some ways to select an option.
Place

New York

1) Select an option by the displayed text ‘Tokyo’. (Assign a string object using an equal
assignment)
'Place'> = 'New York'
2) Select an option by the option value ‘ny’. (Use @= assignment)
'Place'> @= 'ny'
'Place'>.selectByValue('ny'); // same as @=
3) Select an option by the index number (0-orign). (Assign an int number using an equal
assignment)
'Place'> = 1 // select the second option
'Place'>.selectByIndex(1); // same as ‘= int’
4) Select an option by the display text with using a regular expression. (Use /…/ leteral)
(Select an option text starting with ‘New’)
'Place'> = /New.*/

13

Select element - multiple selection
Select options by the displayed texts. (Use an array).

Color

Red
Yellow
Blue

'Color'> = ['Red', 'Blue']

Radio button
Click on a radio button (Use true literal).
Type

Auto

Truck

1) Click on Auto radio button.
'Type'> = true
2) Click on Truck (Skip Auto radio button).
'Type'> = false true
or
'Truck'< = true

Check box
Click on a check box (Use true literal).
Agreement
'Agreement'> = true

Button element
Click on a button (Use true literal).
Next

'Next' = true
Note that ‘>’ is not needed to specify. In this case, we want to click the ‘Next’ button itself.

14

Lambda Example
https://github.com/teafarm/javatea/tree/master/examples/Lambda
This sample shows how to call lambda function in Tea script.
In Tea script, a lambda function call sets the return value into the current element unless the
return value is null.
Tea script supports the following lambda function formats:
A single statement with no parameters
The function below executes func() and set the return value into the current element.
() -> func()
A single statement with a parameter
The current element is given to the function below through its parameter. The func(element) is
executed and set the return value into the current element.
Note that parentheses for the lambda parameter is required even though there is one
parameter.
(element) -> func(element.getAttribute(‘id’))
Multiple statements with no parameters
The function below executes the function body and set the return value into the current
element.
() -> {
func();
return true;
}
Multiple statements with a parameter
The current element is given to the function below through its parameter. The function body is
executed and set the return value into the current element.
(element) -> {
String id = element.getAttribute(‘id’);
func(id);
return true;
}

15

Wizard Example
https://github.com/teafarm/javatea/tree/master/examples/Wizard
This is a realistic example compared to previous ones. We here test HTML pages with wizard
style. There are two pages titled Page 1 and Page 2. This sample enters a name, an email
address and select a place on the first page. Then the second page shows the values entered.

Here is the JavaTea file to test the pages above.
WizardTest.javat
import static tea.TeaAssert.assertEquals;
import org.junit.jupiter.api.Test;
public class WizardTest extends tea.TeaBase {
@Test
public void test() {
createDriver('chrome');
driver.get(new java.io.File('Page1.html').toURI().toString());
#
'Name'> = 'John'
'john@email'
'Tokyo'
true
#
assertEquals(#'NAME'@>, 'John', 'Name entered');
16

assertEquals(#'EMAIL'@>, 'john@email', 'Email entered');
assertEquals(#'PLACE'@>, 'Tokyo', 'Place selected');
driver.quit();
}
}
The main Tea script code in this sample is this:
'Name'> = 'John'
'john@email'
'Tokyo'
true
The assignment statement in the first line represents sending a value to Name input field. The
‘Name’ is a text locator which returns a text element object of the Name label. But > operator
is attached on the right, so it actually returns a Name input element object displayed right next
to the Name label. Thus, the test string ‘John’ is set in the Name field after execution of this
line.
The assignment statement also counts up element index pointing to current element. Thus
current element index is now pointed to Email input field.
After the first line, there is a value defined in each line. This is because the first line element
index has already set. Since JavaTea moves among elements automatically when a value is set,
you don’t have to specify where you set the value.
The second text string ‘john@email’ is set into the Email field, and element index is increased
and points to the next input element. Likewise, the third line selects ‘Tokyo’ from the selection
box. And the fourth line clicks on the Next button.
After submitted the form on the Page 1, the sample code validates the values entered with the
code below:
assertEquals(#'NAME'@>, 'John', 'Name entered');
assertEquals(#'EMAIL'@>, 'john@email', 'Email entered');
assertEquals(#'PLACE'@>, 'Tokyo', 'Place selected');
The assertEquals() takes three parameters:
void assertEquals( TesElement actual, String expected, String message );
To set an actual value displayed on the Page 2, we want to use a Tea script. So the first
parameter starts with a # sign, and specify the label text of the target value. Next we need to
make a shift and get a target text string of Name. Although we used > operator to shift and
find an input element, this time we need to use @> operator instead. It is because the target
operator is read-only text element, not editable one. ‘Name’@> finds a text string displayed
right next to the ‘Name’ label. In this sample, it is expected to be ‘John’. If the application

17

works fine as expected, the first parameter in the assertion returns ‘John’ and it matches with
the second parameter which is an expected value.
In the sample folder, run the following commands to compile and run the test.
C:> SET PATH=/apache-maven-3.6.1/bin;/webdrivers;C:/Windows/System32
C:> mvn exec:java
C:> mvn test

18

Optional Example
https://github.com/teafarm/javatea/tree/master/examples/Optional
The Optional example shows how optional keyword works. By default, Tea script waits until
the element specified by the locator appears on the page. But in some cases you may want to
skip the element if it does not exist. For example, when entering user information, Name input
field and Next button are displayed for all users, but other input elements (Email, City, Place
and Zip) may not be displayed based on the user profile. In this case, optional keyword can be
added on those locators as shown below:
#
'Name'> = 'John'
optional 'Email'> = 'john@email'
optional 'City'> = 'Test City'
optional 'Place'> = 'Tokyo'
optional 'Zip'> = '12345-6789'
'Next' = true
#
After Tea script has finished to perform the first Name element, if Email text element does not
exist on the page, the second line for Email is skipped. The assignment is executed when
exists.
Note that optional keyword must be used after you confirmed that another element related to
the target is displayed. In the above example, the confirmation is done by ‘Name’ text
element. If ‘Email’ element is displayed, it has to be already displayed when the script handled
the ‘Name’ element.
If there is no other elements on the form page, for example, you can use a text element shown
on the page:
#
waitForText('Page 1')
optional 'Email'> = 'john@email'
#

19

Or Example
https://github.com/teafarm/javatea/tree/master/examples/Or
This contains three examples for Or expression which is used when an element to be displayed
is changed based on stored data. For example, if there are three possibilities on a text to be
displayed on the page, you can describe a list of the three locators separated by a pipe ‘|’.
First example
#
'Ship To'> | 'Bill To'> | 'Note'> = [
'Ship To Address',
'Bill To Address',
'Note Message'
]
#
The first example waits for three text strings ‘Ship To’, ‘Bill To’ and ‘Note’. JavaTea checks to
see if each text is displayed from the left (check ‘Ship To’ -> ‘Bill To’ -> ‘Note’ -> ‘Ship To’ -> …)
until one of the text strings has been shown.
Once one of the texts has been appeared, the corresponding array value is sent to the element
found. For example, if ‘Ship To’ text is found first, ‘Ship To Address’ is set to the element.
Likewise, if ‘Bill To’ appears first, ‘Bill To Address’ is set. And if ‘Note’ appears first, ‘Note
Message’ is set.
Second example
#
'Ship To'> | 'Bill To'> | 'Note'> = 'Default message'
#
The second example defines only one value, ‘Default message’. In this case, no matter which
element is found, the ‘Default message’ is set to the element found.
Third example
#
'Ship To'> | 'Bill To'> | 'Note'> = [ 'Ship To Address' ]
#
The third example has also only one value but this time it is defined in an array. So it is the
same expression as the first example from the syntax point of view. However, this example has
no second and third values in it, which means they are null. The example can be revised by
explicitly defining null values as shown below:

20

Third example (revised)
#
'Ship To'> | 'Bill To'> | 'Note'> = [
'Ship To Address',
null,
null
]
#
If ‘Ship To’ appears first, ‘Ship To Address’ is set. But if ‘Bill To’ or ‘Note’ appears first, nothing
happens.

21

Screenshot Example
https://github.com/teafarm/javatea/tree/master/examples/Screenshot
This example shows how to capture a screenshot when the test failed.
public class ScreenshotTest extends tea.TeaBase {
@Test
public void test() {
try {
createDriver('chrome');
scenario();
} catch (Throwable t) {
takeScreenshot("error.png");
throw t;
} finally {
driver.quit();
}
}
private void scenario() {
…
…
assertEquals(#'NAME'@>, 'Invalid Name', 'Incorrect Name to throw an exception');
}
}
Test scripts are described in scenario() function and it is called from try block in test(). The
assertEquals() in scenario() compares with an invalid name to fail the test. Thus, the test fails
and shows error messages below on console.
[ERROR] Failures:
[ERROR] ScreenshotTest.test:9->scenario:26 Incorrect Name to throw an exception
expected [Invalid Name] but found [John]
[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

22

Also an image ‘error.png’ will be generated in the same folder so that you can check the
screen when failed.

23

Pairwise Example
https://github.com/teafarm/javatea/tree/master/examples/Pairwise
This example uses the same HTMLs as Wizard example. But it specifies two input values
between {* and *} for Name, Email and Place elements. If we test all combinations, we need to
create eight test cases (2 x 2 x 2 = 8 combinations). However, JavaTea has a feature to
generate test cases with using Pairwise technique. In this case, it focuses all pairs of input
values and generates only four test cases. Please see chapter Pairwise Testing to see how
Pairwise reduces the number of test cases.
import static tea.TeaAssert.assertEquals;
import org.junit.jupiter.api.Test;
public class PairwiseTest extends tea.TeaBase {
@Test
public void test() {
createDriver('chrome');
driver.get(new java.io.File('../Wizard/Page1.html').toURI().toString());
#
'Name'> = {* 'John', '' *}
{* 'john@email', '' *}
{* 'New York', 'Tokyo' *}
true
#
assertEquals(#'NAME'@>, toActualValue({***0***}), 'Name entered');
assertEquals(#'EMAIL'@>, toActualValue({***1***}), 'Email entered');
assertEquals(#'PLACE'@>, {***2***}, 'Place selected');
driver.quit();
}
private String toActualValue(String value) {
return value.length() > 0 ? value : "No Value";
}
}
Once you compile the javat file, four java files are generated.

24

Parallel Execution Example
https://github.com/teafarm/javatea/tree/master/examples/ParallelExecution
This example shows how to execute tests in parallel with using JUnit.
import static tea.TeaAssert.assertEquals;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
@Execution(ExecutionMode.CONCURRENT)
public class ParallelExecutionTest extends tea.TeaBase {
@Test
public void test() {
…
In order to run in parallel, you need to annotate @Execution on the test class, and specify
CONCURRENT execution mode.
Also you need to create a junit-platform.properties file and specify the following parameters:
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=6
You can also use TestNG to run tests in parallel. Example for TestNG is available to see the URL
below:
https://github.com/teafarm/javatea/tree/master/examples/ParallelExecutionNG

25

Actions Example
https://github.com/teafarm/javatea/tree/master/examples/Actions
Selenium provides Actions class to simulate complex user operations, e.g. double click, drag
and drop.
Actions builder = new Actions(driver);
builder.doubleClick(#'id:clickBtn'#.element)
.perform();
You can simply use Actions class provided by Selenium. The constructor requires a WebDriver
object, so you can pass driver property to it. Once you get an Actions object (builder variable
in this example), call the Actions APIs.
Note that some APIs requires a WebElement object, but Tea script #'id:clickBtn'# returns
TeaElement. So, you can get the WebElement by the element property.

26

Alert Example
https://github.com/teafarm/javatea/tree/master/examples/Alert
This example shows how to handle Alert, Confirm and Prompt dialogs.
Alert dialog

Alert alert = driver.switchTo().alert();
assertEquals(alert.getText(), 'Hello from alert!', 'Alert message');
alert.accept();
Like Selenium code, you need to get an Alert object by calling switchTo().alert(). Then you can
get the text displayed on the dialog. To close the dialog, call alert.accept().

Confirm dialog

Alert alert = driver.switchTo().alert();
assertEquals(alert.getText(), 'Hello from confirm!', 'Confirmation message');
alert.accept();
Selenium handles Confirm dialog also by using Alert class, hence the code above looks almost
same as the Alert example. But it can call alert.dismiss() to cancel the dialog.

27

Prompt dialog

Alert alert = driver.switchTo().alert();
assertEquals(alert.getText(), 'Hello from prompt!', 'Prompt message');
#alert = 'New Message'#
alert.accept();
Prompt can be handled by Alert class, hence you can call alert.accept() to click OK and call
alert.dismiss() to cancel the dialog. In addition, Prompt has an input box. To send a value, you
can use an equal assignment in Tea script.

28

Demo Example
https://github.com/teafarm/javatea/tree/master/examples/Demo
This is an example using balloon and setAttribute functions for demonstration purpose. We
here assumed that you are going to demonstrate a web application using JavaTea. JavaTea
executes demo scenarios and pause at some points where you want to explain to audience.
Also this shows how to emphasize elements using CSS effects on a web page so that you can
easily to show audience which elements need to be focused on the page. For the details on
balloon APIs and setAttribute function, see JavaTea as a Demonstration Tool chapter.
The DemoTest.javat contains some private methods to make CSS effects on an element.





blurElement()
hideElement()
emphasizeElement()
moveElement()

The blurElement() function blurs text message in the given element. In this example, this
function is applied on an Email text box on Page 1 and an Email text displayed on Page 2 so
that audience cannot see the actual email address presenter entered.

The hideElement() function hides the element from the page.
The emphasizeElement() and moveElement() functions are examples using animation effects.
The emphasizeElement() glows the text in the element by 1pt, and moveElement() moves the
element down by the specified amount of pixels.
It also calls Balloon APIs to show comments on the page.

29

5. Tea Script Language Specifications
This chapter describes language specifications of Tea script. Tea script can be described
between # and # in Java code.
Expression
# 



Navigation menu