1

I have been trying for a while now to use the "Verify" option in Selenium as opposed to the "Assert" option. My thought being that with a Verify option is that my test can be run completely and then to get a list of the errors. In trying to do so I have realized that "code wise" the Verify option is the same as Assert but it is just surrounded by the try-catch block. I have tried various options to make this work, but the issue I am facing is that the test still stops on the statement that breaks as opposed to giving me a list of the errors.

My structure has been:

public class SelTests {

    public StringBuffer verificationErrors = new StringBuffer();
    public WebDriver driver;

    @BeforeTest
    public void setup() throws Exception {
        // DO SETUP STUFF
    }

    @Test
    public void TestScript1() throws Exception {

    try { //assertEquals("string1", "string2") } catch (Exception e) {verificationErrors.append(e.toString());}
    try { //assertEquals("string1", "string2") } catch (Exception e) {verificationErrors.append(e.toString());}
    try { //assertEquals("string1", "string2") } catch (Exception e) {verificationErrors.append(e.toString());}

    @AfterTest
    public void tearDown() throws Exception{

        driver.close();

        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
            System.out.println(verificationErrorString);    
        }    
    }

But even after modifying this in many ways, I still have the same issue. I also tried surrounding the entire block of assert statements instead of individually doing them as in the code above, but nothing works. I knowingly introduces errors in some assertions but each time I get the log of just 1 error.

Environment:

Selenium 2.30 Server-standalone
TestNG annotations and frameworks
Java code using WebDriver implementations
Eclipse
Windows 7 - 64bit (this should not matter though)

Any help in guiding me to achieve what I am trying to implement will be greatly appreciated.

Thanks,

Alister

Ross Patterson
  • 9,527
  • 33
  • 48
  • Maybe this will give you an idea: http://stackoverflow.com/questions/5550175/junit-test-expected-annotation-not-working – djangofan Feb 27 '13 at 23:05
  • Check if this could come to your rescue. http://www.seleniumtests.com/2011/05/verification-of-application-element.html – HemChe Feb 28 '13 at 04:48

1 Answers1

3

The reason it is not going to the catch block is Asserts throw AssertionErrors. Errors are different from exceptions. When you are catching an exception, you are not catching the AssertionError. Either change the catch block Exception e to AssertionError or to Throwable which would mean any Exception or Error.

niharika_neo
  • 8,441
  • 1
  • 19
  • 31
  • Niharika, this worked for me. I believe my problem was that, for some reason my console was not displaying the complete list of Stack Trace which would show me all the errors. I had tried throwable before and was still not able to see the errors. But after exploring the console deeper I found that it actually printed all the errors but was just not showing me the complete message. Also, to add to this for anyone having similar issues, I tried changing catch block exception to "AssertionError e" and it did not behave the way I wanted it to, or the way that "Throwable e" does. – Alister Ernest Mar 01 '13 at 18:21