This is a migrated thread and some comments may be shown as answers.

How to fail a step

3 Answers 252 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Kristine
Top achievements
Rank 2
Kristine asked on 23 Nov 2010, 05:09 AM
Hi,

I have a coded step that does the ff:

string.compare(textblocka.text, texta)
string.compare(textblockb.text, textb)
....
string.compare(textblockz.text, textz)

I want to fail this particular coded step if any of the string compare fails, but I'd like the test to continue comparing the other texts after the failure. Is this doable? Right now what I have are just logs, e.g. Log.WriteLine(<error message>). So the step is still marked as passed. Also, I can't access these logs in the quick execution, because quick executions do not have logfiles.

Regards,
Kristine

3 Answers, 1 is accepted

Sort by
0
Stoich
Telerik team
answered on 23 Nov 2010, 02:13 PM
Hello Kristine,

       it's really easy. We have a static class Assert which has methods like Assert.IsTrue(boolean something, String message) , Assert.IsNull() etc.
You will be able to access Assert from your coded steps without having to worry about a thing - WebUI Test Studio has already created all the necessary references for you.

Basically, you can call a method from the Assert class to act as a Return statement. However, unlike a Return statement you can have as many Asserts as you like in a single coded step. Assert throws an exception and fails the step if the set condition isn't satisfied and you can also return a message upon failure.
 Here's is a sample solution for your example: 

int a=     String.compare(textblocka.text, texta);
int b=     String.compare(textblockb.text, textb);
int c=     String.compare(textblockz.text, textz);
 
Boolean allMatch = false
if (a == 0 && b == 0 && c == 0) { allMatch = true; }
 
Assert.IsTrue(allMatch,"There was a mismatch"); //Fails the steps if a match isn't true

This code goes in your coded step. If all the texts don't match this step will fail and you'll see the message ("There was a mismatch") in the Log for your Test Run.

 Check out the Assert class - it offers other methods which you will probably find useful.  Assert is already accessable from the code of any coded step as already mentioned. It's exactly what you need.

This info should be enough to get you started - please don't hesitate to contact us again if you need anything else!

Sincerely yours,
Stoich
the Telerik team
See What’s New in WebUI Test Studio’s Q3 Release 24/7 on Telerik TV

0
Arnab
Top achievements
Rank 1
answered on 03 Dec 2014, 10:54 AM
Hi Stoich,
I have a scenario where I am adding multiple Asserts to verify value in multiple fields. My code looks like this:

HtmlInputText lastnamestring = (HtmlInputText) ActiveBrowser.Find.ById<HtmlInputText>("lastName");
Assert.AreEqual<string>(lastnamestring.Value,"JOHNSON");

HtmlInputText firstnamestring = (HtmlInputText) ActiveBrowser.Find.ById<HtmlInputText>("firstName");
Assert.AreEqual<string>(firstnamestring.Value,"SMITH");

I do not want the test to stop if there is a failure and wanted to keep verifying the values in all the fields (something like "continue on failure flag" present in record n play back). At the end of run I wanted to know which assert resulted in failure and which has passed successfully by seeing the logs. Could you please help me with a sample code to do that.                                                                    
Thanks,                                                                                                                                                                                                        Arnab
                                                                                                  








0
Ivaylo
Telerik team
answered on 08 Dec 2014, 09:30 AM
Hеllo Arnab,

Please note that you are updating a quite old forum post form 2010, in such cases please submit a new thread instead.

On your problem, you can use try-catch block in order to catch exceptions from your assert statements.

Sample code follows:

try{
Assert.IsTrue(Element1.IsVisible());
} catch(Exception e)    {
Log.WriteLine(e.Message);
}

Regards,
Ivaylo
Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
Kristine
Top achievements
Rank 2
Answers by
Stoich
Telerik team
Arnab
Top achievements
Rank 1
Ivaylo
Telerik team
Share this question
or