Question about Fail the Test

361 views
Skip to first unread message

Moises Siles

unread,
Jul 19, 2011, 2:48:07 PM7/19/11
to webd...@googlegroups.com, seleniu...@googlegroups.com
Hi,

Guys one question, I'm running my scripts using Gallio Icarus with NUnit, webdriver and C#, but for example in the code that I put below, if the titles doesn't match it should mark the test as Failed, but so far it always show the results as Passed even if the Title is wrong

            try
            {
                Assert.AreEqual(webDriver.Title, Title);
            }
            catch (Exception e)
            {
              log.write(e.Message);
            }

Should I need to add something in the Catch to indicates that the test is failing.

Regards

Luke Inman-Semerau

unread,
Jul 19, 2011, 4:34:31 PM7/19/11
to webd...@googlegroups.com, seleniu...@googlegroups.com
That's because you're catching the exception and essentially suppressing it... if you want it to fail just remove the try / catch.

--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To post to this group, send email to webd...@googlegroups.com.
To unsubscribe from this group, send email to webdriver+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webdriver?hl=en.

Moises Siles

unread,
Jul 19, 2011, 4:43:25 PM7/19/11
to seleniu...@googlegroups.com
mmm, that was what I thought, so I can't catch that error in the log file..... 

You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.

Luke Inman-Semerau

unread,
Jul 19, 2011, 5:00:42 PM7/19/11
to seleniu...@googlegroups.com
You can add a throw e (sorry don't know c# syntax... but sure it's close) after your log then.

Moises Siles

unread,
Jul 19, 2011, 5:04:50 PM7/19/11
to seleniu...@googlegroups.com
yep, I was doing something like this

            try
            {
                Assert.AreEqual(webDriver.Title, Title);
            }
            catch (Exception e)
            {
              log.write(e.Message);
              throw e;
            }

But I want to make sure if there is a best way to achieve this.

Luke Inman-Semerau

