Run sample native app test
Step 1: Setup environment
Requirements
- JDK (Highly recommended to use JDK 8 SE)
- Java build framework
- Access key
- You can generate an Access key using the following instruction – Access key
- Billing unit
- The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
More information can be found here.
- The billing unit is your team’s unique identifier, or just the word “personal” in the case of an individual account.
Step 2: Upload and install an App
Before running tests, several preparations are required:
- Upload the app (only once).
- Find and take a Device
- Install the App
All of these actions can be performed automatically via API.
For more details, please refer to the Upload and Install document.
Step 3: Run sample test
After installing the app, you can run your first test using all existing information:
package com.mobitru;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.remote.options.BaseOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static java.lang.String.format;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.Assert.assertEquals;
public class AppiumDemo {
private static final String PROJECT_NAME = "--------- BILLING UNIT -----------";
private static final String ACCESS_KEY = "--------- YOU ACCESS KEY -----------";
private static final String APPIUM_HUB = "app.mobitru.com";
private final BaseOptions<? extends MutableCapabilities> options;
private AppiumDriver driver = null;
public AppiumDemo() {
options = new UiAutomator2Options();
options.setCapability("udid", "26281JEGR04493");
options.setCapability("appPackage", "'com.example.android.apis'");
options.setCapability("appActivity", "'.ApiDemos'");
options.setCapability("noReset", true);
}
@Before
public void before() throws Exception {
driver = new AppiumDriver(
new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, ACCESS_KEY, APPIUM_HUB)), options);
// For devices with low performance
driver.manage().timeouts()
.implicitlyWait(ofSeconds(10));
}
@Test
public void demoTest() {
driver.findElement(AppiumBy.androidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"Views\").instance(0));")).click();
driver.findElement(AppiumBy.accessibilityId("Controls")).click();
driver.findElement(AppiumBy.accessibilityId("1. Light Theme")).click();
WebElement element = driver.findElement(AppiumBy.className("android.widget.EditText"));
element.sendKeys("text");
assertEquals("Entered test is incorrect", "text", element.getText());
}
@After
public void after() {
ofNullable(driver).ifPresent(RemoteWebDriver::quit);
}
}package com.mobitru;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.remote.options.BaseOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static java.lang.String.format;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.Assert.assertEquals;
public class AppiumDemo {
private static final String PROJECT_NAME = "--------- BILLING UNIT -----------";
private static final String ACCESS_KEY = "--------- YOU ACCESS KEY -----------";
private static final String APPIUM_HUB = "app.mobitru.com";
private final BaseOptions<? extends MutableCapabilities> options;
private AppiumDriver driver = null;
public AppiumDemo() {
options = new XCUITestOptions();
options.setCapability("udid", "00008030-000C312C1E69802E");
options.setCapability("bundleId", "com.mobitru.appium.uicatalog");
options.setCapability("autoAcceptAlerts", true);
}
@Before
public void before() throws Exception {
driver = new AppiumDriver(
new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, ACCESS_KEY, APPIUM_HUB)), options);
// For devices with low performance
driver.manage().timeouts()
.implicitlyWait(ofSeconds(10));
}
@Test
public void demoTest() {
String text = "text";
WebElement input = driver.findElement(AppiumBy.className("XCUIElementTypeTextField"));
input.sendKeys(text);
String enteredText = input.getAttribute("value");
assertEquals("Entered text is incorrect", text, enteredText);
}
@After
public void after() {
ofNullable(driver).ifPresent(RemoteWebDriver::quit);
}
}