Page 1 of 1

Code to click on something if it exists?

Posted: Wed Feb 17, 2016 3:03 pm
by Fergal
I created a simple code module to click on text on the Ranorex.com home page. The module works as it should and contains the line of code below:

Code: Select all

repo.Ranorex_Com_Dom.Home_Page_Heading_Text.Click();
How can I write that code, so that it clicks on the repo item if it exists?

What I want is:

Code: Select all

if (repo.Ranorex_Com_Dom.Home_Page_Heading_Text Exists) {
  repo.Ranorex_Com_Dom.Home_Page_Heading_Text.Click();
}
Not sure if it's needed but the full code of my module is copied below:

Code: Select all

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Threading;
using WinForms = System.Windows.Forms;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;


namespace Code_Project.Code_Modules
{
    /// <summary>
    /// Description of Click_Text_if_Exists_Code_Module.
    /// </summary>
    [TestModule("7C15D77D-4539-4DBD-8A9D-CFF9BB4D8491", ModuleType.UserCode, 1)]
    public class Click_Text_if_Exists_Code_Module : ITestModule
    {
    	// Add repository
    	public static Code_Project.Code_ProjectRepository repo = Code_Project.Code_ProjectRepository.Instance;
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public Click_Text_if_Exists_Code_Module()
        {
            // Do not delete - a parameterless constructor is required!
        }

        /// <summary>
        /// Performs the playback of actions in this module.
        /// </summary>
        /// <remarks>You should not call this method directly, instead pass the module
        /// instance to the <see cref="TestModuleRunner.Run(ITestModule)"/> method
        /// that will in turn invoke this method.</remarks>
        void ITestModule.Run()
        {
            Mouse.DefaultMoveTime = 300;
            Keyboard.DefaultKeyPressTime = 100;
            Delay.SpeedFactor = 1.0;
            
            // Report click on home page heading text
            Report.Log(ReportLevel.Info, "Mouse", "Click home page heading text.", repo.Ranorex_Com_Dom.Home_Page_Heading_TextInfo,
                       new RecordItemIndex(0));
            
            // Click home page heading text
            repo.Ranorex_Com_Dom.Home_Page_Heading_Text.Click();
            }
        }
    }
Thanks!

Re: Code to click on something if it exists?

Posted: Wed Feb 17, 2016 3:13 pm
by krstcs
Pretty much like you have it. :D

Code: Select all

if (myButton.Exists(<timeout>)) {
  myButton.Click();
}
<timeout> overrides the default effective timeout of the element, so the exists function will only search for <timeout>, if supplied.

Checkout the API for more info: http://www.ranorex.com/Documentation/Ranorex/

Check under Ranorex.Core.Repository > RepoItemInfo.Exists()

Re: Code to click on something if it exists?

Posted: Wed Feb 17, 2016 3:18 pm
by odklizec
Hi Fergal,

I would just add to what Kelly said, that 'myButton' should be a 'RepoItemInfo' element!

So if using directly repo item, the code should look like this...

Code: Select all

if (repo.Ranorex_Com_Dom.Home_Page_Heading_TextInfo.Exists(timeout)) 
{
  repo.Ranorex_Com_Dom.Home_Page_Heading_Text.Click();
}

Re: Code to click on something if it exists?

Posted: Wed Feb 17, 2016 3:21 pm
by krstcs
Yeah, sorry! :oops:

Every repo item has an info object that goes with it.

myButton
myButtonInfo

For folders:

myFolder <- contains all the child repo elements
myFolder.Self <- the actual pointer to the UI element this folder references
myFolder.SelfInfo

Re: Code to click on something if it exists?

Posted: Wed Feb 17, 2016 4:20 pm
by Fergal
Thanks very much krstcs and odklizec, that works perfectly.

I couldn't get it to work before, because my repo element wihtout "Info" could not access "Exists". The Exists method was also causing an error, when used in the if(), without the timeout.

Thanks again!