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

Assert and Continue

7 Answers 1495 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Naveen
Top achievements
Rank 1
Naveen asked on 18 Oct 2011, 09:36 PM
If i have a scenario to test whether 2 elements on the page are visible

Assert.IsTrue(Element1.IsVisible());
Assert.IsTrue(Element2.IsVisible());


In my test method if first Assert fails, i do not want to stop the execution, is there a mechanism where in i can continue the execution of second assert statement??

7 Answers, 1 is accepted

Sort by
0
Stoich
Telerik team
answered on 19 Oct 2011, 02:40 PM
Hello Naveen,
   you can simply put the assert statement inside of try catch block and then paste the exception message into the log like so:
try {
Assert.IsTrue(Element1.IsVisible());
} catch (Exception e)    {
Log.WriteLine(e.Message);
}

Assert.IsTrue(Element2.IsVisible());

 
Greetings,
Stoich
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Accepted
Stoich
Telerik team
answered on 19 Oct 2011, 03:26 PM
Hello Naveen,
     one our engineers pointed something out. The approach in my previous post has the drawback that test will appear to have passed even if the assert does fail. If this is a problem for you and you want the test to fail (but not stop) if one or both Asserts fail, you can try a different approach:
if (!Element1.IsVisible())
           {
               Log.WriteLine("Error: Element 1 is not visible");
           }
           if (!Element2.IsVisible())
           {
               Log.WriteLine("Error: Element 2 is not visible");
           }
 
           Assert.IsTrue(Element1.IsVisible() && Element2.IsVisible());


Greetings,
Stoich
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Gaurav
Top achievements
Rank 1
answered on 19 Mar 2014, 07:25 AM
Hi Stoich,

In your above example, if I use logical statement then tell me is there any need to write the Assert method?. Because we have already verify the element in logical statement.
if (!Element2.IsVisible())           
{               
Log.WriteLine("Error: Element 2 is not visible");          
}
here you have already checked, if element is not visible then write the following line on code.
Then what is the need to write Assert method here.




0
Matt
Top achievements
Rank 1
answered on 21 Mar 2014, 12:48 PM
[quote]Naveen said:If i have a scenario to test whether 2 elements on the page are visible

Assert.IsTrue(Element1.IsVisible());
Assert.IsTrue(Element2.IsVisible());


In my test method if first Assert fails, i do not want to stop the execution, is there a mechanism where in i can continue the execution of second assert statement??

[/quote]
Since an Assertion is just a boolean check you can do the following:

bool Element1IsVisible = Element1.IsVisible();
bool Element2IsVisible = Element2.IsVisible();
Assert.IsTrue(Element1IsVisible && Element2IsVisible, String.Format("At least one element was not visible. Element1: {0} Element2: {1}", Element1IsVisible, Element2IsVisible));

What this does is have you perform the boolean checks before hand, and make sure both are visible during the assertion. The String.Format is used to provide the Assertion Error that you expect, and will tell you if both are False or if only one is False. That way you don't have to do a try catch or check the log file for a "passed" unit test.


0
Velin Koychev
Telerik team
answered on 24 Mar 2014, 11:35 AM
@ Gaurav - You can use Assert statement if you want to fail the test, if the condition inside the Assert statement is not met. If you don't want to fail the test, you can use code that is similar to the code sample that you've showed us. 

@Matt - Thank you for your answer and for helping others!

Regards,
Velin Koychev
Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
0
Priyanka
Top achievements
Rank 1
answered on 17 Aug 2018, 03:58 PM

You can use multiple asserts.

Assert.Multiple(() => 

{

Assert.IsTrue(Element1.IsVisible());
Assert.IsTrue(Element2.IsVisible());

});

0
Elena
Telerik team
answered on 20 Aug 2018, 02:42 PM
Hello Priyanka,

Thank you for sharing additional ideas! I hope that will be helpful for others as well! 

Regards,
Elena Tsvetkova
Progress Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
Naveen
Top achievements
Rank 1
Answers by
Stoich
Telerik team
Gaurav
Top achievements
Rank 1
Matt
Top achievements
Rank 1
Velin Koychev
Telerik team
Priyanka
Top achievements
Rank 1
Elena
Telerik team
Share this question
or