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

0 votes
May 21, 2019 in Selenium by Junaid
5,755 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 22, 2019 by Abha
• 28,140 points

Related Questions In Selenium

+1 vote
1 answer

How can I automate the process of adding iPhone to cart in Flipkart using Selenium(java),Page Object Model and TestNG? Also validate if product is added and available in cart?

Hey check this https://www.edureka.co/community/47160/automate-purchase-adding-book-cart-flipkart-using-selenium? It deals with a similar ...READ MORE

answered Jan 13, 2020 in Selenium by Karan
• 19,610 points
7,864 views
0 votes
2 answers
0 votes
2 answers

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,500 points
12,749 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
8,622 views
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,910 points
6,697 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,561 views
+1 vote
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP