Help: User Code to execute in case test execution fails

Best practices, code snippets for common functionality, examples, and guidelines.
bpdyna
Posts: 13
Joined: Wed Mar 30, 2016 2:11 pm

Help: User Code to execute in case test execution fails

Post by bpdyna » Wed May 04, 2016 10:43 am

Hello,

I have tried searching the forum but have been unable to find a solution to my problem, so I'm making a new post for this.

My situation:
I am testing a website using different stubs, each stub has it's own login credentials. Each testcase uses it's own stub.
My issue:
When testcase 1 fails somewhere along the road, it will stop executing this testcase and continue with the next testcase. However, I am still logged in and the test automation is trying to execute his first action: logging in.

I could ofcourse build in a check in the Login method to see if I'm still logged in and if that is true, then log me out .

However, I'm wondering if I can't build something inside each UserCode module that will only execute when an action in a Recording Module fails. So in the above situation, it will execute some code to log me out. Preferably this would be a single method which can be inherited from my Library.

Is such a thing possible? Any help would be appreciated.

stapes
Posts: 206
Joined: Wed Sep 16, 2015 10:55 am

Re: Help: User Code to execute in case test execution fails

Post by stapes » Wed May 04, 2016 12:09 pm

I created a Folder under my Test Suite folder called /Common and put common code in Classes in there.

This includes methods for logging out when a test fails, so that each test can begin again from the start.

I found it best to use many smaller modules, as not every logout was the same.

Example:

Code: Select all

public static void ValidateAndLogout(Test_365AgilePortalRepository repo)
{
Report.Log(ReportLevel.Info, "Validation", "Validating AttributeEqual (Text='Logout') on item 'IOS365Agile.Com365agileEnterprise365AgileTEST.UIView.Logout'.", repo.IOS365Agile.Com365agileEnterprise365AgileTEST.UIView.Logout_ButtonInfo);

Validate.Attribute(repo.IOS365Agile.Com365agileEnterprise365AgileTEST.UIView.Logout_ButtonInfo, "Text", "Logout");

Report.Log(ReportLevel.Info, "Touch", "Touch item 'IOS365Agile.Com365agileEnterprise365AgileTEST.UIView.Logout' at Center", repo.IOS365Agile.Com365agileEnterprise365AgileTEST.UIView.Logout_ButtonInfo);

repo.IOS365Agile.Com365agileEnterprise365AgileTEST.UIView.Logout_Button.Touch();
}
These processes can then be called in any sequence required, for example

Code: Select all

Report.Failure  ("Error","Expected fields do not exist.");            	
Screenshot_And_Snapshot ();           	
Common.IOS_Common_Functions.CloseAfterErrorMethod(repo);
Common.IOS_Common_Functions.Touch_Home(repo);				
Common.IOS_Common_Functions.ValidateAndLogout(repo);
Common.IOS_Common_Functions.ConfirmLogout(repo);
Common.IOS_Common_Functions.Touch_Standard(repo);

bpdyna
Posts: 13
Joined: Wed Mar 30, 2016 2:11 pm

Re: Help: User Code to execute in case test execution fails

Post by bpdyna » Wed May 04, 2016 1:32 pm

Hi stapes,

Thank you for your input, however I am still a little lost.
Looking at the snippets you posted, your ValidateAndLogout seems to be the method you use to logout. Or at least, it is one of the ways you use to logout because you mention you use many smaller modules as not every logout is the same for you.
Your logout method contains a report line, a validation, another report line and an action (touch logout button).

So far so good, because I use a similar approach where I have a Library folder with different .cs files and I inherit these within my actual recording modules.
However, what I'm looking for is the following:

step 1 (login): success
step 2 (click): success
step 3 (validate): fails
step 4 and further will not be executed for this testcase because of the fail. Error behavior is set to Sibling, so the testsuite will start the next testcase on step 1 which is login again.

How can I make it so that this piece of usercode to logout only executes when one of the steps fails? And ofcourse when all steps are successfully executed, it should not run?

From your 2nd snippet it looks like you execute a report line for failure and you execute every logout method.

stapes
Posts: 206
Joined: Wed Sep 16, 2015 10:55 am

Re: Help: User Code to execute in case test execution fails

Post by stapes » Wed May 04, 2016 2:23 pm

I have been running my tests for some time & have been able to code my way out of several types of failure.

The chunk of code shown above is executed if this statement fails:

Code: Select all

if(RepoItemInfo .WaitForControl (
            	repo.IOS365Agile.MyIpadApp.AddressLine1Info,
            	20000,
            	"Address Line 1 found.",
            	true ,
            	"Address Line 1 not found."))
        	{
WaitForControl is another Function in my common library:

Code: Select all

/// <summary>
		/// this function should wait for control to appear - returns true or false depending on success
		/// </summary>
		/// <param name="repoItemInfo">the control being waited for</param>
		/// <param name="timeoutInMs">how long in MicroSeconds to wait</param>
		/// <param name="successMsg">message to display if successful</param>
		/// <param name="failOnFail">do we want to fail the test if control not found</param>
		/// <param name="failMsg">failure message</param>
		/// <returns></returns>
		public static bool WaitForControl(
			Ranorex .Core .Repository .RepoItemInfo repoItemInfo, 
			int timeoutInMs, 
			string successMsg, 
			bool failOnFail, 
			string failMsg)
		{
			Duration effectiveTimeout = new Duration (timeoutInMs); //Timeout in ms
        	
        	Stopwatch stopwatch=new Stopwatch ();
			stopwatch .Reset ();
			stopwatch .Start ();
        	
        	bool success=repoItemInfo.Exists (effectiveTimeout);
        	
        	stopwatch .Stop ();
        	if(success )
        	{
        		Report.Success  ( successMsg + " Time taken = " + stopwatch .ElapsedMilliseconds + " ms.");
        		return true ;
        	}
        	else
        	{
        		if(failOnFail)
        		{
        			Report.Failure  (failMsg  + " Time taken = " + stopwatch .ElapsedMilliseconds + " ms.");
        		}
        		else
        		{
        			Report.Info ( failMsg + " Time taken = " + stopwatch .ElapsedMilliseconds + " ms.");
        		}
        		return false ;
        	}
		}
After detecting a failure, this automation will back out of the current screen, return to the Home page, logout. A dialog asks if the user is sure they want to log out. This is confirmed by clicking Yes.
There is one last thing to do to return the system to the start button, & that is to press the Standard button. The system always begins by pressing the Advanced button.

Does this help?

bpdyna
Posts: 13
Joined: Wed Mar 30, 2016 2:11 pm

Re: Help: User Code to execute in case test execution fails

Post by bpdyna » Mon May 09, 2016 8:20 am

Hi stapes,

That looks like a good solution and exactly what I'm looking for. I will try it out later today.
Thank you so far :)