自动检查正确的网址和标题页

时间:2017-01-31 17:27:35

标签: java selenium selenium-webdriver automation selenium-chromedriver

我尝试创建一个方法,以便检查网页是否加载了正确的网址和标题。 我的代码通过几次,几次下降。 这是我的错误:

java.lang.AssertionError: Checking if http://store.demoqa.com/products-page/product-category/accessories/ contains: imacs

这是我的代码:

public void showNavigationLinks() {

        Actions action = new Actions(driver);
        String[] submenu = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"};

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

            WebElement productCategory = waitForElementToBeDisplayed(By.xpath("//a[contains(.,'Product Category')]"), 5000);
            action = action.moveToElement(productCategory);

            WebElement submenuItem = waitForElementToBeDisplayed(By.xpath("//a[contains(.,'" + submenu[i] + "')]"), 5000);
            action.moveToElement(submenuItem).click().build().perform();

            String currentUrl = driver.getCurrentUrl();
            String title = driver.getTitle();

            Assert.assertTrue("Checking if " + currentUrl + " contains: " + submenu[i].toLowerCase(),
                    currentUrl.contains(submenu[i].toLowerCase()));

            Assert.assertTrue("Checking if title contains: " + submenu[i],
                    title.contains(submenu[i]));
            System.out.println(title);
        }

2 个答案:

答案 0 :(得分:0)

您获得AssertionError的原因是因为您的断言失败了。提取的网址是&#34; http://store.demoqa.com/products-page/product-category/accessories/&#34;而你期望网址包含&#34; imacs&#34; (我想您希望URL为http://store.demoqa.com/products-page/product-category/imacs/)导致断言失败。

在您的代码中,您正在等待子菜单项&#34; imacs&#34;要显示,然后单击子菜单选项并验证是否已加载子菜单选项的URL。此操作始终不会产生相同的结果,因为页面可能需要一段时间才能加载,您可能正在获取以前的URL。我建议在获取页面的URL和标题之前等待新页面完全加载。

答案 1 :(得分:-1)

请尝试以下操作:{它不使用断言但只有当它们与您的子菜单条目匹配时才打印标题和网址:

package package1;

import java.awt.AWTException;

import org.testng.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class new1 {

static WebDriver driver = null;

public static void main(String[] args) throws InterruptedException, AWTException {

    System.setProperty("webdriver.chrome.driver","C:\\Eclipse\\Selenium\\chromedriver.exe");

    driver = new ChromeDriver();

    driver.manage().window().maximize();

    driver.get("http://store.demoqa.com/products-page/product-category/accessories/");

    Thread.sleep(5000);

    showNavigationLinks() ;

}

public static void showNavigationLinks() {

    Actions action = new Actions(driver);
    String[] submenu = {"Accessories", "iMacs", "iPads" , "iPhones" , "iPods" , "MacBook"};

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

        WebElement productCategory = driver.findElement(By.xpath("//a[contains(.,'Product Category')]"));
        action = action.moveToElement(productCategory);

        WebElement submenuItem = driver.findElement(By.xpath("//a[contains(.,'" + submenu[i] + "')]"));
        action.moveToElement(submenuItem).click().build().perform();

        String currentUrl = driver.getCurrentUrl();
        String title = driver.getTitle();

        if(currentUrl.contains(submenu[i].toLowerCase())){

            System.out.println("CurrentUrl is " + currentUrl + "  " + submenu[i].toLowerCase());

        }



        if(title.contains(submenu[i])){

            System.out.println("title is " + title + "  " + submenu[i]);

        }



    }

}

}