Forum Discussion

Azeddin_Margani's avatar
Azeddin_Margani
Contributor
8 years ago

Using variables within text

Hi,

 

Could someone please help - I want to use variables fed from spread sheet within piece of text.

 

The part of text i need to replace as variables is highlighted in red:

.ClickItem ("Product Order: Megarider 4 Weekly (Adult). Card 6432323412399756454", 0);

 

My attempted code:

.ClickItem ("Product Order: "Project.Variables.UserData..Value("ProductName") (Adult). Card "Project.Variables.UserData.Value("ProductName")", 0);

 

Is there something wrong with this code?.

 

Your help is appreciated..

 

Regards,

A.M.

 

 

 

 

  • Here you go, this function will replace the things you want.

  • shankar_r's avatar
    shankar_r
    Community Hero

    Maybe we have to write a function that can differentiate the String and and Project variable.

     

    Something like this,

     

    Excel cell value = "String1<projectvariablename1>String2<projectvariablename2>"

     

    then you can write a function which will get the name of the project and returns the project variable value.

     

     

    • shankar_r's avatar
      shankar_r
      Community Hero

      Here you go, this function will replace the things you want.

  • HKosova's avatar
    HKosova
    SmartBear Alumni (Retired)

    Azeddin_Margani wrote:
     My attempted code:

    .ClickItem ("Product Order: "Project.Variables.UserData..Value("ProductName") (Adult). Card "Project.Variables.UserData.Value("ProductName")", 0);


    That's almost correct. You want to concatenate the strings using +:

    .ClickItem("Product Order: "
    + Project.Variables.UserData.Value("ProductName")
     + " (Adult). Card "
    + Project.Variables.UserData.Value("CardNumber"), 0);

    Alternatively, you can use aqString.Format to build the string. %s are placeholders that will be replaced with the variable values:

    var strItem = aqString.Format(
    "Product Order: %s (Adult). Card %s"),
    Project.Variables.UserData.Value("ProductName"),
    Project.Variables.UserData.Value("CardNumber"));
    ...ClickItem(strItem);