[英]if else condition on Assert.assertEquals selenium testNG
I am working on selenium and testNG with java.. I have some problem about this code: 我正在使用Java进行硒和testNG。我对此代码有一些疑问:
Assert.assertEquals(webDriver.getCurrentUrl(), "http://google.com");
the question is how to create if else condition in assertEquals. 问题是如何在assertEquals中创建else条件。 like this
像这样
if( Assert.assertEquals(webDriver.getCurrentUrl(), "http://google.com"));
{
//do Nothing
}
else
{
// take screenshoot
}
any idea guys? 有什么主意吗?
If an assert fails, it throws an assertionError. 如果断言失败,则抛出assertionError。 You need to catch the AssertionError and in the catch capture the screenshot.
您需要捕获AssertionError并在捕获中捕获屏幕截图。
try{
Assert.assertEquals(...,...);
}catch(AssertionError e){
Log error;
Takescreenshot;
}
string url = webDriver.getCurrentUrl();
if(url == "http://google.com")
{
// take screenshoot
}
Assert.assertEquals(url, "http://google.com")
If the condition in Assert.assertEquals()
is false, for example Assert.assertEquals("qwerty", "asdfgh")
, the test will terminate, so there is no point to put it in if
statement. 如果
Assert.assertEquals()
的条件为false,例如Assert.assertEquals("qwerty", "asdfgh")
,则测试将终止,因此没有必要将其放入if
语句中。
If you want the test to to take screenshot in failure you can write your on assertEquals
implementation 如果您希望测试失败时截取屏幕截图,则可以编写on
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();
}
}
}
}
And then simply do 然后简单地做
Assert.assertEquals(webDriver.getCurrentUrl(), "http://google.com"));
You can also change stopOnError
to false to prevent test termination when they are not equal. 您也可以将
stopOnError
更改为false,以防止测试不相等时终止测试。
If you don't want the test to end if the URL is wrong simply do 如果您不想在URL错误的情况下结束测试,只需执行
if (!webDriver.getCurrentUrl().equals("http://google.com"))
{
// take screenshot
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.