Forum Discussion

gshaikh's avatar
gshaikh
Occasional Contributor
7 months ago

How to assign value of Object Property to Global variable created dynamically during test run.

I have created an Global Variable 'FirstLinkName', i want to assign  property value of Object 'Link_FirstLinkName' to variable using run code snippet. Is this achievable?

I tried using Last operation result and assigning value using Set variable value but if variable is being used in same test runs for multiple times its not clearing out previous value.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Can you provide screenshots, of what you have done so far? So that we can get a better understanding

  • gshaikh's avatar
    gshaikh
    Occasional Contributor

    Hi rraghvani please see screenshot below.. on step 1 i am trying to create Global variable using script routing AddVariable()

    in second step i want to assign value of property 'title' to variable using run code snippet from an mapped object 'Aliases.browser.page_Search_Lightning.link_firstResultLink'

    I was trying something like below 

    Project.Variables.VariableByName["Coveo_FirstLinkName"] =Aliases.browser.page_Search_Lightning.link_firstResultLink.contentText;

    but its giving syntax error

    hope this information will help.

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      In Keyword Testing, you have the option to create a variable,

      You can specify a constant value to the variable, 

      and use Log to output the value,

      In JavaScript code, you can assign the variable like so,

      and in your Keyword Test call the routine to assign the variable, and again use Log to output the value,

       

  • gshaikh's avatar
    gshaikh
    Occasional Contributor

    Hi rraghvani,

    Issue with creating an local variable in keywordtest is if we use same keyword test multiple times in an test run it retains previous value..so i am trying to create and remove variable after running the specified keyword test.

    hence i want an solution how i can assign value to dynamically created variable from an Object property.

    • rraghvani's avatar
      rraghvani
      Champion Level 3

      Here's an example using JavaScript code of assigning an object (Page Object) to a variable (var page), which is then used to find a button using the extended method (FindElement)

      var page = Sys.Browser("chrome").Page("https://www.w3schools.com/css/css3_buttons.asp");
      var button = page.FindElement("//button[.='CSS Button']");
      

      It's not necessary to delete a variable, you can assign an empty value to it instead. You can then check, if it's an empty value, assign a value to it. Perform the necessary steps, then set the variable to an empty value once finished. 

      Code example,

      If you don't assign an empty value to Var1, the previous value is stored shown in Local Value. Assigning Var1 to "Good Bye World" will not work.

      You need to ensure your logic is correct