Code to click on something if it exists?
Posted: 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:
How can I write that code, so that it clicks on the repo item if it exists?
What I want is:
Not sure if it's needed but the full code of my module is copied below:
Thanks!
Code: Select all
repo.Ranorex_Com_Dom.Home_Page_Heading_Text.Click();
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();
}
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();
}
}
}