Module 17 WeBuyExample : Products not added to Cart | Selenium Forum
M
Posted on 21/02/2016
Hi

I have written the code and have checked multiple times. The logic is fine but for some reason I am not able to add items in the cart.

Need help.

Here is the code

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

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.interactions.Actions;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.openqa.selenium.support.events.internal.EventFiringMouse;
import org.testng.annotations.Test;

public class WeBuyExample {

@Test
public void testProductPurchase() throws InterruptedException
{
Xls_Reader p = new Xls_Reader("C:\\TestData\\webuy.xlsx");
int row = p.getRowCount("Sports");
System.out.println("Total rows are :" + row);
List<String> producttobeselected = new ArrayList<String>();
List<String> productquantity = new ArrayList<String>();

List<WebElement> allpdtlist = null;
List<WebElement> allcartbuttons = null;
for (int i=2;i<=row;i++)
{
String product = p.getCellData("Sports", "Product Name", i);
String qty = p.getCellData("Sports", "Quantity", i);
// String p_sub = product.substring(0, 20);
System.out.println(product + "----------" + qty);
// System.out.println("Part of the string -->" + p_sub);
producttobeselected.add(product);
productquantity.add(qty);
}

WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.navigate().to("https://au.webuy.com/search/index.php?stext=*&section=&catid=850");
Actions ac = new Actions(driver);
MyListener listener = new MyListener();
EventFiringMouse mouse = new EventFiringMouse(driver, listener);


WebElement sportsitems = driver.findElement(By.xpath("//html/body/div[5]/div[1]/div[3]/div[5]"));
allpdtlist = sportsitems.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
allcartbuttons = sportsitems.findElements(By.xpath("//div[@class='prodPrice']/a[2]/div"));
System.out.println("Total products are :" +allpdtlist.size());
System.out.println("Total Cart Buttons :->" + allcartbuttons.size());


for (int n =0;n<allpdtlist.size();n++)
{
System.out.println("------------------");
System.out.println("Popular products on site are :" + allpdtlist.get(n).getText());
System.out.println("Popular products on site are :" + allpdtlist.get(n).isDisplayed());

if (producttobeselected.contains(allpdtlist.get(n).getText()))
{
System.out.println("Found --->" + allpdtlist.get(n).getText());
Thread.sleep(3000);
allcartbuttons.get(n).click();
//allpdtlist = sportsitems.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
//allcartbuttons = sportsitems.findElements(By.xpath("//div[@class='prodPrice']/a[2]/div"));
//ac.moveToElement(allpdtlist.get(n));
//Locatable item = (Locatable) allpdtlist.get(n);
//Coordinates c = item.getCoordinates();
//mouse.mouseDown(c);
//driver.findElement(By.xpath("//ul[@id='homefeatured']/li[1]/div/div[2]/div[2]/a[1]/span")).click();
}
else
{
System.out.println("Not Found");
}


}

}

}

M
Replied on 24/02/2016

the items you're trying to buy are out of stock.