How to check whether the page loaded with the correct url and tab title

丶灬走出姿态 提交于 2019-12-25 09:16:02

问题


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)

回答1:


That is not an error, your Assertion failed!

You are Asserting ...toLowerCase().contains(submenus[i]). However, every single item in your submenus 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 your submenus items? I would suspect not! Change your Assert to something like:

Assert.assertTrue("checking if URL contains: " + submenus[i],
    driver.getCurrentUrl().toLowerCase().contains(submenus[i].toLowerCase()));

That way you can see exactly when and why your Assert is failing.



来源:https://stackoverflow.com/questions/41704922/how-to-check-whether-the-page-loaded-with-the-correct-url-and-tab-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!