Selenium with Java
If you prefer to run your Selenium tests using Java, clone the repository, or select and make use of one of the sample tests below to see our platform in action.
All the scripts below makes use of the grid access key. In order to run the test do the following
- Get your access key
- Replace it with <YOUR ACCESS KEY>, in the sample tests below.
Replace <server> with the appropriate URL.
- Public Continuous Testing Cloud - https://cloud.seetest.io/wd/hub/.
- Dedicated Continuous Testing Cloud environment - Your own domain. For example: https://company.experitest.com/wd/hub/
- On-premises Continuous Testing Cloud environment - Your designated URL. For example: https://company.com/wd/hub
Chrome with Java and JUnit Expand source
package Junit;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class ChromeTest {
private static final String ACCESSKEY = "<YOUR ACCESS KEY>";
String CLOUDURL = "<server>";
String testName= "Selenium Test on Chrome";
RemoteWebDriver driver;
@Before
public void setUp() throws Exception {
//Set Browser Type
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
//Set Grid capabilities
dc.setCapability("accessKey", ACCESSKEY);
dc.setCapability("generateReport", true);
dc.setCapability("testName", testName);
driver = new RemoteWebDriver(new URL(CLOUDURL), dc);
}
@Test
public void testSeleniumOnChrome() {
driver.get("https://seetest.io");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Manual']")));
WebElement manualNavLink = driver.findElement(By.xpath("//*[text()='Manual']"));
manualNavLink.click();
WebElement automationNavLink = driver.findElement(By.xpath("//*[text()='Automation']"));
automationNavLink.click();
WebElement webinarFooterLink = driver.findElement(By.xpath("//*[text()='Webinars']"));
webinarFooterLink.click();
String webinarH2TitleText = driver.findElement(By.xpath("//h2[1]")).getText();
System.out.println("The title of the first h2 is: " + webinarH2TitleText);
}
@After
public void tearDown() {
driver.quit();
}
}
Chrome with Java and TestNG Expand source
package TestNG;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class ChromeTest {
private static final String ACCESSKEY = "<YOUR ACCESS KEY>";
String CLOUDURL = "<server>";
String testName= "Selenium Test on Chrome";
RemoteWebDriver driver;
@BeforeMethod
public void setUp() throws Exception {
//Set Browser Type
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
//Set Grid capabilities
dc.setCapability("accessKey", ACCESSKEY);
dc.setCapability("generateReport", true);
dc.setCapability("testName", testName);
driver = new RemoteWebDriver(new URL(CLOUDURL), dc);
}
@Test
public void testSeleniumOnChrome() {
driver.get("https://seetest.io");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Manual']")));
WebElement manualNavLink = driver.findElement(By.xpath("//*[text()='Manual']"));
manualNavLink.click();
WebElement automationNavLink = driver.findElement(By.xpath("//*[text()='Automation']"));
automationNavLink.click();
WebElement webinarFooterLink = driver.findElement(By.xpath("//*[text()='Webinars']"));
webinarFooterLink.click();
String webinarH2TitleText = driver.findElement(By.xpath("//h2[1]")).getText();
System.out.println("The title of the first h2 is: " + webinarH2TitleText);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
Firefox with Java and JUnit Expand source
package Junit;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class FirefoxTest {
private static final String ACCESSKEY = "<YOUR ACCESS KEY>";
String CLOUDURL = "<server>";
String testName= "Selenium Test on Firefox";
RemoteWebDriver driver;
@Before
public void setUp() throws Exception {
//Set Browser Type
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
//Set Grid capabilities
dc.setCapability("accessKey", ACCESSKEY);
dc.setCapability("generateReport", true);
dc.setCapability("testName", testName);
driver = new RemoteWebDriver(new URL(CLOUDURL), dc);
}
@Test
public void testSeleniumOnFirefox() {
driver.get("https://seetest.io");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Manual']")));
WebElement manualNavLink = driver.findElement(By.xpath("//*[text()='Manual']"));
manualNavLink.click();
WebElement automationNavLink = driver.findElement(By.xpath("//*[text()='Automation']"));
automationNavLink.click();
WebElement webinarFooterLink = driver.findElement(By.xpath("//*[text()='Webinars']"));
webinarFooterLink.click();
String webinarH2TitleText = driver.findElement(By.xpath("//h2[1]")).getText();
System.out.println("The title of the first h2 is: " + webinarH2TitleText);
}
@After
public void tearDown() {
driver.quit();
}
}
Firefox with Java and TestNG Expand source
package TestNG;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class FirefoxTest {
private static final String ACCESSKEY = "<YOUR ACCESS KEY>";
String CLOUDURL = "<server>";
String testName= "Selenium Test on Firefox";
RemoteWebDriver driver;
@BeforeMethod
public void setUp() throws Exception {
//Set Browser Type
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
//Set Grid capabilities
dc.setCapability("accessKey", ACCESSKEY);
dc.setCapability("generateReport", true);
dc.setCapability("testName", testName);
driver = new RemoteWebDriver(new URL(CLOUDURL), dc);
}
@Test
public void testSeleniumOnFirefox() {
driver.get("https://seetest.io");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Manual']")));
WebElement manualNavLink = driver.findElement(By.xpath("//*[text()='Manual']"));
manualNavLink.click();
WebElement automationNavLink = driver.findElement(By.xpath("//*[text()='Automation']"));
automationNavLink.click();
WebElement webinarFooterLink = driver.findElement(By.xpath("//*[text()='Webinars']"));
webinarFooterLink.click();
String webinarH2TitleText = driver.findElement(By.xpath("//h2[1]")).getText();
System.out.println("The title of the first h2 is: " + webinarH2TitleText);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
IE with Java and JUnit Expand source
package Junit;
import java.net.URL;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class IETest {
private static final String ACCESSKEY = "<YOUR ACCESS KEY>";
String CLOUDURL = "<server>";
String testName= "Selenium Test on IE";
RemoteWebDriver driver;
@Before
public void setUp() throws Exception {
//Set Browser Type
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.FIREFOX);
//Set Grid capabilities
dc.setCapability("accessKey", ACCESSKEY);
dc.setCapability("generateReport", true);
dc.setCapability("testName", testName);
dc.setCapability("initialBrowserUrl", "https://www.google.com");
driver = new RemoteWebDriver(new URL(CLOUDURL), dc);
}
@Test
public void testSeleniumOnIE() {
driver.get("https://seetest.io");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Manual']")));
WebElement manualNavLink = driver.findElement(By.xpath("//*[text()='Manual']"));
manualNavLink.click();
WebElement automationNavLink = driver.findElement(By.xpath("//*[text()='Automation']"));
automationNavLink.click();
WebElement webinarFooterLink = driver.findElement(By.xpath("//*[text()='Webinars']"));
webinarFooterLink.click();
String webinarH2TitleText = driver.findElement(By.xpath("//h2[1]")).getText();
System.out.println("The title of the first h2 is: " + webinarH2TitleText);
}
@After
public void tearDown() {
driver.quit();
}
}
IE with Java and TestNG Expand source
package TestNG;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class IETest {
private static final String ACCESSKEY = "<YOUR ACCESS KEY>";
String CLOUDURL = "<server>";
String testName= "Selenium Test on IE";
RemoteWebDriver driver;
@BeforeMethod
public void setUp() throws Exception {
//Set Browser Type
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.IE);
//Set Grid capabilities
dc.setCapability("accessKey", ACCESSKEY);
dc.setCapability("generateReport", true);
dc.setCapability("testName", testName);
dc.setCapability("initialBrowserUrl", "https://www.google.com");
driver = new RemoteWebDriver(new URL(CLOUDURL), dc);
}
@Test
public void testSeleniumOnIE() {
driver.get("https://seetest.io");
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[text()='Manual']")));
WebElement manualNavLink = driver.findElement(By.xpath("//*[text()='Manual']"));
manualNavLink.click();
WebElement automationNavLink = driver.findElement(By.xpath("//*[text()='Automation']"));
automationNavLink.click();
WebElement webinarFooterLink = driver.findElement(By.xpath("//*[text()='Webinars']"));
webinarFooterLink.click();
String webinarH2TitleText = driver.findElement(By.xpath("//h2[1]")).getText();
System.out.println("The title of the first h2 is: " + webinarH2TitleText);
}
@AfterMethod
public void tearDown() {
driver.quit();
}
}
Before you begin, make sure to install Selenium dependencies. We recommend obtaining the dependencies with the help of Gradle or Maven.
Fetch Selenium Dependencies with Gradle
In your build.gradle file, in the dependencies group, add the following line:
Dependency in gradle build file
dependencies {
testCompile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '3.9.0'
}
// change the version to the latest or desired version
Fetch Selenium Dependencies with Maven
In your pom.xml, add the following:
Dependency in Maven POM.xml
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.9.0</version>
</dependency>