unread,
Jul 19, 2011, 5:13:10 PM7/19/11
to seleniu...@googlegroups.com
I actually like to use a fluent-assertions library (i'm in java and use fest) which usually allow you to add a description. I'm not sure why you are logging, but you probably have some reason? Usually the test runner reports back the failed test and tracebacks should be apart of it if an exception was thrown..

Moises Siles

unread,
Jul 19, 2011, 6:02:57 PM7/19/11
to seleniu...@googlegroups.com
yep Luke, I think you are right, the test runner should catch all of those things. I just want to catch the assert error in my log file.

something like 

INFO 2011-07-19 02:56:31 – Verify Page Title - My Title here
ERROR2011-07-19 02:56:32 –   Expected string length 48 but was 49. Strings differ at index 1.
  Expected: "My Title here 2"
  But was:  "My Title here"

Krishnan Mahadevan

unread,
Jul 19, 2011, 10:53:55 PM7/19/11
to seleniu...@googlegroups.com
Moises

Atleast with TestNG provided asserts, I do know that the asserts do provide a mechanism to provide an optional "Descriptive message" to be added when an Assert Fails.

The following could work for you (Am doing this in Java, you can convert it into C#)

try{
    Assert.assertEquals(actual, expected);
} catch(AssertionError e){
    log(mymessage);
    throw new AssertionError(e);
}

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"

Michael Zhang

unread,
Jul 19, 2011, 10:57:51 PM7/19/11
to seleniu...@googlegroups.com
Are you trying to manage your own log? Or you want to customise the standard log?
If the first one, you can just do anything you want in "catch" and rethrow it.
If the second, you can modify the exception description, it will log into the test frameworks log.
Hope that's what you need.

Moises Siles

unread,
Jul 20, 2011, 9:34:59 AM7/20/11
to seleniu...@googlegroups.com
Thanks for the answer guys,

Moises Siles

unread,
Jul 20, 2011, 10:23:53 AM7/20/11
to seleniu...@googlegroups.com
So I'm planning to catch all the assertions in a stringbuilder and then I can check the StringBuilder variable in the Teardown, I saw this approach when I export an example from IDE to c#, in order to see if the string var is empty or it has errors. Please let me know if that could work correctly or it is not a good practice

        [TearDown]
        public static void Teardown()
        {
            try
            {
                webDriver.Quit();                
            }
            catch (Exception e)
            {
                Log.writeLog(e.Message);
            }
            Assert.AreEqual("", verificationErrors.ToString());
        }


        public static void VerifyElementText(string ElementText, string TextValue)
        {
            try
            {
                Assert.AreEqual(ElementText, TextValue);
            }
            catch (Exception e)
            {
                Log.writeLog(e.Message);
                verificationErrors.Append(e.Message);
            }
        }

Luke Inman-Semerau

unread,
Jul 20, 2011, 10:30:49 AM7/20/11
to seleniu...@googlegroups.com
That's basically one way for your test runner to not stop on the first error, there's other ways to accomplish this without having to catch every possible exception your test may throw

-Luke

Moises Siles

unread,
Jul 20, 2011, 11:41:47 AM7/20/11
to seleniu...@googlegroups.com
Cool, I just want to make sure, I think this works for me..

Thanks for all your help guys

Sasi kumar

unread,
Jul 22, 2011, 5:56:06 AM7/22/11
to Selenium Users
Is anyone using some other way ?

On Jul 20, 8:41 pm, Moises Siles <moises.si...@gmail.com> wrote:
> Cool, I just want to make sure, I think this works for me..
>
> Thanks for all your help guys
>
> On Wed, Jul 20, 2011 at 8:30 AM, Luke Inman-Semerau
> <luke.seme...@gmail.com>wrote:
>
>
>
>
>
>
>
> > That's basically one way for your test runner to not stop on the first
> > error, there's other ways to accomplish this without having to catch every
> > possible exception your test may throw
>
> > -Luke
>
> > On Jul 20, 2011, at 7:23 AM, Moises Siles <moises.si...@gmail.com> wrote:
>
> > So I'm planning to catch all the assertions in a stringbuilder and then I
> > can check the StringBuilder variable in the Teardown, I saw this approach
> > when I export an example from IDE to c#, in order to see if the string var
> > is empty or it has errors. Please let me know if that could work correctly
> > or it is not a good practice
>
> >         [TearDown]
> >         public static void Teardown()
> >         {
> >             try
> >             {
> >                 webDriver.Quit();
> >             }
> >             catch (Exception e)
> >             {
> >                 Log.writeLog(e.Message);
> >             }
> >             Assert.AreEqual("", verificationErrors.ToString());
> >         }
>
> >         public static void VerifyElementText(string ElementText, string
> > TextValue)
> >         {
> >             try
> >             {
> >                 Assert.AreEqual(ElementText, TextValue);
> >             }
> >             catch (Exception e)
> >             {
> >                 Log.writeLog(e.Message);
> >                 verificationErrors.Append(e.Message);
> >             }
> >         }
>
> > On Wed, Jul 20, 2011 at 7:34 AM, Moises Siles < <moises.si...@gmail.com>
> > moises.si...@gmail.com> wrote:
>
> >> Thanks for the answer guys,
>
> >> On Tue, Jul 19, 2011 at 8:53 PM, Krishnan Mahadevan <<krishnan.mahadevan1...@gmail.com>
> >> krishnan.mahadevan1...@gmail.com> wrote:
>
> >>> Moises
>
> >>> Atleast with TestNG provided asserts, I do know that the asserts do
> >>> provide a mechanism to provide an optional "Descriptive message" to be added
> >>> when an Assert Fails.
>
> >>> The following could work for you (Am doing this in Java, you can convert
> >>> it into C#)
>
> >>> try{
> >>>     Assert.assertEquals(actual, expected);
> >>> } catch(AssertionError e){
> >>>     log(mymessage);
> >>>     throw new AssertionError(e);
> >>> }
>
> >>> Thanks & Regards
> >>> Krishnan Mahadevan
>
> >>> "All the desirable things in life are either illegal, expensive,
> >>> fattening or in love with someone else!"
>
> >>> On Wed, Jul 20, 2011 at 3:32 AM, Moises Siles < <moises.si...@gmail.com>
> >>> moises.si...@gmail.com> wrote:
>
> >>>> yep Luke, I think you are right, the test runner should catch all of
> >>>> those things. I just want to catch the assert error in my log file.
>
> >>>> something like
>
> >>>> INFO 2011-07-19 02:56:31 – Verify Page Title - My Title here
> >>>> ERROR2011-07-19 02:56:32 –   Expected string length 48 but was 49.
> >>>> Strings differ at index 1.
> >>>>   Expected: "My Title here 2"
> >>>>   But was:  "My Title here"
>
> >>>> On Tue, Jul 19, 2011 at 3:13 PM, Luke Inman-Semerau <<luke.seme...@gmail.com>
> >>>> luke.seme...@gmail.com> wrote:
>
> >>>>> I actually like to use a fluent-assertions library (i'm in java and use
> >>>>> fest) which usually allow you to add a description. I'm not sure why you are
> >>>>> logging, but you probably have some reason? Usually the test runner reports
> >>>>> back the failed test and tracebacks should be apart of it if an exception
> >>>>> was thrown..
>
> >>>>> On Tue, Jul 19, 2011 at 2:04 PM, Moises Siles <<moises.si...@gmail.com>
> >>>>> moises.si...@gmail.com> wrote:
>
> >>>>>> yep, I was doing something like this
>
> >>>>>>             try
> >>>>>>             {
> >>>>>>                 Assert.AreEqual(webDriver.Title, Title);
> >>>>>>             }
> >>>>>>             catch (Exception e)
> >>>>>>             {
> >>>>>>               log.write(e.Message);
> >>>>>>               throw e;
> >>>>>>             }
>
> >>>>>> But I want to make sure if there is a best way to achieve this.
>
> >>>>>> On Tue, Jul 19, 2011 at 3:00 PM, Luke Inman-Semerau <<luke.seme...@gmail.com>
> >>>>>> luke.seme...@gmail.com> wrote:
>
> >>>>>>> You can add a throw e (sorry don't know c# syntax... but sure it's
> >>>>>>> close) after your log then.
>
> >>>>>>> On Tue, Jul 19, 2011 at 1:43 PM, Moises Siles <<moises.si...@gmail.com>
> >>>>>>> moises.si...@gmail.com> wrote:
>
> >>>>>>>> mmm, that was what I thought, so I can't catch that error in the log
> >>>>>>>> file.....
>
> >>>>>>>> On Tue, Jul 19, 2011 at 2:34 PM, Luke Inman-Semerau <<luke.seme...@gmail.com>
> >>>>>>>> luke.seme...@gmail.com> wrote:
>
> >>>>>>>>> That's because you're catching the exception and essentially
> >>>>>>>>> suppressing it... if you want it to fail just remove the try / catch.
>
> >>>>>>>>> On Tue, Jul 19, 2011 at 11:48 AM, Moises Siles <<moises.si...@gmail.com>
> >>>>>>>>> moises.si...@gmail.com> wrote:
>
> >>>>>>>>>> Hi,
>
> >>>>>>>>>> Guys one question, I'm running my scripts using Gallio Icarus with
> >>>>>>>>>> NUnit, webdriver and C#, but for example in the code that I put below, if
> >>>>>>>>>> the titles doesn't match it should mark the test as Failed, but so far it
> >>>>>>>>>> always show the results as Passed even if the Title is wrong
>
> >>>>>>>>>>             try
> >>>>>>>>>>             {
> >>>>>>>>>>                 Assert.AreEqual(webDriver.Title, Title);
> >>>>>>>>>>             }
> >>>>>>>>>>             catch (Exception e)
> >>>>>>>>>>             {
> >>>>>>>>>>               log.write(e.Message);
> >>>>>>>>>>             }
>
> >>>>>>>>>> Should I need to add something in the Catch to indicates that the
> >>>>>>>>>> test is failing.
>
> >>>>>>>>>> Regards
>
> >>>>>>>>>> --
> >>>>>>>>>> You received this message because you are subscribed to the Google
> >>>>>>>>>> Groups "webdriver" group.
> >>>>>>>>>> To post to this group, send email to <webd...@googlegroups.com>
> >>>>>>>>>> webd...@googlegroups.com.
> >>>>>>>>>> To unsubscribe from this group, send email to
> >>>>>>>>>> <webdriver%2Bunsu...@googlegroups.com>
> >>>>>>>>>> webdriver+...@googlegroups.com.
> >>>>>>>>>> For more options, visit this group at
> >>>>>>>>>> <http://groups.google.com/group/webdriver?hl=en>
> >>>>>>>>>>http://groups.google.com/group/webdriver?hl=en.
>
> >>>>>>>>>  --
> >>>>>>>>> You received this message because you are subscribed to the Google
> >>>>>>>>> Groups "Selenium Users" group.
> >>>>>>>>> To post to this group, send email to
> >>>>>>>>> <seleniu...@googlegroups.com>seleniu...@googlegroups.com.
> >>>>>>>>> To unsubscribe from this group, send email to
> >>>>>>>>> <selenium-users%2Bunsu...@googlegroups.com>
> >>>>>>>>> selenium-user...@googlegroups.com.
> >>>>>>>>> For more options, visit this group at
> >>>>>>>>> <http://groups.google.com/group/selenium-users?hl=en>
> >>>>>>>>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>>>>>>>  --
> >>>>>>>> You received this message because you are subscribed to the Google
> >>>>>>>> Groups "Selenium Users" group.
> >>>>>>>> To post to this group, send email to
> >>>>>>>> <seleniu...@googlegroups.com>seleniu...@googlegroups.com.
> >>>>>>>> To unsubscribe from this group, send email to
> >>>>>>>> <selenium-users%2Bunsu...@googlegroups.com>
> >>>>>>>> selenium-user...@googlegroups.com.
> >>>>>>>> For more options, visit this group at
> >>>>>>>> <http://groups.google.com/group/selenium-users?hl=en>
> >>>>>>>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>>>>>>  --
> >>>>>>> You received this message because you are subscribed to the Google
> >>>>>>> Groups "Selenium Users" group.
> >>>>>>> To post to this group, send email to
> >>>>>>> <seleniu...@googlegroups.com>seleniu...@googlegroups.com.
> >>>>>>> To unsubscribe from this group, send email to
> >>>>>>> <selenium-users%2Bunsu...@googlegroups.com>
> >>>>>>> selenium-user...@googlegroups.com.
> >>>>>>> For more options, visit this group at
> >>>>>>> <http://groups.google.com/group/selenium-users?hl=en>
> >>>>>>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>>>>>  --
> >>>>>> You received this message because you are subscribed to the Google
> >>>>>> Groups "Selenium Users" group.
> >>>>>> To post to this group, send email to
> >>>>>> <seleniu...@googlegroups.com>seleniu...@googlegroups.com.
> >>>>>> To unsubscribe from this group, send email to
> >>>>>> <selenium-users%2Bunsu...@googlegroups.com>
> >>>>>> selenium-user...@googlegroups.com.
> >>>>>> For more options, visit this group at
> >>>>>> <http://groups.google.com/group/selenium-users?hl=en>
> >>>>>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>>>>  --
> >>>>> You received this message because you are subscribed to the Google
> >>>>> Groups "Selenium Users" group.
> >>>>> To post to this group, send email to <seleniu...@googlegroups.com>
> >>>>> seleniu...@googlegroups.com.
> >>>>> To unsubscribe from this group, send email to
> >>>>> <selenium-users%2Bunsu...@googlegroups.com>
> >>>>> selenium-user...@googlegroups.com.
> >>>>> For more options, visit this group at
> >>>>> <http://groups.google.com/group/selenium-users?hl=en>
> >>>>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>>>  --
> >>>> You received this message because you are subscribed to the Google
> >>>> Groups "Selenium Users" group.
> >>>> To post to this group, send email to <seleniu...@googlegroups.com>
> >>>> seleniu...@googlegroups.com.
> >>>> To unsubscribe from this group, send email to
> >>>> <selenium-users%2Bunsu...@googlegroups.com>
> >>>> selenium-user...@googlegroups.com.
> >>>> For more options, visit this group at
> >>>> <http://groups.google.com/group/selenium-users?hl=en>
> >>>>http://groups.google.com/group/selenium-users?hl=en.
>
> >>>  --
> >>> You received this message because you are subscribed to the Google Groups
> >>> "Selenium Users" group.
> >>> To post to this group, send
>
> ...
>
> read more »
Reply all
Reply to author
Forward
0 new messages