通过Assert.assertEquals比较整个url

时间:2018-04-10 05:44:39

标签: selenium selenium-webdriver webdriver testng assert

我想比较网址和打印,所以我使用了以下代码。但它正在比较整个网址,如下所示

对以下网址进行了实际比较:

https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache=

我使用的代码:

String URL = driver.getCurrentUrl();
Assert.assertEquals(URL, "https://accounts.google.com" );
System.out.println(URL);

需要解决方案:

我只想比较'https://accounts.google.com'

请帮我解决这个问题

3 个答案:

答案 0 :(得分:0)

当您访问网址 https://accounts.google.com时,网址设置为:

https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1&ltmpl=default&ltmplcache= 

网址本质上是动态。因此,您无法将assertEquals()用作:

  • assertEquals()定义为:

    void org.testng.Assert.assertEquals(String actual, String expected)
    
    Asserts that two Strings are equal. If they are not, an AssertionError is thrown.
    
    Parameters:
        actual the actual value
        expected the expected value
    

因此assertEquals()将验证两个字符串是否相同。因此,您会看到错误。

解决方案

要在当前网址中声明https://accounts.google.com的存在,您可以使用Assert.assertTrue()函数,如下所示:

String URL = driver.getCurrentUrl();
Assert.assertTrue(URL.contains("https://accounts.google.com"));
System.out.println(URL);

说明

  • assertTrue()定义为:

    void org.testng.Assert.assertTrue(boolean condition)
    
    Asserts that a condition is true. If it isn't, an AssertionError is thrown.
    
    Parameters:
        condition the condition to evaluate
    

答案 1 :(得分:0)

在您的情况下,您不应该使用' assertEquals'而是使用' assertTrue'

Request textDocument/definition failed

答案 2 :(得分:0)

我使用了以下代码,它对我来说很好用

String URL = driver.getCurrentUrl();
    if(URL.contains("url name"))
    {
        System.out.println("Landed in correct URL" +
                "" + URL);

    }else
    {
        System.out.println("Landed in wrong URL");

    }