Skip to content

Java Implementation

chevy2dr56 edited this page Jun 20, 2017 · 4 revisions

Here you'll go through the process of test definition using Java, to create a suite of test scenarios, and define the steps to be taken for each test.

Creating your test

Familiarize yourself with the [project structure](Project Layout) and use the following simple procedure to create your Java test suite.

To write your test:

  1. Download the sample project from the Perfecto Git Repository and save to your project folder.

  2. Import the project as a Maven project into [Eclipse](Java Implementation#in-eclipse) or [IntelliJ](Java Implementation#in-intellij).

  3. If required, create the [Object Repository](Object Repository) under the resources/ folder at the top level, or add objects definitions of your choice.

  4. Create a new test class that extends the JavaTestsRunner class. This provides the support for all extensions needed to run the Quantum environment.
    The test class should be created in the src/test/java/com/perfectomobile/quantum/java folder.

  5. Use one of the sample files as a template for your test file. Edit the file, deleting the existing code but preserving the header information, and add your test scenario code.

  6. [Write your test](Java Implementation#writing-the-test) by creating scenarios using either:

    • pre-defined logical steps supplied by Perfecto classes (PerfectoApplicationSteps and PerfectoDeviceSteps),
    • your [customized steps](Creating customized steps) for the specific application,
    • [utility methods](Java Utility classes), or
    • basic Selenium/Appium Java statements.
  7. Annotate your test scenarios with the @Test annotation and add tags or groups to your scenarios to group them as test classes in the [testng.xml](Quantum TestNG File) file.

  8. Configure your [testng.xml file](Quantum TestNG File) to define your test suite and test classes. Identifying the tag and test runner class to use for each test class.

Importing the sample code as an IDE project

In Eclipse

  1. Select File > Import.

  2. Select Maven > Existing Maven project.

  3. Click Next, and then Browse to the downloaded project's local location.

  4. Select the cloned project's pom.xml file, supply the name for your project, and click Finish.

In IntelliJ

  1. Select File > Open.

  2. Browse to the project folder and open the pom.xml file under it.

  3. Select File > Settings, and verify your git location is added to the Maven home directory list.

Writing the test

ℹ️ To be able to use operations such as click() and sendText("text") in your test, include the statement: import static com.qmetry.qaf.automation.step.CommonStep.*;.

Import statements

The following import statements should be included in your test class file:

import com.quantum.steps.PerfectoApplicationSteps;
import com.quantum.utils.DeviceUtils;
import com.quantum.utils.AppiumUtils;

import com.qmetry.qaf.automation.testng.dataprovider.QAFDataProvider;
import com.qmetry.qaf.automation.ui.webdriver.QAFExtendedWebDriver;
import org.testng.annotations.Test;

import java.util.Map;

import static com.qmetry.qaf.automation.step.CommonStep.*;

In addition, if you have defined [customized logical steps](Creating customized steps), import the custom steps class:

import com.perfectomobile.quantum.steps.KeepNoteSteps;

Test Methods

Each test method should be annotated with @Test annotation and may be assigned a tag or a groups tag. for example:

    @Test (groups = {"run"})
    public void communityLogin(Map<String, String> data){...}

The groups or tag should be referenced in the testng.xml file to identify which test methods should be activated. For example:

    <groups>
        <run>
            <include name="run"/>
        </run>
    </groups>

Data Driven Tests

If the test class is data-driven then use the @QAFDataProvider data provider class and provide a link to the data file. Data files may be in Excel csv or xls, json, or xml format - where each data value is associated with a key, used to access the value.

    @QAFDataProvider(dataFile = "src/test/resources/data/testData.xls")

To access the values from the data file use the QAF get() method of the data provider instance, providing the data key. The method retrieves the associated value.

        sendKeys(data.get("username"), "login.user");
        sendKeys(data.get("password"), "login.password");

Pre-defined Logical Steps and Utilities

Quantum supplies a set of built-in logical steps that can be used in your Java test code as static methods. These include methods such as click, sendKeys, waitForText. These steps are accessible by using the following import statement:

import static com.qmetry.qaf.automation.step.CommonStep.*;

Perfecto also supplies a set of [Utility Methods](Java Utility classes) that can be used as static class methods.

Information about these logical steps are supplied as javadoc information displayed automatically by the IDE.

In addition, you can extend the set of logical steps by supplying appropriate classes and methods and importing them. Your logical step classes should be located in the src/test/java/com/perfectomobile/quantum/steps folder. You can see an example of this in the MyCommunityTestSteps.java file that includes the following two logical steps:

    @Then("^I fill user \"(.*?)\" and password \"(.*?)\"$")
    public void IFill(String user, String password) {
        assertEnabled("login.user");
        sendKeys(user, "login.user");
        sendKeys(password, "login.password");

    }

    @Then("^I go to login page$")
    public void goToLogin() {
        click("button.menu");
        click("button.login");
    }

Using objects from the Object Repository

References to user-interface elements should use the logical names defined in the [Object Repository](Object Repository). For example to click on the Login button, use the logical name "button.login" as in the following statement:

click("button.login");