GPT Driver + TestNG

Add dependencies to pom.xml

<project ...>

    ...
    
    <dependencies>
    
        ...
        
        <!-- GPT Driver Java SDK -->
        <dependency>
            <groupId>io.mobileboost.gptdriver</groupId>
            <artifactId>gptdriver-client</artifactId>
            <version>1.0.11</version>
        </dependency>
        
        <!-- TestNG for testing -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.7.0</version>
            <scope>test</scope>
        </dependency>
        
    </dependencies>
</project>

In you pom.xml add the necessary build entries incl. the Surefire Plugin:

<project ...>

    ...
        
    <build>
        <plugins>
            <!-- Compiler Plugin: specify Java version -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>11</source>
                    <target>11</target>
                </configuration>
            </plugin>

            <!-- Surefire Plugin to run TestNG tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
</project>

Create a testng.xml file to define your Test Suites:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample Test Suite">
    <test name="SampleTest">
        <classes>
            <class name="com.example.SampleTest"/>
        </classes>
    </test>
</suite>

Create the test classes

The actual test class extending our AppiumTest class:

import com.example.AppiumTest;
import io.mobileboost.gptdriver.GptDriver;
import org.testng.annotations.Test;

public class SampleTest extends AppiumTest {
    @Test
    public  void test() throws Exception {

        // Initialize GptDriver, which can also be done in the Before method
        GptDriver gptDriver = new GptDriver(
                "TEST_API_KEY",
                driver,
                "http://127.0.0.1:4723/wd/hub"
        );
        // Start a gpt driver session
        gptDriver.startSession();

        // Execute commands within the GPT Driver session
        gptDriver.execute("perform a login with email (test@test.com) and password (Test1234)");
        gptDriver.execute("check that you can see your account stats incl. an inbox count");
        gptDriver.execute("perform a logout");

        gptDriver.stopSession("success");
    }
}

The AppiumTest class to create the appium driver being utilized by GPT Driver:

package com.example;

import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import org.openqa.selenium.MutableCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.net.URL;


public class AppiumTest {

    public AndroidDriver driver;

    @BeforeMethod(alwaysRun=true)
    public void setUp() throws Exception {
        // Setting up Appium driver
        MutableCapabilities capabilities = new UiAutomator2Options();
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }

    @AfterMethod(alwaysRun=true)
    public void tearDown() throws Exception {
        // Closing Driver
        driver.quit();
    }
}

Last updated