Using "Instance" and TheItem"Info" or not: What is better concerning coding?

Ask general questions here.
User avatar
_KK_
Posts: 36
Joined: Tue Nov 21, 2017 12:51 pm

Using "Instance" and TheItem"Info" or not: What is better concerning coding?

Post by _KK_ » Wed Sep 04, 2019 5:12 am

The first coding snippet uses an entry in the repository with Keyword "Instance" and the "Info" entry of that thing:

Code: Select all

internal void CheckSomethingImportantExists()
        {
        	if(Ranorex_Automation_Helpers.Ranorex_Automation_HelpersRepository.Instance.MySpecialStructure.CloseButtonInfo.Exists(Duration.ToMilliseconds(10000)))
        	{
        		Ranorex_Automation_Helpers.Ranorex_Automation_HelpersRepository.Instance.MySpecialStructure.CloseButton.Click();
        	}
        }

The second coding snippet checks something other with importance but in a different way:

Code: Select all

public void CloseXPanelIfUnexpectedlyOpen()
        {
	
            	if (Validate.NotExists("/form[@title='foo' and @processname='UIprocess']//button[@automationid='CloseMessageDialog']",25002,"Check if msg panel is unexpectedly open ...",false))
            	{
            		//As expected.
            		Ranorex.Report.Info ("Ok. No x panel was unexpectedly open. Nothing to do. ");
            	}
            		else 
            		{repoName.MyElementInRepositoy.MyNameOfButton.Click();
            			Ranorex.Report.Info ("X panel was unexpectedly open! Have closed this!");
            		}// end else
        
        }
Which way is best?

Thanks for any feed back.

With best regards

_KK_

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

Re: Using "Instance" and TheItem"Info" or not: What is better concerning coding?

Post by odklizec » Wed Sep 04, 2019 6:48 am

Hi,

In my opinion, the best approach is to use combination of RepoItemInfo and Adapter method parameters. I'm using this approach in my Recording user codes:

Code: Select all

public static void DoSomethingWithRepoElements(RepoItemInfo repoElement)
{
    if (repoElement.Exists()) // you can eventually apply custom timeout in Exists() method
    {
        repoElement.CreateAdapter<Ranorex.Unknown>(false).Click();
    }
} 
Eventually, in code modules, I'm using this approach:

Code: Select all

//instantiate repository
private static SolutionName.SolutionNameRepository repo = SolutionName.SolutionNameRepository.Instance;
...
public static void DoSomethingWithRepoElements()
{
    if (repo.Element.NameInfo.Exists())
    {
        repo.Element.Name.Click();
    }
}
Both approaches first check the existence/availability of certain repo element (without the risk of 'element does not exists' exception) and only if exists, create or use element's adapter. But to be quite honest, I'm preferring first approach with Recording modules, instead of referencing repo elements directly in code. I just consider assigning repo elements with method parameters (in recording modules) a much cleaner and natural solution ;) 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

User avatar
_KK_
Posts: 36
Joined: Tue Nov 21, 2017 12:51 pm

Re: Using "Instance" and TheItem"Info" or not: What is better concerning coding?

Post by _KK_ » Fri Sep 06, 2019 10:12 am

Thanks.