1

I wrote a method to load the page navigation links. The method works, but when I added code to check the correct URL and tab title my test is not performed. Sometimes it happens that for loop fast clicks on the pages the side that does not get loaded, I do not know whether it is a problem but I can not check whether a page loaded with the correct url or tab title, or the problem is the code that I wrote for check the correct url or tab title.

This is my method:

public void showNavigationLinks(){
        Actions action = new Actions(driver);

        String[] submenus = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"};

        for(int i=0;i<submenus.length;i++)
        {

            WebElement we = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
            wait(2000);
            action.moveToElement(we).moveToElement(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]"))).click().build().perform();
            wait(3000);

            waitForElementToBeDisplayed(driver.findElement(By.xpath("//a[contains(.,'"+submenus[i]+"')]")) , 500);
            Assert.assertTrue(driver.getCurrentUrl().toLowerCase().contains(submenus[i]));

            Assert.assertTrue(driver.getTitle().contains(submenus[i]));
        }

        link_all_product.click();
    }

This is my error:

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:86)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at org.junit.Assert.assertTrue(Assert.java:52)
    at PageObject.ProductPage.showNavigationLinks(ProductPage.java:627)
MY DZON
  • 109
  • 1
  • 4
  • 9
  • Possible duplicate of [How to check whether the page loaded with the correct url and tab title](http://stackoverflow.com/questions/41704922/how-to-check-whether-the-page-loaded-with-the-correct-url-and-tab-title) – SiKing Jan 19 '17 at 00:46

1 Answers1

1

One of your asserts is returning false, so your current title or url doesn't contain submenus[i]

You're converting the URL to lowercase here (driver.getCurrentUrl().toLowerCase()), but you're comparing it to your submenus, which isn't lowercase. This is probably your problem. Here is the fix:

String expected = submenus[i].toLowerCase();
String actualUrl = driver.getCurrentUrl().toLowerCase();

Assert.assertTrue(actualUrl.contains(expected));

For debugging purposes, you can step through your code to see what's happening, and/or you can make your error more meaningful:

Assert.assertTrue("Expected: " + actualUrl + " to contain: " + expected,
    actualUrl.contains(expected))
mrfreester
  • 1,981
  • 2
  • 17
  • 36
  • 1
    Code does not arrive to check all sides, falling when you check page 3. Code for debugging does not work, get me this error: Assert cannot be applied to (boolen, java.lang.String) – MY DZON Jan 18 '17 at 23:10
  • When the test fall on page 3 shows this error: java.lang.AssertionError – MY DZON Jan 18 '17 at 23:13
  • Sorry I've fixed the assert, got my test frameworks mixed up. The message should be first, and the condition second. Try again please :) – mrfreester Jan 18 '17 at 23:14
  • Displays this error: java.lang.AssertionError. Now does not arrive first page to check. :( Marks this line of code: Assert.assertTrue (actualUrl.contains (expected)); – MY DZON Jan 18 '17 at 23:22
  • Does your assertion Error tell you the expected and actual url's now? Did you use `Assert.assertTrue("Expected: " + actualUrl + " to contain: " + expected, actualUrl.contains(expected))`? If it's working sometimes and not others, my guess is you'll end up doing some explicit waits. I can help with that depending on what your errors show. – mrfreester Jan 18 '17 at 23:25
  • Yes, I used to. Showing me the error: java.lang.asertionerror and marks this line of code: Assert.assertTrue (actualUrl.contains (expected)); – MY DZON Jan 18 '17 at 23:32
  • Can you help me how to write explicit waits? – MY DZON Jan 18 '17 at 23:54
  • I would take a look at the answer here for elements to be clickable: http://stackoverflow.com/questions/16057031/webdriver-how-to-wait-until-the-element-is-clickable-in-webdriver-c-sharp That being said, you might need to adjust this to wait for something on the new page to be there, before you try to click the next link. It sounds like you might be running into a situation where you click the next link before the previous one gets a chance to load. – mrfreester Jan 19 '17 at 22:02