Examples

Simple example

import io.mobileboost.gptdriver.GptDriver;
import io.mobileboost.gptdriver.types.Platform;

import java.util.Map;
import java.util.List;

public class Playground {

    public static void main(String[] args) throws Exception {

        GptDriver gptDriver = new GptDriver(
                "...",
                Platform.Android,
                "http://localhost:4723"
        );
        
        gptDriver.startSession();
        
        gptDriver.execute("Tap on Gmail and succeed the task");
        
        gptDriver.stopSession("success");
    }
}

TestNG + Browserstack example

Add dependencies to pom.xml

<!-- BrowserStack Java SDK -->
<dependency>
    <groupId>com.browserstack</groupId>
    <artifactId>browserstack-java-sdk</artifactId>
    <version>1.22.0</version>
</dependency>

<!-- TestNG for testing -->
<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.5</version>
    <scope>test</scope>
</dependency>

Also adding a TestNG related profile and plugins required for Browserstack

<profiles>
    <!-- Profile for running sample test -->
    <profile>
        <id>sample-test</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <suiteXmlFiles>
                            <suiteXmlFile>src/test/resources/sample-test.testng.xml</suiteXmlFile>
                        </suiteXmlFiles>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

<plugins>
    <!-- Maven dependency getClasspathFilenames plugin -->
    <plugin>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>3.3.0</version>
        <executions>
            <execution>
                <id>getClasspathFilenames</id>
                <goals>
                    <goal>properties</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    <!-- Maven Surefire plugin -->
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.2</version>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>src/test/resources/sample-test.testng.xml</suiteXmlFile>
            </suiteXmlFiles>
            <argLine>-javaagent:${com.browserstack:browserstack-java-sdk:jar}</argLine>
        </configuration>
    </plugin>
</plugins>

Create test classes

BrowserstackExampleTest.java

package integrations;

import io.mobileboost.gptdriver.GptDriver;
import org.testng.annotations.Test;

public class BrowserstackExampleTest extends AppiumTest {
    @Test
    public  void test() throws Exception {
        System.out.println("Executing test code...");

        String gptDriverApiKey = "...";

        // Initialize GptDriver
        GptDriver gptDriver = new GptDriver(
                gptDriverApiKey,
                driver,
                "https://hub-use.browserstack.com/wd/hub"
        );

        // Start session, execute commands, and stop the session
        gptDriver.startSession();
        
        gptDriver.execute("tap on get started");
        
        gptDriver.stopSession("success");
    }
}

AppiumTest.java

package integrations;

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 {
        System.out.println("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 {
        System.out.println("Closing Driver");
        driver.quit();
    }
}

Creating the TestNG xml

sample-test.testng.xml in the /test/java/resources folder

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="First">
    <test name="FirstTest">
        <classes>
            <class name="integrations.BrowserstackExampleTest"/>
        </classes>
    </test>
</suite>

Last updated