Powered By GitBook
Java
Java SDK (v1)
This repository contains code examples based on TestProject Java SDK.

Briefing

This document describes the bare minimum steps to start developing tests using the Java SDK. TestProject provides a unified test automation SDK with support for Android, iOS and Web applications by utilizing the open-source Selenium and Appium frameworks. TestProject is OS agnostic and can run on Windows, Linux or Mac. It is a full stack automation framework with capabilities that allow automation test management, remote and local test execution, job scheduling, reporting dashboards, collaboration and more.

Preparations

To kick-off automation development with TestProject, it is necessary to have an active TestProject account and the TestProject Agent installed. TestProject's Agent is a cross-platform desktop application, allowing you to create, debug and execute your test automation locally. TestProject Agent can be downloaded from Agents page.

Getting Java SDK

You can download TestProject SDK for Java from the Developers page and reference it in your project.

Installing SDK

To use TestProject SDK you have to add it as a reference to your project. Here are some examples for how it should be done using Maven or Gradle.
Maven:
1
<dependency>
2
<groupId>io.testproject</groupId>
3
<artifactId>java-sdk</artifactId>
4
<version>1.0</version>
5
<systemPath>/path/to/sdk/io.testproject.sdk.java.jar</systemPath>
6
<scope>system</scope>
7
</dependency>
Copied!
Gradle:
1
compile files("/path/to/sdk/io.testproject.sdk.java.jar")
Copied!
Refer to pom.xml and build.gradle files in the provided examples for more details.

Test Development

The best way to start developing automated tests with TestProject is by reviewing the source code of a basic test that performs a login and updates a profile form, expecting the save to succeed.
There is also a Generic test, representing a dummy scenario that can be automated. It can be used as a reference for real scenarios that automate a non-UI sequences (those that do not require a Selenium or Appium driver).

Test Class

In order to build a Test that can be executed by TestProject, the class has to implement on of the interfaces that the SDK provides. Interface implementation requires an implementation of the execute() method, that will be be invoked by the platform to run the Test. The execute() method returns ExecutionResult enum which can be PASSED or FAILED.
Below are some examples for Test implementation on different platforms:
Web Test
BasicTest class implements the WebTest interface:
1
public class BasicTest implements WebTest
Copied!
Test entry point is the execute method:
1
public ExecutionResult execute(WebTestHelper helper) throws FailureException
Copied!
The following line of code retrieves a driver to automate the browser.
1
// Get driver initialized by TestProject Agent
2
// No need to specify browser type, it can be done later via UI
3
WebDriver driver = helper.getDriver();
Copied!
Following code is used to navigate the browser to the relevant URL. After it is executed, TestProject Demo page is loaded.
1
// Navigate to TestProject Demo website
2
driver.navigate().to("https://example.testproject.io/web/");
Copied!
The following code uses the Page Object Model pattern to login in and complete a profile form, saving it:
1
// Login using provided credentials
2
LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);
3
loginPage.login(name, password);
4
5
// Complete profile forms and save it
6
ProfilePage profilePage = PageFactory.initElements(driver, ProfilePage.class);
7
profilePage.updateProfile(country, address, email, phone);
Copied!
The following return statement will assert test result:
1
return profilePage.isSaved() ? ExecutionResult.PASSED : ExecutionResult.FAILED;
Copied!
Android Test
BasicTest class implements the AndroidTest interface:
1
public class BasicTest implements AndroidTest
Copied!
Test entry point is the execute method:
1
public ExecutionResult execute(AndroidTestHelper helper) throws FailureException
Copied!
The following line of code retrieves a driver to automate the App.
1
// Get driver initialized by TestProject Agent
2
AndroidDriver driver = helper.getDriver();
Copied!
The following code resets the App and uses the Page Object Model pattern to login in and complete a profile form, saving it:
1
driver.resetApp();
2
3
LoginPage loginPage = new LoginPage(driver);
4
loginPage.login(name, password);
5
6
ProfilePage profilePage = new ProfilePage(driver);
7
profilePage.updateProfile(country, address, email, phone);
Copied!
The following return statement will assert test result:
1
return profilePage.isSaved() ? ExecutionResult.PASSED : ExecutionResult.FAILED;
Copied!
iOS Test
BasicTest class implements the IOSTest interface:
1
public class BasicTest implements IOSTest
Copied!
Test entry point is the execute method:
1
public ExecutionResult execute(IOSTestHelper helper) throws FailureException
Copied!
The following line of code retrieves a driver to automate the App.
1
// Get driver initialized by TestProject Agent
2
IOSDriver driver = helper.getDriver();
Copied!
The following code resets the App and uses the Page Object Model pattern to login in and complete a profile form, saving it:
1
driver.resetApp();
2
3
LoginPage loginPage = new LoginPage(driver);
4
loginPage.login(name, password);
5
6
ProfilePage profilePage = new ProfilePage(driver);
7
profilePage.updateProfile(country, address, email, phone);
Copied!
The following return statement will assert test result:
1
return profilePage.isSaved() ? ExecutionResult.PASSED : ExecutionResult.FAILED;
Copied!
Generic Test
BasicTest class implements the GenericTest interface:
1
public class BasicTest implements GenericTest
Copied!
Test entry point is the execute method:
1
public ExecutionResult execute(TestHelper helper) throws FailureException
Copied!
The following code adds the value stored in 'a' variable to the value in 'b' and if equals 2, asserts success:
1
int a = 1, b = 1;
2
3
if (a + b == 2) {
4
return ExecutionResult.PASSED;
5
} else {
6
return ExecutionResult.FAILED;
7
}
Copied!

