Created
October 17, 2012 20:01
-
-
Save dwelch2344/3907781 to your computer and use it in GitHub Desktop.
Selenium WebDriver example with IE
This file contains hidden or 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
| package co.ntier.selenium; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import org.openqa.selenium.By; | |
| import org.openqa.selenium.WebDriver; | |
| import org.openqa.selenium.WebElement; | |
| import org.openqa.selenium.ie.InternetExplorerDriver; | |
| import org.openqa.selenium.support.ui.ExpectedCondition; | |
| import org.openqa.selenium.support.ui.WebDriverWait; | |
| public class DriverExample { | |
| public List<WebDriver> drivers() { | |
| @SuppressWarnings("serial") | |
| List<WebDriver> drivers = new ArrayList<WebDriver>() {{ | |
| // DesiredCapabilities ieCapabilities = DesiredCapabilities.internetExplorer(); | |
| // ieCapabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); | |
| // ieCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true); | |
| // InternetExplorerDriver ie = new InternetExplorerDriver(ieCapabilities); | |
| InternetExplorerDriver ie = new InternetExplorerDriver(); | |
| add(ie); | |
| /*add(new FirefoxDriver()); | |
| add(new ChromeDriver());*/ | |
| }}; | |
| return drivers; | |
| } | |
| public void test() { | |
| for(WebDriver driver : drivers()) { | |
| System.out.println("Starting " + driver.toString()); | |
| buh(driver); | |
| System.out.println("Finishing " + driver.toString()); | |
| System.out.println(); | |
| System.out.println(); | |
| System.out.println(); | |
| } | |
| } | |
| public void buh(WebDriver driver) { | |
| // And now use this to visit Google | |
| //driver.get("http://www.google.com"); | |
| driver.navigate().to("http://www.google.com"); | |
| /*System.out.println(driver.getPageSource());*/ | |
| WebElement element = ( | |
| new WebDriverWait(driver, 10)) | |
| .until(new ExpectedCondition<WebElement>(){ | |
| public WebElement apply(WebDriver d) { | |
| return d.findElement( By.id("gbqfq") ); | |
| } | |
| } | |
| ); | |
| // Enter something to search for | |
| element.sendKeys("Cheese!"); | |
| // 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()); | |
| // Google's search is rendered dynamically with JavaScript. | |
| // Wait for the page to load, timeout after 10 seconds | |
| (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { | |
| public Boolean apply(WebDriver d) { | |
| return d.getTitle().toLowerCase().startsWith("cheese!"); | |
| } | |
| }); | |
| // Should see: "cheese! - Google Search" | |
| System.out.println("Page title is: " + driver.getTitle()); | |
| //Close the browser | |
| driver.quit(); | |
| } | |
| public static void main(String[] args) { | |
| DriverExample e = new DriverExample(); | |
| e.test(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment