2016-01-20 22 views
0

else条件我硒和TestNG使用Java ..我有这个代码的一些问题:如果Assert.assertEquals硒TestNG的

Assert.assertEquals(webDriver.getCurrentUrl(), "http://google.com"); 

的问题是如何创造的assertEquals如果其他条件。像这样

if(Assert.assertEquals(webDriver.getCurrentUrl(), "http://google.com")); 
{ 
    //do Nothing 
} 

else 
{ 
    // take screenshoot 
} 

任何想法的家伙?

+0

使用没有'assert'的'if'。 –

+0

和assert方法不返回任何东西,所以你不能写入if条件。 – Mahi

回答

0
string url = webDriver.getCurrentUrl(); 
if(url == "http://google.com") 
{ 
    // take screenshoot 
} 

Assert.assertEquals(url, "http://google.com") 
+0

你不应该用'=='测试'String'是否相等(只测试参考标识)。另外,它是'String'(而不是'string')。 –

0

如果Assert.assertEquals()条件为假,例如Assert.assertEquals("qwerty", "asdfgh"),该测试将终止,所以没有点把它放在if声明。

如果您希望测试采取失败的截图可以在assertEquals实施写你

public static class Assert 
{ 
    public static void assertEquals(Object actualResult, Object expectedResult, boolean stopOnError = true) 
    { 
     if (!expectedResult.equals(actualResult)) 
     { 
      // take screenshot 
      if (stopOnError) 
      { 
       throw new Exception(); 
      } 
     } 
    } 
} 

,然后简单地做

Assert.assertEquals(webDriver.getCurrentUrl(), "http://google.com")); 

您还可以更改stopOnError为false,以防止测试当它们不相等时终止。

如果您不想在测试结束,如果URL是错误的根本就

if (!webDriver.getCurrentUrl().equals("http://google.com")) 
{ 
    // take screenshot 
} 
4

如果断言失败,它抛出一个AssertionError。你需要捕捉AssertionError并捕获截图。

try{ 
    Assert.assertEquals(...,...); 
}catch(AssertionError e){ 
    Log error; 
    Takescreenshot; 
}