Run sample web 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: Run sample test
After setup up the environment, you can run your first web test
package com.mobitru;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.options.UiAutomator2Options;
import io.appium.java_client.android.AndroidDriver;
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.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static java.lang.String.format;
import static java.time.Duration.ofMinutes;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.openqa.selenium.By.name;
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 static final String PLATFORM_VERSION = "11";
private static final String BROWSER_NAME = "chrome";
private static final String DEVICE_NAME = "GOOGLE Pixel 4 XL";
private final BaseOptions<? extends MutableCapabilities> capabilities;
private AppiumDriver driver = null;
public AppiumDemo() {
capabilities = new UiAutomator2Options();
capabilities.setCapability("platformVersion", PLATFORM_VERSION);
capabilities.setCapability("browserName", BROWSER_NAME);
capabilities.setCapability("deviceName", DEVICE_NAME);
}
@Before
public void before() throws Exception {
driver = new AndroidDriver(
new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, ACCESS_KEY, APPIUM_HUB)), capabilities);
// For devices with low performance
driver.manage().timeouts()
.pageLoadTimeout(ofMinutes(5))
.implicitlyWait(ofSeconds(90));
}
@Test
public void demoTest() {
final String openUrl = "https://www.google.com/";
assertTrue(format("Focus is not on '%s'", BROWSER_NAME), driver.isBrowser());
driver.get(openUrl);
new WebDriverWait(driver, ofMinutes(1), ofSeconds(1))
.withMessage("Page was not loaded")
.until(driver -> ExpectedConditions.presenceOfAllElementsLocatedBy(name("q")));
assertEquals("Current url is incorrect", openUrl, driver.getCurrentUrl());
assertEquals("Page title is incorrect", "Google", driver.getTitle());
}
@After
public void after() {
ofNullable(driver).ifPresent(RemoteWebDriver::quit);
}
}
package com.mobitru;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
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.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import static java.lang.String.format;
import static java.time.Duration.ofMinutes;
import static java.time.Duration.ofSeconds;
import static java.util.Optional.ofNullable;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.openqa.selenium.By.name;
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 static final String PLATFORM_VERSION = "15.6";
private static final String BROWSER_NAME = "safari";
private static final String DEVICE_NAME = "IPHONE iPhone13,2";
private final BaseOptions<? extends MutableCapabilities> options;
private AppiumDriver driver = null;
public AppiumDemo() {
options = new XCUITestOptions();
options.setCapability("platformVersion", PLATFORM_VERSION);
options.setCapability("browserName", BROWSER_NAME);
options.setCapability("deviceName", DEVICE_NAME);
}
@Before
public void before() throws Exception {
driver = new IOSDriver(
new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, ACCESS_KEY, APPIUM_HUB)), options);
// For devices with low performance
driver.manage().timeouts()
.pageLoadTimeout(ofMinutes(5))
.implicitlyWait(ofSeconds(90));
}
@Test
public void demoTest() {
final String openUrl = "https://www.google.com/";
assertTrue(format("Focus is not on '%s'", BROWSER_NAME), driver.isBrowser());
driver.get(openUrl);
new WebDriverWait(driver, ofMinutes(1), ofSeconds(1))
.withMessage("Page was not loaded")
.until(driver -> ExpectedConditions.presenceOfAllElementsLocatedBy(name("q")));
assertEquals("Current url is incorrect", openUrl, driver.getCurrentUrl());
assertEquals("Page title is incorrect", "Google", driver.getTitle());
}
@After
public void after() {
ofNullable(driver).ifPresent(RemoteWebDriver::quit);
}
}