Ask a Question

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

SOLVED
kkrush
Occasional Contributor

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-ac...



   // 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.");
   }
}

9 REPLIES 9
TanyaYatskovska
SmartBear Alumni (Retired)

Hi @kkrush,

 

You can use the EvaluateXPath in the Run Code Snippet Operation.

Also, you can run a script routine from keyword tests - https://support.smartbear.com/testcomplete/docs/keyword-testing/reference/test-actions/run-script-ro...

 

Does it help?

 

 

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



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
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\')]");

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
kkrush
Occasional Contributor

@AlexKaras 

 

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\')]");
 
 
I am attaching the project file, can you please take a look.
keyword test - Test2
 
Test case Steps
Go to url : 
Maximize the browser
Get the xpath of a button (which is the code snippet) , using "evaluateXpath"
Process it to check how many are there on the page.
Display the count as an output.
 
 
Note : The same keyword when converted to a script, it works fine, I am curious to know why keyword test is not working.
 
 
 
kkrush
Occasional Contributor

@AlexKaras 

Hello, 

 

Can you let me know if you received my earlier  response  sent today ? and with the project files in the response to you?

AlexKaras
Champion Level 3

@kkrush 

No. I got mail notification but your reply disappeared then...

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
kkrush
Occasional Contributor

@AlexKaras 

 

 

a) What line generates the exception?

 Right now it is on Step 4 .

 

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

 I have no clue why testcomplete says "NewValue" as I had not configured it anyhwere.

 

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\')]");

 Even if I add "return", it says return type unknown or invalid.

 

I am attaching the project.

keyword test : test 2

script - unit 7

 

here is the steps

1) access the url

2) select the button  and take its xpath.

3) use evaluatexpath function to get the total coupons who have the text

4) output message saying ("the total count of buttons whose valueis -"  xx)

Note : Rename the attahment to .zip.

 

AlexKaras
Champion Level 3

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.

 

 

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
kkrush
Occasional Contributor

Hi Alex, 

 

Thank you for the reply, after going thru the documentation (https://support.smartbear.com/testcomplete/docs/app-testing/desktop/windows-store/addressing-objects...), I see where the problem is, 

EvaluateXPath(), this method returns SAFEARRAY that you can't assign to any existing type of KDT variables (you also mentioned the same). With TestComplete 14.0,  internal casting allows assigning them to Tables. The resulting table has one column and one row per each object returned by EvaluateXPath().

 

So now the script fails when I try to get the resulting object into a variable.(see screeshot attached)

If the Keyword test is converted to a script, it fails the same steps as that of keyword test.

Any help in resolving this issue is appreciated, I would like to know what is wrong in the code below?

 

Error observed in code

var LastResult;
LastResult = Aliases.browser.page.panel.EvaluateXPath("//a[contains(@class,)]");
Project.Variables.VarTemp.$set("Item", "", "", LastResult);
if(Project.Variables.VarTemp != null)

 


error thrown
JavaScript runtime error.
Error: Type mismatch.
Error location:
Unit: "TestProject1\TestProject1\Script\Unit3"
Line: 12 Column: 30.

 

TestCase Steps:

Go to url : https://stopandshop.com/

get the xpath for the text "+ load to card"

use the evaluatexpath method to get the total.

count the total objects that have "+load to card" label

display output message "Total found :" xx);

 

 

cancel
Showing results for 
Search instead for 
Did you mean: