Automate a purchase by adding a book to the cart in flipkart using Selenium

0 votes
May 21 in Selenium by Junaid
179 views

1 answer to this question.

0 votes

Hello Junaid, this code snippet would help you to automate the purchase of a book by adding it to the cart in flipkart using Selenium:

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.chrome.ChromeDriver;
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 AutomateFlipkart {

public static WebDriver driver;

public static String driverPath = "C:\\\\Users\\\\Abha_Rathour\\\\Downloads\\\\ExtractedFiles\\\\chromedriver_win32\\\\chromedriver.exe";



private static final String sSearchResult = "//li[contains(text(),'in')]//span";

private static final String sBookName = "(//a[contains(.,'Selenium')])[last()]";

private static final String sAddToCart = "(//input[contains(@value,'Add to Cart')])[1]";

private static final String sViewCartXPath = "(//a[contains(.,'view cart')])[1]";



public static void initWebDriver(String URL) throws InterruptedException {



// Setting up Chrome driver path.

System.setProperty("webdriver.chrome.driver", driverPath);

// Launching Chrome browser.

driver = new ChromeDriver();

driver.get(URL);

driver.manage().window().maximize();

}



public static void main(String[] args) throws InterruptedException {



initWebDriver("http://www.flipkart.com");



flipkartLogin();



driver.findElement(By.xpath("//div[@class='O8ZS_U']")).sendKeys("Selenium");



WebElement searchResult = getElement(By.xpath(sSearchResult));

searchResult.click();



WebDriverWait wait = new WebDriverWait(driver, 30);



wait.until(ExpectedConditions.elementToBeClickable(By.xpath(sBookName))).click();



wait.until(ExpectedConditions.elementToBeClickable(By.xpath(sAddToCart))).click();



getElement(By.xpath(sViewCartXPath)).click();

getElement(By.cssSelector("form[id='view-cart-form'] button")).click();

getElement(By.xpath("//input[@id='email' and @name='email']")).sendKeys("test@testmail.com");



// pause for a second and close the browser.

Thread.sleep(1000);

endSession();

}



public static WebElement getElement(final By locator) {

FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver).withTimeout(30, TimeUnit.SECONDS)

.pollingEvery(5, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);



WebElement element = wait.until(new Function<WebDriver, WebElement>() {



@Override

public WebElement apply(WebDriver arg0) {

return arg0.findElement(locator);

}



});



return element;

}



public static void flipkartLogin() {

getElement(By.xpath("//input[@class='_2zrpKA']")).sendKeys("user email or mobile number");

getElement(By.xpath("//input[@class='_2zrpKA _3v41xv']")).sendKeys("password");

getElement(By.xpath("//button[@class='_2AkmmA _1LctnI _7UHT_c']")).click();



try {

Thread.sleep(1000);

} catch (InterruptedException e) {

// TBD: Auto-generated catch block.

e.printStackTrace();

}

}



public static void endSession() {

driver.close();

driver.quit();

}

}
answered May 21 by Abha
• 22,870 points

Related Questions In Selenium

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
555 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
890 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
553 views
0 votes
1 answer