How to bind varables to Code module from Excel file

Ask general questions here.
avi6666
Posts: 14
Joined: Tue Apr 17, 2018 12:40 pm

How to bind varables to Code module from Excel file

Post by avi6666 » Mon Jul 01, 2019 7:55 am

How to bind variables to Code module from Excel file

i have situation where i have bind variables (column) form excel file to variables in code module

like

[TestVariable("379125e8-cc81-4051-9e88-0a0f48515f18")]
public string Printformat
{
get { return _Printformat; }
set { _Printformat = value; }
}

[TestVariable("c530e178-dbf8-4c16-a262-d07d9dc16f82")]
//for image comparison printValidate= "Contains|C:\\TestData\\TestPrint\\bmp2.bmp"
//for pdf text comaprison printValidate="Dr Test Practitioner|Ms Testadult Prescriptionceasemedication|PARACETAMOL DROPS 100mg/mL"
public string PrintVerify
{
get { return _PrintVerify; }
set { _PrintVerify = value; }
}

i want bind this two from excel

in suite level, it showing me to bind and i did it.



but i build it shows error
variables doesn't exist in current context

can anyone please help me out ???

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

Re: How to bind varables to Code module from Excel file

Post by odklizec » Mon Jul 01, 2019 8:41 am

Hi,

Please post entire code module. Without seeing your failing code, there is no way to tell what's wrong. My best guess is that the variable is used incorrectly in you code? Also, what Ranorex version do you use?
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

avi6666
Posts: 14
Joined: Tue Apr 17, 2018 12:40 pm

Re: How to bind varables to Code module from Excel file

Post by avi6666 » Mon Jul 01, 2019 11:58 am

string _PrintVerify;
[TestVariable("c530e178-dbf8-4c16-a262-d07d9dc16f82")]
public string PrintVerify
{
get { return _PrintVerify; }
set { _PrintVerify = here i want to get value from excel sheet bounded variable and set it ; }
}


set { _PrintVerify = here i want to get value from excel sheet bounded variable and set it ; }

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

Re: How to bind varables to Code module from Excel file

Post by odklizec » Mon Jul 01, 2019 2:07 pm

Hi,

You must declare new variable anywhere before Run section. So the code module should look like this:

Code: Select all

/*
 * Created by Ranorex
 * User: pkudrys
 * Date: 01/07/2019
 * Time: 14:54
 * 
 * To change this template use Tools > Options > Coding > Edit standard headers.
 */
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 RPDashboard.CodeModules
{
    /// <summary>
    /// Description of TestCodeModulWithVariable.
    /// </summary>
    [TestModule("AD604456-9CD5-48FE-8006-32A2F72F6A7B", ModuleType.UserCode, 1)]
    public class TestCodeModulWithVariable : ITestModule
    {
   
        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        public TestCodeModulWithVariable()
        {
            // Do not delete - a parameterless constructor is required!
        }

        string _varXYZ = "";
	    [TestVariable("2b46b416-cd6c-4c71-88b4-828bb3317560")]
	    public string varXYZ
	    {
	    	get { return _varXYZ; }
	    	set { _varXYZ = value; }
	    }     
        /// <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.Log(ReportLevel.Info, "varXYZ:" + varXYZ);
        }
    }
And then you should be able to bind the code module's variable with excel data connector of your choice.
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

avi6666
Posts: 14
Joined: Tue Apr 17, 2018 12:40 pm

Re: How to bind varables to Code module from Excel file

Post by avi6666 » Tue Jul 02, 2019 5:32 am

I did the same as you have mentioned

string _varXYZ = "";
[TestVariable("2b46b416-cd6c-4c71-88b4-828bb3317560")]
public string varXYZ
{
get { return _varXYZ; }
set { _varXYZ = value; }
}

my doubt

set { _varXYZ = value; }

what happens in the above statement, value get the data from excel column and assigns it the variable ???

becoz i did the same

string _PrintVerify = " ";
public string PrintVerify
{
get { return _PrintVerify; }
set { _PrintVerify = PrintVerify; }
}

and i binded excel data from suite level , when i build it says error as variables doesn't exist in current context

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

Re: How to bind varables to Code module from Excel file

Post by odklizec » Tue Jul 02, 2019 7:56 am

Hi,

Please post your code module (entire source). I'm sure there is something wrong in the way you apply the module variable?
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

avi6666
Posts: 14
Joined: Tue Apr 17, 2018 12:40 pm

Re: How to bind varables to Code module from Excel file

Post by avi6666 » Tue Jul 02, 2019 10:21 am

attached code file
Attachments
ValidatePrintedCopy.cs
(3.1 KiB) Downloaded 5 times

avi6666
Posts: 14
Joined: Tue Apr 17, 2018 12:40 pm

Re: How to bind varables to Code module from Excel file

Post by avi6666 » Tue Jul 02, 2019 11:22 am

/*
* Created by Ranorex
* User: tester
* Date: 29/09/2016
* Time: 1:34 PM
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
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;
using CommonLibraries.Libraries;

namespace CommonModules.CodeModules
{
/// <summary>
/// Description of ValidatePrintedCopy.
/// </summary>
[TestModule("15AA4F81-D48A-4B81-BC46-8320CC9056BB", ModuleType.UserCode, 1)]
public class ValidatePrintedCopy : ITestModule
{
/// <summary>
/// Constructs a new instance.
/// </summary>
public ValidatePrintedCopy()
{
// 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

string _Printformat = " ";
[TestVariable("379125e8-cc81-4051-9e88-0a0f48515f18")]
public string Printformat
{
get { return _Printformat; }
set { _Printformat = value; }
}

/// that will in turn invoke this method.</remarks>
string _PrintVerify = " ";
[TestVariable("c530e178-dbf8-4c16-a262-d07d9dc16f82")]
//for image comparison printValidate= "Contains|C:\\TestData\\TestPrint\\bmp2.bmp"
//for pdf text comaprison printValidate="Dr Test Practitioner|Ms Testadult Prescriptionceasemedication|PARACETAMOL DROPS 100mg/mL"
public string PrintVerify
{
get { return _PrintVerify; }
set { _PrintVerify = value; }
}

void ITestModule.Run()
{
Mouse.DefaultMoveTime = 300;
Keyboard.DefaultKeyPressTime = 100;
Delay.SpeedFactor = 1.0;
CustomKeywords keywords=new CustomKeywords();
keywords.addDelay("largeWait");


if(Printformat.Equals("pdf",StringComparison.InvariantCultureIgnoreCase))
{
FileOperations fileop1=new FileOperations();
fileop1.validateprintedpdfText(PrintVerify);
}

else
{
if(Printformat.Equals("bmp",StringComparison.InvariantCultureIgnoreCase))
{
ImageRelated img=new ImageRelated();

img.validatePrintedbmp(PrintVerify);
}

else
{
Report.Warn("Wrong printed copy fileformat specified,use either pdf or bmp");
}

}
}



}
}

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

Re: How to bind varables to Code module from Excel file

Post by odklizec » Tue Jul 02, 2019 11:42 am

Thanks. The code module looks OK. At which line the test fails exactly? And are you sure it does not fail in one of the CommonLibraries methods? Any chance you could post entire (zipped) test suite? BTW, what Ranorex version do you use?
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