Highlighted
Absent Member.. Absent Member..
Absent Member..
1493 views

TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

I initially posted this in the wrong forum, so sorry for the double post.

I have an `Evaluate JS on object` step that finds an object, which has the following object.innerHTML.

Foo bar<br />BAZ
<br />


I need to extract BAZ from this text to use elsewhere, so I have the following code in the JS portion, to extract it using a regex:

var regex = /Foo\s+Bar<br\s+\/>(.*)\s*<br \/>/i;
var matches = [];
var match;
matches = regex.exec(object.innerHTML);
match = matches[matches.length - 1];
alert(match);  // show what's being matched.


However, when I run the script, this fails with the following error:

** 6: Evaluate JavaScript var regex = /Foo\s+B... alert(match); on Foo Bar ** failed - an argument is invalid: 'Code': JavaScript exception 'TypeError: matches is null' during evaluation


I have tested this regex here: https://regex101.com/r/dO7gY7/1

Using this Javascript tester site (http://www.webtoolkitonline.com/javascript-tester.html), I successfully tested the following variant on the code, to ensure that it extracts what I need it to:

var data = "Foo Bar<br />BAZ<br />";
var regex = /Foo\s+Bar<br\s+\/>(.*)\s*<br \/>/i;
var matches = [];
var match;
matches = regex.exec(data);
match = matches[matches.length - 1];
alert(match);


This returns BAZ as expected.

0 Likes
1 Solution

Accepted Solutions
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

It turns out this was a generic Javascript issue, not a TruClient-specific issue.  I was able to resolve it, and the solution is posted here.

---

 

View solution in original post

0 Likes
5 Replies
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

Hello drenze,

Do you have the javascript function defined in JS-functions.js file?

Regards,

Daniela Gómez Alvarado
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the STAR at the bottom left of the post and show your appreciation.
0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

No, I don't have it defined in JS-Functions.js.  I'm entering the JS code directly into the "Arguments" section of the "Evaluate Javascript on Object" step.  This is essentially the same code that worked prior to the changes I'm testing, with the exception of the addition of the regex to the mix.  In the previous version of the code, this was a field in a table, and I was easily able to grab the "object.innerHTML" and store it for future use.  But since there was never more than one row to the table, the devs made the decision to emulate the functionality of the table with:

<div class="column">
Foo Bar<br />
BAZ<br />
&nbsp;
</div>

The "Evaluate Javascript on Object" step identifies the correct <div>...</div> block, but I need to grab JUST the "BAZ" from the innerHTML.

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

Note: When I define the regex in a function in JS-functions.js, and then call it from the Arguments, it still gives the same error.  I didn't think this was the issue, but wanted to check.

0 Likes
Highlighted
Absent Member.. Absent Member..
Absent Member..

Re: TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

It turns out this was a generic Javascript issue, not a TruClient-specific issue.  I was able to resolve it, and the solution is posted here.

---

 

View solution in original post

0 Likes
Highlighted
Acclaimed Contributor.
Acclaimed Contributor.

Re: TruClient - Using Regex in `Evaluate JS on object` script.

Jump to solution

Hello drenze,

 

Thank you for share your solution.

Regards,

Daniela Gómez Alvarado
Customer Support Engineer

If you find that this or any other post resolves your issue, please be sure to mark it as an accepted solution.
If you are satisfied with anyone’s response please remember to give them a KUDOS by clicking on the STAR at the bottom left of the post and show your appreciation.
0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.