简体   繁体   English

如果在Assert.assertEquals硒测试NG上有其他条件

[英]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.

相关问题 Selenium Web驱动程序-Java-TestNG-Assert.assertEquals如何将实际结果与预期结果之间的范围进行比较 - Selenium Web Driver - Java - TestNG - Assert.assertEquals how to compare actual result with the range between expected result Selenium可以对测试(Assert.assertEquals())失败进行截图吗? - Can Selenium take a screenshot on test(Assert.assertEquals()) failure? Assert.assertEquals在两个列表上 - Assert.assertEquals on two lists Assert.assertEquals的奇怪行为 - Strange behaviour of Assert.assertEquals assertEquals()和Assert.assertEquals()之间的JUnit测试差异 - JUnit test difference between assertEquals() and Assert.assertEquals() selenium TestNG 中的断言(assert 类型的 assertEquals(String, String) 方法未定义) - Asserts in selenium TestNG (The method assertEquals(String, String) is undefined for the type Assert) webDriver java selenium:如果/ else具有asserttrue assertEquals和condition或/ and - webDriver java selenium: if /else with asserttrue assertEquals and condition or /and 如何使用 Assert.assertEquals() 测试单链表 - How to test Single Linked List using Assert.assertEquals() Assert.assertEquals 没有正确比较 String - Assert.assertEquals didn't compares String right way java-有时返回字符串且有时返回null时如何使用Assert.assertEquals - java - How to use Assert.assertEquals when sometimes a String is returned and sometimes null is returned
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM