Skip to content

Instantly share code, notes, and snippets.

@FriendlyTester
Created April 30, 2017 21:08
Show Gist options
  • Save FriendlyTester/d0706d5ca51e573c0bd3554c70dc5b2f to your computer and use it in GitHub Desktop.
Save FriendlyTester/d0706d5ca51e573c0bd3554c70dc5b2f to your computer and use it in GitHub Desktop.
Running Chrome Headlessly Using WebDriver
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.safari.SafariDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
public class HeadlessChrome
{
@Test
public void createChromeDriverHeadless() throws IOException
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setBinary("/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary");
chromeOptions.addArguments("--headless");
WebDriver Driver = new ChromeDriver(chromeOptions);
Driver.navigate().to("https://the-internet.herokuapp.com/login");
WebDriverWait waitForUsername = new WebDriverWait(Driver, 5000);
waitForUsername.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
Driver.findElement(By.id("username")).sendKeys("tomsmith");
Driver.findElement(By.cssSelector("button.radius")).click();
WebDriverWait waitForError = new WebDriverWait(Driver, 5000);
waitForError.until(ExpectedConditions.visibilityOfElementLocated(By.id("flash")));
Assert.assertTrue(Driver.findElement(By.id("flash")).getText().contains("Your password is invalid!"));
Driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment