简体   繁体   English

Selenium和AssertTrue()连接

[英]Selenium and AssertTrue() connection

I'm using Selenium in Eclipse/Java and I have a Try/Catch like this: 我在Eclipse / Java中使用Selenium,并且有这样的Try / Catch:

            try {
        assertTrue(selenium.isTextPresent("You Are Now Logged xxxOut"));
        System.out.println("You Are Now Logged Out is present on the web page");
        }
        catch (Throwable e) {
        System.out.println("You Are Now Logged Out is NOT present on the web page");
        }           

I guess I'm missing the Selenium connection with this forced failure (xxxOut) as how to get Selenium to report this as a failure? 我想我缺少带有此强制故障(xxxOut)的Selenium连接,因为该如何将Selenium报告为故障? My TestNG reports my script ran OK without failures but if I look at the console I see "You Are Now Logged Out is NOT present on the web page", so I did have a failure (the expected text wasn't there). 我的TestNG报告说我的脚本运行正常,没有失败,但是如果我查看控制台,我会看到“您现在已注销,但网页上不存在”,所以我确实失败了(期望的文本不存在)。

Thanks... 谢谢...

Since you have caught the failure, the test has passed. 由于您已捕获故障,因此测试已通过。 If you want to log your custom message and still report it as a failure, you should throw it again. 如果要记录自定义消息并仍然将其报告为失败,则应再次抛出该消息。 Add throw e after your sysout and the case would be reported as a failure. 在您的sysout之后添加throw e,该案例将被报告为失败。

If you do not want to log custom message, then do not catch it at all. 如果您不想记录自定义消息,则根本不要捕获它。

Firstly, is the try/catch block necessary? 首先,try / catch块是否必要? If none of the methods under test are likely to throw exceptions it is preferable to keep the test simple 如果被测方法均未抛出异常,则最好保持测试简单

assertTrue(selenium.isTextPresent("You Are Now Logged xxxOut"));

If the failure message doesn't need to be output to any particular output stream, but you want to retain custom messages 如果不需要将故障消息输出到任何特定的输出流,但是您想要保留自定义消息

String failureMsg = "You Are Now Logged Out is NOT present on the web page";
assertTrue(selenium.isTextPresent("You Are Now Logged xxxOut"), failureMsg);
String successMsg = "You Are Now Logged Out is present on the web page";
System.out.println(successMsg);

If logging is absolutely essential in your test 如果在测试中日志记录绝对必要

try {
    assertTrue(selenium.isTextPresent("You Are Now Logged xxxOut"));
    System.out.println("You Are Now Logged Out is present on the web page");
} catch (Throwable e) {
    String failureMsg = "You Are Now Logged Out is NOT present on the web page";
    fail(failureMsg, e)
    System.out.println(failureMsg);
}

As a matter of style I prefer the first option. 考虑到样式,我更喜欢第一种选择。 Unless there is a very compelling reason to wrap the code in a Try/Catch I would advise against it because: 除非有非常令人信服的理由将代码包装在“尝试/捕获”中,否则我建议不要这样做,因为:

  1. it isn't necessary based on the provided snippet; 不必根据提供的代码段; the assertTrue() call will throw an AssertionError which will fail the test cleanly and there is an overloaded version that allows for a custom message assertTrue()调用将引发AssertionError,这将使测试彻底失败,并且存在一个允许自定义消息的重载版本
  2. catching all Throwable instances will catch the AssertionError instance and cause you to rethrow it (unnecessary), or call fail() (redundant, it just throws a new AssertionError) 捕获所有Throwable实例将捕获AssertionError实例并导致您将其重新抛出(不必要),或调用fail()(冗余,它只会抛出一个新的AssertionError)
  3. rethrowing an exception other than AssertionError will cause the test to fail with an Error condition rather than a Failure condition, indicating that a test failed in a way that you didn't anticipate, which is generally considered a more serious error 重新抛出AssertionError以外的异常将导致测试失败,并显示错误条件而不是失败条件,这表明测试以您无法预料的方式失败,通常认为这是更严重的错误

Additionally it is generally not recommended to catch instances of Throwable. 另外,通常不建议捕获Throwable实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM