Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Last active July 11, 2017 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcalmac/fec0a1093ad916c6b3199a49b35acfc1 to your computer and use it in GitHub Desktop.
Save bcalmac/fec0a1093ad916c6b3199a49b35acfc1 to your computer and use it in GitHub Desktop.
Minimal Selenium example that performs a search on stackoverflow.com
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
public class StackOverflowSearchExample {
@Test
public void search() {
WebDriver driver = new FirefoxDriver();
// And now use this to visit StackOverflow
driver.get("https://stackoverflow.com/");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("selenium");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
// Wait for the page to load, timeout after 10 seconds
new WebDriverWait(driver, 10).until(d -> d.getTitle().toLowerCase().contains("selenium"));
// Should see: newest 'selenium' questions
System.out.println("Page title is: " + driver.getTitle());
//Close the browser
driver.quit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment