Run sample Selenium 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 setting up the environment, you can run your first Selenium test
package com.mobitru;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.AbstractDriverOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.net.URL;
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.jupiter.api.Assertions.assertEquals;
import static org.openqa.selenium.By.className;
import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfAllElementsLocatedBy;
public class SeleniumDemoTest {
private static final String PROJECT_NAME = "epm-tstf";
private static final String API_KEY = "--------- YOU API KEY -----------";
private static final String SELENIUM_HUB = "browserhub-us.mobitru.com";
private final AbstractDriverOptions<? extends MutableCapabilities> options;
private RemoteWebDriver driver = null;
public SeleniumDemoTest() {
options = new ChromeOptions();
}
@BeforeEach
public void before() throws Exception {
URL connectionUrl = new URL(format("https://%s:%s@%s/wd/hub", PROJECT_NAME, API_KEY, SELENIUM_HUB));
driver = new RemoteWebDriver(connectionUrl, options);
driver.manage().timeouts()
.pageLoadTimeout(ofMinutes(1));
}
@Test
public void demoTest() {
final String openUrl = "https://mobitru.com/";
driver.get(openUrl);
new WebDriverWait(driver, ofMinutes(1), ofSeconds(1))
.withMessage("Page was not loaded")
.until(driver -> presenceOfAllElementsLocatedBy(className("get-access")));
assertEquals(openUrl, driver.getCurrentUrl(),
"Current url is incorrect");
assertEquals("Mobitru: Cloud and On-Prem Mobile App Testing", driver.getTitle(),
"Page title is incorrect");
}
@AfterEach
public void after() {
ofNullable(driver).ifPresent(RemoteWebDriver::quit);
}
}