Skip to content

Instantly share code, notes, and snippets.

@antsmartian
Created August 30, 2015 14:02
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 antsmartian/58ea54e8ec2f7c995b7b to your computer and use it in GitHub Desktop.
Save antsmartian/58ea54e8ec2f7c995b7b to your computer and use it in GitHub Desktop.
Coding Session 2
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
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.FluentWait;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;
public class FlipkartCodingSession2 {
private static final String ADDRESS = "http://www.flipkart.com";
private static final String SEARCH_BOX = "fk-top-search-box";
private static final String SEARCH_RESULT = "//li[contains(text(),'webdriver')]";
private static final String BOOK_NAME = "a[title='Selenium WebDriver (English) 1st Edition (Paperback) ']";
private static final String ADD_TO_CART = "input[data-buy-listing-id='LSTBOK9789332526297270AEB'][value='Add to Cart']";
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get(ADDRESS);
driver.findElement(By.id(SEARCH_BOX)).sendKeys("Selenium");
WebElement seleniumSearchResult = fluentWait(By.xpath(SEARCH_RESULT), driver);
seleniumSearchResult.click();
WebDriverWait wait = new WebDriverWait(driver, 40);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(BOOK_NAME)))
.click();
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(ADD_TO_CART)))
.click();
}
public static WebElement fluentWait(final By locator, WebDriver driver)
{
FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(30,TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
//f(x) = x , is any integer and gives back y , which is an any integer
WebElement element = wait.until(new Function<WebDriver,WebElement>() {
@Override
public WebElement apply(WebDriver arg0) {
// TODO Auto-generated method stub
return arg0.findElement(locator);
}
});
return element;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment