Binding variables in UserCode

Ask general questions here.
Eamonn
Posts: 63
Joined: Wed Jul 16, 2014 9:30 am
Location: Dundalk, Ireland.

Binding variables in UserCode

Post by Eamonn » Tue Jan 29, 2019 1:57 pm

I converted a action item in a recorded module to a UserCode action. In the UserCode.cs I added code, one line was a call to use an existing Recorded Module which contains variables. How do I bind to these variables in this Recorded Module to the data table I'm using with the parent Test Case/Test Suite?

I am calling module using Recordings.AddConsultantWebsiteSubscription.Start();


Here is the UserCode.ie:

///////////////////////////////////////////////////////////////////////////////
//
// This file was automatically generated by RANOREX.
// Your custom recording code should go in this file.
// The designer will only add methods to this file, so your custom code won't be overwritten.
// http://www.ranorex.com
//
///////////////////////////////////////////////////////////////////////////////

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.Repository;
using Ranorex.Core.Testing;

using TieredRegression.Recordings;

namespace TieredRegression.Recordings
{
public partial class ConfirmConsultantHasValidWebsiteSubscription
{
/// <summary>
/// This method gets called right after the recording has been started.
/// It can be used to execute recording specific initialization code.
/// </summary>
private void Init()
{
// Your recording specific initialization code goes here.
}

/// <summary>
/// Method validates the Manage button exists, if it is not, then check if Subscribe button exists.
/// If Subscribe boon existes then call module to add subscription and allow TC continue.
/// If Manage nor Subscribe buttons do not exist then fail validation test.
/// </summary>
/// <param name="webelementInfo"></param>
public void Validate_WebsiteSubscriptionManageLink(RepoItemInfo webelementInfo)
{

if (webelementInfo.Exists())
{
Report.Log(ReportLevel.Info, "Validation", "Validating Exists on item 'Testing.WebsiteSubscriptionManageLink'.", repo.ThirtyOneGifts.WebsiteSubscriptionManageLinkInfo, new RecordItemIndex(0));
Validate.Exists(repo.ThirtyOneGifts.WebsiteSubscriptionManageLinkInfo);
Delay.Milliseconds(0);

}
else
{
Report.Log(ReportLevel.Warn, "Validation", "Unable to Validate Exists on item 'Website Subscription Manage Link'. May not have active Website Subscription? ", webelementInfo);

var personalWebsiteSubscripeButton = repo.ThirtyOneGifts.PersonalWebsiteSubscribeButtonInfo;

if (personalWebsiteSubscripeButton.Exists())
{
Report.Log(ReportLevel.Success, "Validation", "Validating Exists on item 'Personal Website Subscribe Button'. ", personalWebsiteSubscripeButton);
Report.Warn("Validation", "Calling 'AddConsultantWebsiteSubscription.rxrec' to add an active subscription.");
Recordings.AddConsultantWebsiteSubscription.Start();

}
else
{
Report.Log(ReportLevel.Info, "Validation", "Validating Exists on item 'Testing.WebsiteSubscriptionManageLink'.", repo.ThirtyOneGifts.WebsiteSubscriptionManageLinkInfo, new RecordItemIndex(0));
Report.Failure("Validation","Failed Validating Exists on item 'Testing.WebsiteSubscriptionManageLink'.");
Ranorex.Validate.Fail("Failed to find item 'Testing.WebsiteSubscriptionManageLink'.");
}
}
}
}
}

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

Re: Binding variables in UserCode

Post by odklizec » Tue Jan 29, 2019 2:02 pm

Hi,
Eamonn wrote:
Tue Jan 29, 2019 1:57 pm
I am calling module using Recordings.AddConsultantWebsiteSubscription.Start();
And this is why it will always fail. You must insert the code module to the solution, otherwise, you will be unable to bind the variables. There is no way to bind a module variable to the data connector, if the module is not placed in solution. Starting modules using Start() method, is generally bad idea.
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

Eamonn
Posts: 63
Joined: Wed Jul 16, 2014 9:30 am
Location: Dundalk, Ireland.

Re: Binding variables in UserCode

Post by Eamonn » Tue Jan 29, 2019 3:01 pm

Thank you for confirming this, so when I use a code module then I can access the variable to bind. I will create a new Code Module.

User avatar
Vega
Posts: 160
Joined: Mon Jul 11, 2016 2:15 pm

Re: Binding variables in UserCode

Post by Vega » Tue Jan 29, 2019 8:09 pm

When you do get around to creating your code module, the variables are created a little different. Right click the code editor while your code module is open and select Insert new module variable.
post1.png
post1.png (52.55 KiB) Viewed 238 times
post2.png
post2.png (14.6 KiB) Viewed 238 times
If you just create a normal var in code like

Code: Select all

int abc = 0;
then Ranorex will not be able to bind data to this variable as is. The method above creates the variable for you including get/set and a GUID.

hope this helps