How to design nested if-else statement in C# Ranorex

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

How to design nested if-else statement in C# Ranorex

Post by premravi » Fri Sep 27, 2019 2:33 pm

Hi,

Basically the requirement is "If either of the two conditions are satisfied than pass it" else fail it

I tried operator like || , ^ nothing worked

if(condition 1 || condition 2)

I tried

if (condition 1 ^ condition 2)

I tried

if(condition1)
{
}
else if (condition 2)
{
}
Else
{
}
Did Not worked

Please let me know what logic can we right to handle such scenario

Please let me know for more details

Regards

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 5256
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to design nested if-else statement in C# Ranorex

Post by odklizec » Fri Sep 27, 2019 2:37 pm

Hi,

Try this...
if (condition1| condition2)
If it does not help, please post exact conditions you are using.
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

premravi
Posts: 95
Joined: Tue Jan 08, 2019 1:55 pm

Re: How to design nested if-else statement in C# Ranorex

Post by premravi » Fri Sep 27, 2019 4:04 pm

This is the piece of code which I am trying to execute
It is successfully executing if block
But in case if "IF" block does not executed, rather then going to else if block it is failing

#region validateDifferenceInReports()

public void validateDifferenceInReports()
{

try
{
if(excelrepo.NewTableCompareTableCompareBeyo.Container1DifferenceRowS.Visible)

{
HtmlReports.SuccessReport("To verify that the difference in the report is not more than 2", "Successfully verified the difference in the report is 2 which is acceptable");
}

else if(excelrepo.NewTableCompareTableCompareBeyo.Container2DifferenceRowS.Visible)

{
HtmlReports.SuccessReport("To verify that the difference in the report is not more than 1", "Successfully verified the difference in the report is 1 which is acceptable");
}

else
{
HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2");
}

}
catch(Exception ex)
{
HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2" + ex.Message);
throw;
}
}
#endregion

User avatar
odklizec
Ranorex Guru
Ranorex Guru
Posts: 5256
Joined: Mon Aug 13, 2012 9:54 am
Location: Zilina, Slovakia

Re: How to design nested if-else statement in C# Ranorex

Post by odklizec » Mon Sep 30, 2019 7:54 am

Hi,

Basically, after some code re-formating, your code looks like this:

Code: Select all

#region validateDifferenceInReports()
public void validateDifferenceInReports()
{
    try
    {
        if(excelrepo.NewTableCompareTableCompareBeyo.Container1DifferenceRowS.Visible)
        {
            HtmlReports.SuccessReport("To verify that the difference in the report is not more than 2", "Successfully verified the difference in the report is 2 which is acceptable");
        }
        else if(excelrepo.NewTableCompareTableCompareBeyo.Container2DifferenceRowS.Visible)
        {
            HtmlReports.SuccessReport("To verify that the difference in the report is not more than 1", "Successfully verified the difference in the report is 1 which is acceptable");
        }
        else
        {
            HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2");
        }
    }
    catch(Exception ex)
    {
        HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2" + ex.Message);
        throw;
    }
}	
#endregion
But in my opinion, you should test 'visible' attribute, only after knowing the given element exists! So better code would be like this:

Code: Select all

#region validateDifferenceInReports()
public void validateDifferenceInReports()
{
    try
    {
        if(excelrepo.NewTableCompareTableCompareBeyo.Container1DifferenceRowSInfo.Exists())
        {
            if (excelrepo.NewTableCompareTableCompareBeyo.Container1DifferenceRowS.Visible)
	    {
                HtmlReports.SuccessReport("To verify that the difference in the report is not more than 2", "Successfully verified the difference in the report is 2 which is acceptable");
            }
        }
        else if(excelrepo.NewTableCompareTableCompareBeyo.Container2DifferenceRowSInfo.Exists())
        {
            if (excelrepo.NewTableCompareTableCompareBeyo.Container2DifferenceRowS.Visible)
            {
                HtmlReports.SuccessReport("To verify that the difference in the report is not more than 2", "Successfully verified the difference in the report is 2 which is acceptable");
            }
            else
            {	
                HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2");
            }
        }
        else
        {	
            HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2");
        }
    }	
    catch(Exception ex)
    {
        HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2" + ex.Message);
        throw;
    }
}	
#endregion
If you directly test 'Visible' attribute and the element does not exists (yet), the code may actually immediately fail (after reaching repo element's Effective Timeout) on 'element does not exists' exception and so it may never reach second 'else if' statement. So you should always test the element's existence, before testing its 'Visibility' (or whatever element's attribute or action).

Additionally, I would suggest to avoid referencing repo elements directly in code and rather use them as method parameters, like this:

Code: Select all

#region validateDifferenceInReports()
public void validateDifferenceInReports(RepoItemInfo container1, RepoItemInfo container2)
{
    try
    {
        if(container1.Exists())
        {
            if (container1.CreateAdapter<Ranorex.Unknown>(false).Visible)
            {
                HtmlReports.SuccessReport("To verify that the difference in the report is not more than 2", "Successfully verified the difference in the report is 2 which is acceptable");
            }
        }
        else if(container2.Exists())
        {
            if (container2.CreateAdapter<Ranorex.Unknown>(false).Visible)
            {
                HtmlReports.SuccessReport("To verify that the difference in the report is not more than 2", "Successfully verified the difference in the report is 2 which is acceptable");
            }
           else
            {	
                HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2");
            }
        }
        else
        {	
            HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2");
        }
    }	
    catch(Exception ex)
    {
        HtmlReports.FailureReport("To verify that the difference in the report is not more than 2","The difference in the reports is more than 2" + ex.Message);
        throw;
    }
}	
#endregion
Hope this helps?
Pavel Kudrys
Ranorex explorer at Descartes Systems

Please add these details to your questions:
  • Ranorex Snapshot. Learn how to create one >here<
  • Ranorex xPath of problematic element(s)
  • Ranorex version
  • OS version
  • HW configuration

premravi
Posts: 95
Joined: Tue Jan 08, 2019 1:55 pm

Re: How to design nested if-else statement in C# Ranorex

Post by premravi » Fri Oct 04, 2019 1:01 pm

Hi,

Thanks a lot for this information

Its really helpful

Regards