Code to click on something if it exists?

Class library usage, coding and language questions.
Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Code to click on something if it exists?

Post by Fergal » Wed Feb 17, 2016 3:03 pm

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!

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Code to click on something if it exists?

Post by krstcs » Wed Feb 17, 2016 3:13 pm

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()
Shortcuts usually aren't...

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

Re: Code to click on something if it exists?

Post by odklizec » Wed Feb 17, 2016 3:18 pm

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();
}
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

krstcs
Ranorex Guru
Posts: 2683
Joined: Tue Feb 07, 2012 4:14 pm
Location: Austin, Texas, USA

Re: Code to click on something if it exists?

Post by krstcs » Wed Feb 17, 2016 3:21 pm

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
Shortcuts usually aren't...

Fergal
Certified Professional
Certified Professional
Posts: 298
Joined: Tue Feb 18, 2014 2:14 pm
Location: Co Louth, Ireland
Contact:

Re: Code to click on something if it exists?

Post by Fergal » Wed Feb 17, 2016 4:20 pm

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!