Get last element in split array - get error

I have a parameter that extracts a string from the page. (using the ‘Get Text’ addon).
I split the value of this parameter using a delimiter (" ").
I then want to get the last element of this array.

This is the script I’m executing using the ‘Execute Javascript’ addon

var x = {param};
x = x.split(" ");
return x[x.length - 1];

I keep getting either Unexpected Identifier or javascript error: Unexpected token ‘;’

The string I’m trying to split is “x-x of x”.

For the record, I have tried using the ‘Split string with regex’ addon.
This gave me: “params = [x-x, of, x]”.

I then used this script

var x = {params};
return x[x.length - 1];

I get this error: javascript error: of is not defined

Despite it saying ‘Split string with regex’, I found out that its actually returning a string, due to accidentally using(not reading properly) the ‘Get Substring from Range’ addon where a hardcoded index gave the value “,” , which shouldnt be a thing if the return value was an array.

Hi @erik.yeow,
I have reproduced your issues and have found two errors:

  1. Your parameter is empty, which caused the first error message. Make sure your parameter has a value and is not empty.
  2. You didn’t add quotation marks before and after the string (the parameter), which caused the second error message.

Let me know if it helps solve your issue or if you are still facing problems.

The value of my param isnt empty as using the Get Text addon returns “x-x of x” as expected.

But, as per this suggestion, I changed my first script to

var x = "{param}";
x = x.split(" ");
return x[x.length - 1];

This seems to have solved my issue. Thanks.
I cant help but feel curious whether this is a Javascript thing or a TestProject thing as I was on the assumption that Get Text returns the value as a string.

Hi @erik.yeow, I am happy to hear that the issue is resolved.
When inserting the parameter’s value to the variable, it gets the value as it is, without quotation marks.
It is as if you tried to declare the variable this way: var x = x-x of x; instead of var x = "x-x of x";.

This topic was automatically closed after 180 days. New replies are no longer allowed.