Forum Discussion

kkrush's avatar
kkrush
Occasional Contributor
5 years ago

EvaluateXpath - using keyword test , can we use evaluateXpath.?

I am trying to use keyword test to perform evaluatexpath, but am not able to, as per the below example/code, can we do the same with keyword test? if yes, can you please provide me with the keyword steps and screenshots if possible.

 

Appreicate your reply.

 

Example as given in the testcomplete documentation

https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/page/evaluatexpath-action-page-object.html



   // Obtain the Page object
   let url = "http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_type";
   Browsers.Item(btIExplorer).Run(url);
   let page = Sys.Browser("*").Page("*");

   // Call the function
   let arr = page.EvaluateXPath("//button[contains(@type, 'submit')]");

   // Check the result
   if (!strictEqual(arr, null))
   {
     // and click the first element that was found
    arr[0].Click(); // Note we refer to the array item
   }
   else 
   {
     // If nothing was found, post a message to the log
     Log.Error("Nothing was found.");
   }
}

  • Hi,

     

    Thank you for the project.

    Well, I was not able to access your tested site (no navigation error in the browser but the page was just empty), so I replaced it with https://www.potnoodle.com in both, Keyword test and NameMapping.

    Also I changed type of the VarTemp test local variable from String to Object (as it is expected to store a reference to the found web element, but not a string).

    After that I was able to execute keyword test with this line of code for the Run Code Snippet operation:

    Aliases.browser.pageXXXURL.QuerySelector("a.login-to-load.ng-star-inserted");

    (Obviously, the test reported that nothing was found but there was no exception.)

     

    Unfortunately, test failed with the Type Mismatch error when I tried to use .QuerySelectorAll() and store search result to the VarTemp variable. I think this is because .QuerySelectorAll() returns an array and (even empty) array cannot be assigned to the keyword test local variable of either Object or any other built-in type.

    I don't know if it is possible to assign an array that is obtained as the result of the last operation to the keyword test local variable as I am almost do not use keyword testing. Maybe those who use it will be able to provide you with some piece of advice.

     

     

     

    • kkrush's avatar
      kkrush
      Occasional Contributor

      Hi Tanya, 

       

      Thank you so much for the information, I have created the keyword script for evaluatexpath, but  the keyword test fails with the following exception "

      Unable to evaluate the operation's "NewValue" parameter. Error: ReferenceError: LastResult is not defined", please find the screenshot attached.
       
      I then converted the keyword test into a script and ran it, and it works fine.
       
      RunCode snippet used is : Aliases.browser.pageXXXXXXXXXURL.panel.EvaluateXPath("//a[contains(@class, \'login-to-load ng-star-inserted\')]");
      tried with "QuerySelectorAll", same thing, keyword test fails, but convert to script and run it, it works.
       
      Why is the keyword script failing, can you please help here ?
       
       
       
       
      • AlexKaras's avatar
        AlexKaras
        Champion Level 3

        Hi,

         

        a) What line generates the exception?

        b) What is this NewValue parameter? Where and how it is defined and used?

        c) I am not sure, but I think that something must be returned from the Run Code Snippet operation in order LastResult variable to be assigned a value. What if you change your line of code to

        return Aliases.browser.pageXXXXXXXXXURL.panel.EvaluateXPath("//a[contains(@class, \'login-to-load ng-star-inserted\')]");