简体   繁体   English

硒(Java),java.lang.AssertionError

[英]Selenium(Java), java.lang.AssertionError

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.该方法有效,但是当我添加代码来检查正确的 URL 和选项卡标题时,我的测试没有执行。 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.有时会发生for循环快速点击未加载一侧的页面,我不知道这是不是问题,但我无法检查页面是否加载了正确的url或标签标题,或者问题是我为检查正确的 url 或标签标题而编写的代码。

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)

One of your asserts is returning false, so your current title or url doesn't contain submenus[i]您的断言之一是返回 false,因此您当前的标题或网址不包含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.您在此处将 URL 转换为小写( driver.getCurrentUrl().toLowerCase() ),但您将它与您的子菜单进行比较,它不是小写。 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM