The else block is not getting executed

Ask general questions here.
premravi
Posts: 128
Joined: Tue Jan 08, 2019 1:55 pm

The else block is not getting executed

Post by premravi » Wed Jul 24, 2019 1:26 pm

Hi,

In the below piece of code the else block is not getting executed

Only if the "IF" condition satisfies it will work perfectly fine

If the "IF" condition does not satisfy, it is directly going in to the catch block and failing the test case

Code: Select all

public bool smallSamplePopUp()
		{
			try
			{
				if(repo.Fusion.SaveSmallSampleClickOK.Visible)
				{
					repo.Fusion.SaveSmallSampleClickOK.Click("18;12");
					Reports.Success("Click on OK Button to run less records file", "Successfully run the report");
				}
				else
				{
					
				}
				return true;
			}
			
			catch
			{
				return false;
			}
		}
The requirement is if the "IF" condition the execute it otherwise do nothing move to the next step of the test case

That's why I did not kept anything in the else block

Please help me to understand the issue

Regards

manish
Certified Professional
Certified Professional
Posts: 53
Joined: Fri Aug 10, 2018 12:46 pm

Re: The else block is not getting executed

Post by manish » Wed Jul 24, 2019 1:37 pm

Hi,

It could happen that the if condition is evaluated before the repo item SaveSmallSampleClickOK becomes available or starts to Exist.
You could wait for the element to become visible and evaluate the Exists() condition such as this:

Code: Select all


public bool smallSamplePopUp()
		{
			try
			{
				if(repo.Fusion.SaveSmallSampleClickOKInfo.Exists(10000) && repo.Fusion.SaveSmallSampleClickOK.Visible)
				{
					repo.Fusion.SaveSmallSampleClickOK.Click("18;12");
					Reports.Success("Click on OK Button to run less records file", "Successfully run the report");
				}
				else
				{

				}
				return true;
			}

			catch
			{
				return false;
			}
		}
		

If the issue still persists, please upload a snapshot of the element so that we can have a better look.