3

I'm trying to compare two different tables using the method called VerifyOrderSummary located at the end of the main method below.

public static void main(String[] args) {

    // TODO Auto-generated method stub

    System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    driver = new FirefoxDriver();   

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    Item testItem = Item.WRENCH;

    driver.get("http://test.url/WebOrderScreen");
    AddItem(testItem);
    ChangeItemQuantity(testItem, 3);

    WebElement shoppingCartListContents = driver.findElement(By.id("shopping-cart")).findElement(By.xpath(".//tbody[@class='nx-list-contents']"));
    List<WebElement> shoppingCartRows = shoppingCartListContents.findElements(By.tagName("tr"));

    System.out.println(shoppingCartRows.size());
    System.out.println(shoppingCartRows.get(0).getText());

    ClickWorkflowButton(Button.PAYMENTINFORMATION);

    WebElement orderSummaryListContents = driver.findElement(By.id("order-summary")).findElement(By.xpath(".//tbody[@class='nx-list-contents']"));
    List<WebElement> orderSummaryRows = orderSummaryListContents.findElements(By.tagName("tr"));

    VerifyOrderSummary(shoppingCartRows, orderSummaryRows);


}

The issue i am seeing is that when i print out the first row of shoppingRowCartsRows in the main method, it prints out correctly.

but within the VerifyOrderSummary method, it prints out a blank line:

 public static void VerifyOrderSummary(List<WebElement> shoppingCartRows, List<WebElement> orderSummaryRows) {      

    //given the shopping cart rows and order summary rows, compare the two

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

        System.out.println("shopping cart row: "+shoppingCartRows.get(i).getText());
        System.out.println("order summary row: "+orderSummaryRows.get(i).getText());


        //          Assert.assertEquals(shoppingCartRows.get(i).getText(), orderSummaryRows.get(i).getText());

        //System.out.println(models.get(i).getName());

    }

Can anyone tell me why that is happening? I'm completely lost at the moment.

3
  • Which line is printed blank?
    – Alexey R.
    Commented Oct 29, 2017 at 22:21
  • 6
    I would be interested if ClickWorkflowButton(Button.PAYMENTINFORMATION); navigates away from the page you collected your rows from. My initial guess is that the elements you found might have gone stale before you tried to use them a second time.
    – Cherree
    Commented Oct 31, 2017 at 11:33
  • I hope when navigate to payment info, the items in the shopping cart is removed from session of your application. Please can you check the cookie variables in the shopping cart page as well as in the order review page.
    – Bhavani
    Commented Nov 8, 2017 at 18:10

1 Answer 1

1

I would not recommend to use WebElement objects as the carriers of some long-life information. They might change depending on the changes of your page. Instead I would recommend to have the following approach:

  1. You have WebElement list that you have just populated somehow:

    List<WebElement> weList = driver.findElements(By.xpath("//some-x-path"));
    
  2. Then you should create a list holding the types of valuable parts of your Webelements. For text it would look like:

    2.1. For Java 8:

    either

    List<String> weText = new ArrayList<>();
    weList.forEach(webElement -> {weText.add(webElement.getText());});
    

    or

    List<String> weText  = weList.stream().map(webElement -> webElement.getText()).collect(Collectors.toList());
    

    2.2. For pre-8 Java:

    List<String> weText = new ArrayList<>();
    for(WebElement webElement: weList){
        weText.add(webElement.getText());
    }
    

Now you can preserve the values for further comparison.

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.