cancel
Showing results for 
Search instead for 
Did you mean: 

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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Hero

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

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================

View solution in original post

9 REPLIES 9
Community Manager

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

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 Gorbunova
SmartBear Community Manager

 Join SmartBear Connect next April in Boston to meet with software development, API and testing experts from around the world! Get your ticket today!
Occasional Contributor

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

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 ?
 
evaluexpath-keyword.pngEvaluateXpath - Result Output.png
 
 
Community Hero

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

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Occasional Contributor

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

@AlexKaras 

 

a) What line generates the exception?

<Ravi> its on Line 4

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

<Ravi> I have not configured any NewValue Parameter, testcomplete tool mentions this in the error message generated.

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

<Ravi> When i add return, error is generated as 

An error occurred while executing the code snippet: SyntaxError: Unexpected token return
 
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.
 
 
 
Occasional Contributor

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

@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?

Community Hero

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

@kkrush 

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

 

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Occasional Contributor

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

@AlexKaras 

 

 

a) What line generates the exception?

<Ravi> Right now it is on Step 4 .

 

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

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

<Ravi> 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 "log in to load" and take its xpath.

3) use evaluatexpath function to get the total coupons who have the text "log in to load" text.

4) output message saying ("the total count of buttons whose value is log in to load is -"  xx)

Note : Rename the attahment to .zip.

 

Highlighted
Community Hero

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

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 Expert Group]
____
[Community Expert Group] members 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. Postings made by [Community Expert Group] members
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.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================

View solution in original post

Occasional Contributor

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

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.pageDigitalCouponCenterURL.panel.EvaluateXPath("//a[contains(@class, \'login-to-load ng-star-inserted\')]");
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/coupons-weekly-circular/digital-coupons/#/available

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);