Last active
July 11, 2017 22:06
-
-
Save bcalmac/fec0a1093ad916c6b3199a49b35acfc1 to your computer and use it in GitHub Desktop.
Minimal Selenium example that performs a search on stackoverflow.com
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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