Last active
September 12, 2015 10:29
-
-
Save antsmartian/9d1d6f9f79806288a047 to your computer and use it in GitHub Desktop.
Project Changed - Session
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 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 ProjectChangedSession { | |
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(),'in')]//span"; | |
private static final String BOOK_NAME = "//a[contains(text(),' Design Patterns and Best Practices (English) (Paperback) ')]"; | |
private static final String ADD_TO_CART = "input[data-buy-listing-id='LSTBOK9781783982707JPMTAC'][value='Add to Cart']"; | |
public static void main(String[] args) throws Exception { | |
WebDriver driver = new FirefoxDriver(); | |
driver.get(ADDRESS); | |
login(driver); | |
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.xpath(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) = y (is a function which takes x and gives y). Both x and y are 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; | |
} | |
public static void login(WebDriver driver) | |
{ | |
driver.findElement(By.linkText("Login")).click(); | |
//TODO: replace with your username and password of flipkart! | |
fluentWait(By.cssSelector("input[placeholder='Enter email/mobile']"), driver).sendKeys(""); | |
fluentWait(By.cssSelector("input[placeholder='Enter password']"), driver).sendKeys(""); | |
fluentWait(By.cssSelector("input[value='Login'][class='submit-btn login-btn btn']"), driver).click(); | |
//sleep is required here, as the website blinks for an moment! | |
try { | |
Thread.sleep(3000); | |
} catch (InterruptedException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment