Skip to content

Instantly share code, notes, and snippets.

@eviltester
Created October 17, 2018 12:01
Show Gist options
  • Select an option

  • Save eviltester/0b5c1289263d31cd5305fb70985c2d8f to your computer and use it in GitHub Desktop.

Select an option

Save eviltester/0b5c1289263d31cd5305fb70985c2d8f to your computer and use it in GitHub Desktop.
code for old Udemy blog post
import org.junit.Assert;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;
public class MyFirstWebDriverTest {
@Test
public void checkSeleniumHQinFirefox(){
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.org");
WebElement downloadTab = driver.findElement(By.id("menu_download"));
WebElement downloadLink = downloadTab.findElement(By.tagName("a"));
downloadLink.click();
Assert.assertEquals("Downloads", driver.getTitle());
driver.quit();
}
@Test
public void checkSeleniumHQinFirefoxUsingCSSSelector(){
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.org");
// the css selector is smaller and I can find the element with one findElement command
WebElement downloadLink = driver.findElement(By.cssSelector("#menu_download a"));
downloadLink.click();
Assert.assertEquals("Downloads", driver.getTitle());
driver.quit();
}
@Test
public void implicitWaitExample(){
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.org");
// if menu_download was loaded via AJAX then we would have to wait before using it
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Now the findElements will retry if an element can not be found, for 10 seconds
WebElement downloadTab = driver.findElement(By.id("menu_download"));
WebElement downloadLink = downloadTab.findElement(By.tagName("a"));
downloadLink.click();
Assert.assertEquals("Downloads", driver.getTitle());
// set the timeout back to 0
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
driver.quit();
}
@Test
public void explicitWaitExample(){
WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.org");
// if menu_download was loaded via AJAX then we would have to wait before using it
WebDriverWait wait = new WebDriverWait(driver, 10);
// wait until the menu_download div is part of the dom
// Note that we are returning the WebElement from the wait
WebElement downloadLink = wait.until(
ExpectedConditions.presenceOfElementLocated(
By.cssSelector("#menu_download a")));
// wait until the link is clickable
wait.until(ExpectedConditions.elementToBeClickable(downloadLink));
// now that we know it is in the dom, and clickable, we can safely click it
downloadLink.click();
wait.until(ExpectedConditions.titleIs("Downloads"));
Assert.assertEquals("Downloads", driver.getTitle());
// set the timeout back to 0
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
driver.quit();
}
}
@eviltester
Copy link
Author

This was a set of examples for a Udemy blog post from 2015

@eviltester
Copy link
Author

pom.xml at the time was

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>myFirstWebDriverTest</groupId>
    <artifactId>myFirstWebDriverTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>2.47.1</version>
        </dependency>
    </dependencies>
</project>

@eviltester
Copy link
Author

Just trimming out repos but not wanting to abandon code that might be useful for someone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment