Cannot do JS slice (string method) on a string inside a variable

Hello,
I have the following problem. If I run this snippet
Uploading: image.png…
I get the correct results, which is the last 16 characters of the string " collection number is Opkrævning blev dannet - Collection: 1061190004472717"

However, if I run it like so, using a variable I captured a string in, it will not do slice, I will have the original string not sliced in $collectionNumber variable and I don’t understand why.

Anyone has any ideas how to make this work?
Thanks!

Try something like this

open | http://localhost/TestPage.htm | |
storeText | xpath=(.//*[normalize-space(text()) and normalize-space(.)='Cell_1_0'])[1]/following::td[1] | cellValue |
echo | ${cellValue} | |
runScript | newVal = '${cellValue}'.slice(-3); return newVal | newVal |
echo | ${newVal} | |

which for me produces the log

[info] Executing: | open | http://localhost/TestPage.htm | |
[info] Executing: | storeText | xpath=(.//*[normalize-space(text()) and normalize-space(.)='Cell_1_0'])[1]/following::td[1] | cellValue |
[info] Store 'Cell_1_1' into 'cellValue'
[info] Executing: | echo | ${cellValue} | |
[info] Expand variable '${cellValue}' into 'Cell_1_1'
[info] echo: Cell_1_1
[info] Executing: | runScript | newVal = '${cellValue}'.slice(-3); return newVal | newVal |
[info] Expand variable 'newVal = '${cellValue}'.slice(-3); return newVal' into 'newVal = 'Cell_1_1'.slice(-3); return newVal'
[info] Store '1_1' into 'newVal'
[info] Executing: | echo | ${newVal} | |
[info] Expand variable '${newVal}' into '1_1'
[info] echo: 1_1

Note that I’m not using the window reference and I’m doing the slicing and returning in a single runScript command.

Hello, thanks for the answer but it’s not working. I don’t get it, it works fine if I set it up in a simple test, but in this long test it just refuses to work :frowning:

[info] Executing: | storeText | //td[@id=‘box5’]/div/div | tempVal |

[info] Store ‘Opkrævning blev dannet - Collection: 1061190004472732’ into ‘tempVal’

[info] Executing: | echo | ${tempVal} | |

[info] Expand variable ‘${tempVal}’ into ‘Opkrævning blev dannet - Collection: 1061190004472732’

[info] echo: Opkrævning blev dannet - Collection: 1061190004472732

[info] Executing: | runScript | newVal = ‘${tempVal}’.slice(-16); return newVal | newVal |

[info] Expand variable ‘newVal = ‘${tempVal}’.slice(-16); return newVal’ into ‘newVal = ‘Opkrævning blev dannet - Collection: 1061190004472732’.slice(-16); return newVal’

[info] Executing: | echo | ${newVal} | |

[info] Expand variable ‘${newVal}’ into ‘${newVal}’

[info] echo: ${newVal}

So I must be doing something wrong.

here is the log if the test only contains this slice code snippet

[info] Executing: | storeText | //td[@id=‘box5’]/div/div | tempVal |

[info] Store ‘IBA>Main>Dashboard’ into ‘tempVal’

[info] Executing: | echo | ${tempVal} | |

[info] Expand variable ‘${tempVal}’ into ‘IBA>Main>Dashboard’

[info] echo: IBA>Main>Dashboard

[info] Executing: | runScript | collectionNo = ‘${tempVal}’.slice(-16); return collectionNo | collectionNo |

[info] Expand variable ‘collectionNo = ‘${tempVal}’.slice(-16); return collectionNo’ into ‘collectionNo = ‘IBA>Main>Dashboard’.slice(-16); return collectionNo’

[info] Store ‘A>Main>Dashboard’ into ‘collectionNo’

[info] Executing: | echo | ${collectionNo} | |

[info] Expand variable ‘${collectionNo}’ into ‘A>Main>Dashboard’

[info] echo: A>Main>Dashboard

I had same issue like in this first log that it’s not exapnd. Use window.newVal instead of newVal.

At this point:

[info] Executing: | runScript | newVal = ‘${tempVal}’.slice(-16); return newVal | newVal |

try to use addScript and than in console of your page try:

console.log(new.Val);

and check what the output is.

Strange that it’s not working for you. I even copied the actual text and it worked for me.

You could try a different approach to get the collection number by using the string method split which creates an array from a string split on a given character. This assumes that the text you are checking is always in the same format as your example.

Add these two lines to the test to see if it works. I’m assuming the cellValue variable contains the text Opkrævning blev dannet - Collection: 1061190004472732

runScript | arr = '${cellValue}'.split(':'); return arr[1].trim() | newVal |
echo | ${newVal} | |

which for me gives the log

[info] Executing: | runScript | arr = '${cellValue}'.split(':'); return arr[1].trim() | newVal |
[info] Expand variable 'arr = '${cellValue}'.split(':'); return arr[1].trim()' into 'arr = 'Opkrævning blev dannet - Collection: 1061190004472732'.split(':'); return arr[1].trim()'
[info] Store '1061190004472732' into 'newVal'
[info] Executing: | echo | ${newVal} | |
[info] Expand variable '${newVal}' into '1061190004472732'
[info] echo: 1061190004472732

This splits the string on the colon into a two element array where the last element is the collection number. We trim the returned string to get rid of the leading space.

I tried that version as well, indeed if I replace the variable with the actual string ’ example string’ it works just fine.
I have no clue as to why it behaves like this.
As I posted above, if I run the same lines in a separate test on a different frame, I get the expected results.
It just refuses to store the sliced/split value. Maybe I should use

[info] Executing: | runScript | window.newVal = ‘${tempVal}’.slice(-16); return window.newVal | ${newVal} | ?

i find it confusing that sometimes you address a variable by its name, and other times you address it using ${variable}. Why not be consistent and use the same notation everywhere?

[info] Executing: | storeText | //td[@id=‘box5’]/div/div | tempVal |

[info] Store ‘Opkrævning blev dannet - Collection: 1061190004472742’ into ‘tempVal’

[info] Executing: | echo | ${tempVal} | |

[info] Expand variable ‘${tempVal}’ into ‘Opkrævning blev dannet - Collection: 1061190004472742’

[info] echo: Opkrævning blev dannet - Collection: 1061190004472742

[info] Executing: | runScript | window.arr = ‘${tempVal}’.split(’:’); return window.arr[1].trim() | newVal |

[info] Expand variable ‘window.arr = ‘${tempVal}’.split(’:’); return window.arr[1].trim()’ into ‘window.arr = ‘Opkrævning blev dannet - Collection: 1061190004472742’.split(’:’); return window.arr[1].trim()’

[info] Executing: | addScript | console.log(new.Val) | |

[info] Executing: | echo | ${newVal} | |

[info] Expand variable ‘${newVal}’ into ‘${newVal}’

[info] echo: ${newVal}

[info] Breakpoint: Stop.

[info] Pausing

[info] Resuming

[info] Executing: | runScript | window.newVal = ‘${tempVal}’.slice(-16); return window.newVal | newVal |

[info] Expand variable ‘window.newVal = ‘${tempVal}’.slice(-16); return window.newVal’ into ‘window.newVal = ‘Opkrævning blev dannet - Collection: 1061190004472742’.slice(-16); return window.newVal’

[info] Executing: | echo | ${newVal} | |

[info] Expand variable ‘${newVal}’ into ‘${newVal}’

[info] echo: ${newVal}

[info] Executing: | addScript | console.log(new.Val) | |

[info] Breakpoint: Stop.

thanks for answering, however I get error in the browser console when I try to do console.log(new.Val)

Uncaught SyntaxError: Unexpected identifier
and windo.newVal outputs the same result :confused:
And using window.whatever does not seem to alter my result at all.

I think using the notation ${variable} tells Katalon Recorder to expand the value held in the variable for output, but don’t quote me on that :shushing_face::slightly_smiling_face:

It looks like there’s a typo in there. There’s no need for the dot in new.Val
It should probably be

console.log(newVal)

tried it like that as well, I get
Uncaught ReferenceError: newVal is not defined
at :1:13
:weary:

Edit:
at least I can see ${collectionNo} in the browser console now :joy:
and that’s because i used it like this console.log(’${collectionNo}’) meh.

It just does not do the store part in the longer test for whatever reason

[info] Expand variable ‘collectionNo = ‘${tempVal}’.slice(-16); return collectionNo’ into ‘collectionNo = ‘Opkrævning blev dannet - Collection: 1061190004472750’.slice(-16); return collectionNo’

#### [info] Store ‘1061190004472750’ into 'collectionNo’

I would try find something without signs like this “æ”. Maybe this cause the problem.

that’s a good observation, but it’s not that, if I replace ${tempVal}

[info] Executing: | runScript | window.newVal = ‘${tempVal}’.slice(-16); | |

with

[info] Executing: | runScript | window.newVal = ’ Opkrævning blev dannet - Collection: 1061190004472744’.slice(-16); | |

I get ‘1061190004472744’
I’d like to know why :smile:

same page, this section in a new test runs with what I need. Why?

[info] Executing: | storeText | //td[@id=‘box5’]/div/div | tempVal |

[info] Store ‘Opkrævning blev dannet - Collection: 1061190004472746’ into ‘tempVal’

[info] Executing: | echo | ${tempVal} | |

[info] Expand variable ‘${tempVal}’ into ‘Opkrævning blev dannet - Collection: 1061190004472746’

[info] echo: Opkrævning blev dannet - Collection: 1061190004472746

[info] Executing: | runScript | collectionNo = ‘${tempVal}’.slice(-16); return collectionNo | collectionNo |

[info] Expand variable ‘collectionNo = ‘${tempVal}’.slice(-16); return collectionNo’ into ‘collectionNo = ‘Opkrævning blev dannet - Collection: 1061190004472746’.slice(-16); return collectionNo’

[info] Store ‘1061190004472746’ into ‘collectionNo’

[info] Executing: | echo | ${collectionNo} | |

[info] Expand variable ‘${collectionNo}’ into ‘1061190004472746’

[info] echo: 1061190004472746

It looks like here there is an additonal step

[info] Store ‘1061190004472746’ into ‘collectionNo’

which does not appear in the other runs.
strange.

Can you confirm that ${tempVal} is enclosed with single apostrophes ' and not the fancy curly ones? It might just be the way this forum formats text but it looks like the apostrophes are ‘${tempVal}’ as opposed to '${tempVal}'

Failing that, could you try enclosing the variable name in double-quotes "${tempVal}".slice(-16)

yep, simple ones ‘${tempVal}’, looks like the forum is converting them to the ’ fancy ones.
double quotes does nothing :confused:
thanks for trying to help, i’ll keep trying

I managed to make it work!
So, for whoever encounters the same issue, simply use storeEval command instead, and you will get the sliced string!
Eg:

#### [info] Executing: | storeText | //td[@id='box5']/div/div | y |
#### [info] Store 'Opkrævning blev dannet - Collection: 1061190004472764' into 'y'
#### [info] Executing: | echo | ${y} | |
#### [info] Expand variable '${y}' into 'Opkrævning blev dannet - Collection: 1061190004472764'
#### [info] echo: Opkrævning blev dannet - Collection: 1061190004472764
#### [info] Executing: | storeEval | x = '${y}'.slice(-16) | x |
#### [info] Expand variable 'x = '${y}'.slice(-16)' into 'x = 'Opkrævning blev dannet - Collection: 
1061190004472764'.slice(-16)'
#### [info] Store '1061190004472764' into 'x'
#### [info] Executing: | echo | ${x} | |
#### [info] Expand variable '${x}' into '1061190004472764'
#### [info] echo: 1061190004472764
1 Like

I should have known that :man_facepalming:

As a formatting tip, you can

indent preformatted text by 4 spaces

or

surround the text block  with 3 backticks ```

great, like the 4 spaces better. Thanks!
Still, there may be a bug with the runScript perhaps.