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) 
1

1 Answer 1

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)) 
8
  • 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
    CommentedJan 18, 2017 at 23:10
  • When the test fall on page 3 shows this error: java.lang.AssertionError
    – MY DZON
    CommentedJan 18, 2017 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 :)CommentedJan 18, 2017 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
    CommentedJan 18, 2017 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.CommentedJan 18, 2017 at 23:25

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.