2

I am trying to verify selected item is exists in another (Cart/Shopping Bag) page, for EX: I am buying one/many items from application and then I will move all the items to Cart/Shopping Bag, here I want to verify the previously selected item is same as in your Cart/Shopping Bag.

Please let me know how to compare items in both/different the pages.

Test URL is: http://www.myntra.com/

Blow are the code sample and screenshots.

    import java.util.List;
    import java.util.concurrent.TimeUnit;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium.*;
    import org.testng.annotations.Test;

    public class CartListInMyntra {
    static WebDriver driver = new FirefoxDriver();
    @Test
    public void method() {
    driver.get("http://www.myntra.com");
    driver.manage().window().maximize();
    driver.findElement(By.linkText("Login")).click();
    driver.findElement(By.xpath(".//*[@id='login']/div[2]")).click();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebDriverWait wait = new WebDriverWait(driver, 20);
    WebElement ele =wait.until(ExpectedConditions.elementToBeClickable(By.id("q")));
    driver.findElement(By.id("q")).sendKeys("puma");
    driver.findElement(By.className("icon-search")).click();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    WebElement table = driver.findElement(By.className("results-cnt"));// 
    List<WebElement> rows=table.findElements(By.className("product"));
    int count = rows.size();
    for (WebElement items : rows){
    System.out.println(items.getText());
    }

Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("/html/body/div[2]/div/div[2]/div[4]/ul/li[1]"));     
action.moveToElement(we).build().perform();
driver.findElement(By.xpath("html/body/div[2]/div/div[2]/div[4]/ul/li[1]/div[1]/span[1]/div[1]")).click();
driver.findElement(By.xpath("*//div[@class='buy-opts']/div[2]/button[1]")).click();

WebElement viewCart = driver.findElement(By.xpath("html/body/div[1]/div[2]/div[1]/a[3]"));
action.moveToElement(viewCart).build().perform();
driver.findElement(By.xpath("html/body/div[1]/div[8]/div[2]/div[3]/a")).click();

  WebElement view = driver.findElement(By.className("prod-name"));
  List<WebElement>selected_Items =view.findElements(By.tagName("a"));       
  for (WebElement sel_items : selected_Items){
  System.out.println(sel_items.getText());
  }
  if(rows.get(0).equals(selected_Items.get(0))){
  System.out.println("EQUALS");
  }else{
  System.err.println("NOT EQUALS");
  }
  }
  }

Compare 01

Compare 02

1

2 Answers 2

3

You are comparing values, so better you store each values in the String and compare both the String values.

0

I think the easiest way to do this would be to manipulate the javascript on the page to get the two strings you need. First go to that primary shopping page and have the browser click the quick view this will load and give you a way to select the text use document.querySelector('h1 > a').textContent; and you should get something like "Roadster Men White Printed T-shirt " you may need to trim the trailing white space and store in a variable. then add it to your cart and on the next page use document.querySelector('div.prod-name > a').textContent; then compare your two strings.

2
  • I am unable to do this (Jquery), if possible could you please give me the query. If possible.
    – QA4it
    Commented Sep 13, 2014 at 8:28
  • 2
    Selenium has a javascript executor otherwise use something else like casperjs it's easier to execute javascript on the page. dropbox.com/s/f5o9esbw0fn0n0k/demo.mov?dl=0
    – alanmbarr
    Commented Sep 14, 2014 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.