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++)
{
Assert.assertTrue(driver.getCurrentUrl().toLowerCase().contains(submenus[i]));
Assert.assertTrue(driver.getTitle().contains(submenus[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);
}
link_all_product.click();
}
This is my error:
Starting ChromeDriver 2.25.426923 (0390b88869384d6eb0d5d09729679f934aab9eed) on port 2140
Only local connections are allowed.
Jan 17, 2017 4:51:23 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
Jan 17, 2017 4:51:31 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
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:620)
That is not an error, your Assertion failed!
You are Asserting
...toLowerCase().contains(submenus[i])
. However, every single item in yoursubmenus
contains uppercase characters!You could change your Assertion to something like
...toLowerCase().contains(submenus[i].toLowerCase())
.The next problem is going to be: Does your URL, from
driver.getCurrentUrl()
really contain every single one of yoursubmenus
items? I would suspect not! Change yourAssert
to something like:That way you can see exactly when and why your
Assert
is failing.