Debugging / Running Test

To debug or run the test locally, you will have to use the Runner class from TestProject SDK. All code examples, have JUnit tests that use Runner to debug the automation locally. Debugging or running a test locally with the Runner class, requires authentication before communication with the TestProject Agent (since it is the execution engine). Development token for authentication can be easily obtained from the Developers page. It should be used as a parameter in one of the Runner factory methods:
Web
1
Runner runner = Runner.createWeb("YOUR_DEV_TOKEN", ...
Copied!
Android
1
Runner runner = Runner.createAndroid("YOUR_DEV_TOKEN", ...
Copied!
Chrome on Android
1
Runner runner = Runner.createAndroidWeb("YOUR_DEV_TOKEN", ...
Copied!
iOS
1
Runner runner = Runner.createIOS("YOUR_DEV_TOKEN", ...
Copied!
Safari on iOS
1
Runner runner = Runner.createIOSWeb("YOUR_DEV_TOKEN", ...
Copied!
Generic
1
Runner runner = Runner.create("YOUR_DEV_TOKEN"...
Copied!

Using parameters and step reports in your tests

Let's make our example more advanced by adding parameters. To add parameters to your test, you simply need to add fields with relevant annotations. In addition, we will create step reports to separate the different stages of the test (each report will appear as a separate step in the future execution reports).
See the relevant platform link for full source code:

Test Annotations

TestProject SDK provides annotations to describe the test and its parameters:
    1.
    The Test annotation is used to better describe the Test and define how it will appear later in TestProject UI:
      name - The name of the test (if omitted, the name of the class will be used).
      description - A description of the test which is shown in various places in TestProject platform (e.g. reporting dashboard). The description may contain placeholders {{propertyName}} that will be changed dynamically according to test parameters.
      version - A version string which is used for future reference.
    2.
    The Parameter annotation is used to better describe your Test inputs and outputs, in the example above there are two inputs - url and expectedTitle.
      description - The description of the parameter
      direction - Defines the parameter as an input (default if omitted) or an output parameter. An input parameter will receive values when the test is executed while the output parameter value will be retrieved at the end of test execution (and can be used in following steps later on in the automation scenario).
      defaultValue - Defines a default value that will be used for the parameter.

Reports

Implemented execute() method receives a Helper instance as a parameter. Via this helper, you can obtain an instance of TestReporter class.
1
TestReporter report = helper.getReporter();
Copied!
Notice the following line in the Extended Test example. This line reports a step based on provided condition and takes a screenshot:
1
report.step("Profile information saved", profilePage.isSaved(), TakeScreenshotConditionType.Always);
Copied!
Using the following code one can set test result message:
1
report.result("Test completed successfully");
Copied!

Addon development

Much like tests, you can develop custom Addons to extend TestProject and shape your automated testing solution for your needs. An Addon is a set of Actions (one or more) where each Action does a specific task. A common Addon scenario will be to extend basic set of Actions on complicated UI elements or make wrappers for user defined API. Once created, Actions can be used to design steps of automated tests.
You can learn more about how to create your own addons, here in the documentation.

Crowd Code / Addon Proxy

One of the greatest features of the TestProject environment is the ability to execute a code written by someone else. It can be your account colleagues writing actions that you can reuse, or TestProject community users. Developer must download a binary file with the proxy class for the Action he wants to execute.
Assuming your account member uploaded the example Addon, named it Example and you want to reuse it's code your Test. To do so, you can download it's proxy JAR and use it like this:
1
ClearFields clearFieldsAction = ExampleAddon.clearFields();
Copied!
Implemented execute() method receives a Helper instance as a parameter. Via this helper, you can execute the proxy by invoking the executeProxy method:
1
StepExecutionResult result = helper.executeProxy(clearFieldsAction);
Copied!
See examples:

Packaging

To package and upload coded tests written using the TestProject V1 SDK in Java, you will need to package it as a .jar file.
Export your code as an uber .jar file with dependencies, excluding the TestProject SDK.
Once the jar is compiled and uploaded, TestProject will display your project as a package with all the tests inside of it.
Please note that once uploaded, the tests cannot be edited unless reuploaded after performing the changes in your local development environment.
The following examples have been created using IntelliJ.
Gradle
Open the Gradle tab in your IDE, you will need to compile a jar using Tasks/build/jar.
After running the task, the jar will be created in the build/libs directory.
Maven
Open the Maven tab, select Lifecycle, and execute the following in order:
    clean
    compile
    package
Once the package command ends; the jar will be created in the target directory.
To upload the package, head to your project on the TestProject platform, and create a new test.
Select the code option.
Upload your jar.
Press next.
Give your package a name, description and choose an application and press next to complete the process.
See build.gradle or pom.xml files in code examples for details on how to include the TestProject V1 SDK dependency in your project.

Support

For any further inquiries, please use TestProject support channels:
Last modified 6mo ago