Module-17 Facing issues when working with https://uk.webuy.c | Selenium Forum
M
Posted on 11/12/2015
Hi Ashish,

I was facing issue when trying to perform same excercise: "https://uk.webuy.com/search/index.php?stext=*&section=&catid=956"
When I am running the test, I am getting different results everytime. Sometime 3 items added to card and sometime 5.
Looks like, "I want to buy this item" is not getting click and due to that basket count is not getting match and assertion fails.

I am not able to find the reason why script is failing and not giving me consistent results in each run. All the items I wanted to add are available, not out of stock.

- Why below statements gives "15" as output, as page contains more than 15 products having similar xpath structure.
System.out.println("Total names = "+ allNames.size());
System.out.println("Total buttons = "+ allButtons.size());

Thanks in advance.

M
Replied on 11/12/2015

post the entire code here i will take look at it.


M
Replied on 14/12/2015

Hi Ashish,

It seems the products refreshes when we scroll till the last element. I have used the Actions class to extract all the elements, on doing so i got Stale Element Exception. I extracted product names and sizes again as it is lapsed every time link is clicked. Still getting an error.

Please suggest the needful. Entire code is pasted below for reference-

import java.util.ArrayList;
import java.util.Hashtable;
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.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;






public class Webuy {


@Test
public void testProductpurchaseProcess() throws InterruptedException
{

Xls_Reader xls=new Xls_Reader("C:\\Users\\admin\\Desktop\\tp\\selenium\\Datatable.xlsx");

int rows=xls.getRowCount("Products");

List<String> productsnametobeadded=new ArrayList<String>();

Hashtable<String,String> productsquantitytobeadded=new Hashtable<String,String>();

for(int i=2;i<=rows;i++)
{
String productName=xls.getCellData("Products", "Product Name", i);
String productquantity=xls.getCellData("Products", "Quantity", i);
System.out.println(productName+ "=====" + productquantity);

productsnametobeadded.add(productName);
productsquantitytobeadded.put(productName,productquantity);


}

WebDriver driver=new FirefoxDriver();

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://uk.webuy.com/search/index.php?stext=*&section=&catid=956");


List<WebElement> allOptions=driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
List<WebElement> allButtons=driver.findElements(By.xpath("//div[@class='action']/div/a[2]"));
System.out.println("Total names" + "=" +allOptions.size());
System.out.println("Total buttons" + "=" +allButtons.size());


Actions act=new Actions(driver);


WebElement newlist=allOptions.get(allOptions.size()-1);
act.moveToElement(newlist).build().perform();
Thread.sleep(3000);

allOptions=driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
allButtons=driver.findElements(By.xpath("//div[@class='action']/div/a[2]"));


System.out.println("Total Size is" +allOptions.size());
System.out.println("TOtal buttons is" +allButtons.size());


int prodCount=0;
for(int i=0;i<allOptions.size();i++)
{


if(productsnametobeadded.contains(allOptions.get(i).getText()))

{


System.out.println("Found"+ "---"+allOptions.get(i).getText());
allButtons.get(i).click();


String count=driver.findElement(By.id("buyBasketCount")).getText();
System.out.println(count);

Assert.assertEquals(count,String.valueOf(prodCount+1));



prodCount++;

}

WebElement newlist1=allOptions.get(allOptions.size()-1);
act.moveToElement(newlist1).build().perform();
Thread.sleep(3000);

allOptions=driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
allButtons=driver.findElements(By.xpath("//div[@class='action']/div/a[2]"));

}



driver.findElement(By.xpath("//*[@id='buyBasketRow']/td[2]/a")).click();
List<WebElement> selects=driver.findElements(By.xpath("//div[@class=''basketPageBox]/form/table/tbody/tr/td[1]/div/select"));
List<WebElement> secondcol=driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[2]"));

for(int i=0;i<secondcol.size()-2;i++)
{

String key=secondcol.get(i).getText().split("\\n")[0];

System.out.println(key + "====" +productsquantitytobeadded.get(key));

WebElement dropdown=selects.get(i);

Select s=new Select(dropdown);
s.selectByVisibleText("productsquantitytobeadded.get(key)");

selects=driver.findElements(By.xpath("//div[@class=''basketPageBox]/form/table/tbody/tr/td[1]/div/select"));
secondcol=driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[2]"));
}

double sum=0.0;
List<WebElement> fourthcol=driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[4]"));

for(int i=0;i<fourthcol.size();i++)
{
String value=fourthcol.get(i).getText().split("\\£")[1];
System.out.println(value);

sum=sum+Double.parseDouble(value);
}

System.out.println("Total cost: " + sum);


int totalrows=driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr")).size();
String delCharges=driver.findElement(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr["+(totalrows-1)+"]/td[2]")).getText();
String expectedTotal=driver.findElement(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr["+(totalrows)+"]/td[2]")).getText();

System.out.println("Delievery charges:" +delCharges);
System.out.println("Expected total:" +expectedTotal);

double charges=Double.parseDouble(delCharges.split("\\£")[1]);

double actualtotal=sum+charges;

double expectedtotal=Double.parseDouble(expectedTotal.split("\\£")[1]);

Assert.assertEquals(actualtotal, expectedtotal);
}
}


Thanks in advance!


M
Replied on 14/12/2015

hi,

plz upload Datatable.xlsx


M
Replied on 15/12/2015

Hi,

PFA Datatable.xlsx


M
Replied on 15/12/2015

i am getting stale element exception at this code


WebElement newlist1 = allOptions.get(allOptions.size() - 1);
act.moveToElement(newlist1).build().perform();
Thread.sleep(3000);


M
Replied on 15/12/2015

I am also stuck here Responsive image

Not sure why this is happening though we are getting new size for allOptions() and allButtons()

Firstly, it gives-

allOptions().size=15
allButtons().size=15

After when the products are refreshed, it gives-

allOptions().size=60
allButtons().size=60

Please help me to proceed further. Thanks!


M
Replied on 15/12/2015

your program is very big. a website such as uk.webuy.com has many dynamic elements.

stale element exception can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. To allow for those cases, you can try to access the element several times in a loop before finally throwing an exception.

http://darrellgrainger.blogspot.com/2012/06/staleelementexception.html


M
Replied on 15/12/2015

Please find below code and attachment:

import java.util.ArrayList;
import java.util.Hashtable;
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.support.ui.Select;
import org.testng.Assert;
import org.testng.annotations.Test;

public class AddtoCart {

@Test
public static void readfromXLS() throws InterruptedException
{

Xls_Reader xls = new Xls_Reader(System.getProperty("user.dir")+ "\\src\\Data.xlsx");
int rows = xls.getRowCount("product");
System.out.println("Total rows in xls -> " + rows);
List<String> productsNamesToBeAdded = new ArrayList<String>();
Hashtable<String,String> productQuatityToBeAdded = new Hashtable<String,String>();
for(int i=2;i<=rows;i++){
String productName = xls.getCellData("product", "ProductName", i);
String productQuantity = xls.getCellData("product", "Quantity", i);

System.out.println(productName +" --- "+productQuantity);
productsNamesToBeAdded.add(productName);
productQuatityToBeAdded.put(productName,productQuantity);
}

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("https://uk.webuy.com/search/index.php?stext=*&section=&catid=956");
driver.findElement(By.xpath("//div[@class='deliver-component-wrapper']")).click();
List<WebElement> allNames = driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
List<WebElement> allButtons = driver.findElements(By.xpath("//div[@class='action']/div/a[2]"));

System.out.println("Total names = "+ allNames.size());
System.out.println("Total buttons = "+ allButtons.size());

for(int j =0;j<allButtons.size();j++)
{System.out.println("Line no: "+ " "+ j + allNames.get(j).getText()+"------"+allButtons.get(j).getText());}
int prodCount=0;
for(int i=0;i< allNames.size();i++){

if(productsNamesToBeAdded.contains(allNames.get(i).getText())){
System.out.println("FOUND --- " +allNames.get(i).getText());

allButtons.get(i).click();

// validate basket count
String count = driver.findElement(By.id("buyBasketCount")).getText();
System.out.println("Basket count - "+ count);
Assert.assertEquals(count, String.valueOf(prodCount+1));
allNames = driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
allButtons = driver.findElements(By.xpath("//div[@class='action']/div/a[2]"));
prodCount++;
}

}
// change the quantities
driver.findElement(By.linkText("View Basket")).click();
List<WebElement> selects =driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[1]/div/select"));
System.out.println("Total select Boxes - "+ selects.size());
List<WebElement> secondCol = driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/td[2]"));

for(int i=0;i<secondCol.size()-2;i++){
String key=secondCol.get(i).getText().split("\\n")[0];
System.out.println(key +" ---- " +productQuatityToBeAdded.get(key));
WebElement dropDown = selects.get(i);
Select s = new Select(dropDown);
s.selectByVisibleText(productQuatityToBeAdded.get(key));

secondCol =driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[2]"));
selects =driver.findElements(By.xpath("//div[@class='basketPageBox']/form/table/tbody/tr/td[1]/div/select"));
}


}
}


M
Replied on 15/12/2015

hi dimple,

what do you want me do with your code?


M
Replied on 17/12/2015

I have uploaded code and data file as you have asked in earlier in this forum's reply.
Can you please help me with below queries:

When I am running the test, I am getting different results everytime. Sometime 3 items added to card and sometime 5.
Looks like, "I want to buy this item" is not getting click and due to that basket count is not getting match and assertion fails.

I am not able to find the reason why script is failing and not giving me consistent results in each run. All the items I wanted to add are available, not out of stock.

- Why below statements gives "15" as output, as page contains more than 15 products having similar xpath structure.
System.out.println("Total names = "+ allNames.size());
System.out.println("Total buttons = "+ allButtons.size());

Thanks.
Dimple


M
Replied on 18/12/2015

your code is fine sometimes website does not have [color=#FF0000:21m35t37]Sims 2 Pets (SN)[/color:21m35t37] and it gives a error.

Responsive image

M
Replied on 30/12/2015

Thanks for reply. The another query related to this exercise is:

- Why below statements gives "15" as output, as page contains more than 15 products having similar xpath structure.

List<WebElement> allNames = driver.findElements(By.xpath("//div[@class='searchRecord']/div[2]/h1/a"));
List<WebElement> allButtons = driver.findElements(By.xpath("//div[@class='action']/div/a[2]"));

System.out.println("Total names = "+ allNames.size());
System.out.println("Total buttons = "+ allButtons.size());

Can you please help me to resolve above issue?


M
Replied on 30/12/2015

it is showing 15 because that is what the website show.

after you scroll it increases the number of items.