SeleniumHQ / selenium-ide

Open Source record and playback test automation for the web.
https://selenium.dev/selenium-ide/
Apache License 2.0
2.72k stars 739 forks source link

The new line character \n in string will block assertion #1833

Closed heandping closed 2 days ago

heandping commented 1 month ago

Dear support,

Im trying to capture the inner text of an element locator, and verify if it contains a certain string. The inner text of this element has a new line character, like below, 'QA auto testing medicine\n240205-1 - 14105'

IDE won't let me go through , it give the error below executeScript on return document.querySelector("tbody tr:nth-child(1) td:nth-child(2)").innerText.includes(${invoice_firstItem}); with value is_include_firstItem Failed:Bad control character in string literal in JSON at position 10 (line 1 column 11)

So I have to deal with \n first, try to eliminate it with javascript function replace(). it still gives the error, and I don't think it is a syntax error executeScript on let a=document.querySelector("tbody tr:nth-child(1) td:nth-child(2)").innerText; let b=a.replace((/\n/g, ''); return b.includes(${invoice_firstItem}); with value is_include_firstItem Failed:Invalid regular expression: missing /

Wonder if the original string has special character, how we can do the assertion

Selenium Version: 3.17.2 Chrome version: 124.0.6367.79 (Official Build) (arm64)

toddtarsi commented 1 month ago

@heandping - I encountered this when using v4 at work. Actually, I changed waitForText in v4 to replace newlines with spaces and trim trailing and leading spaces on both entries using the below logic:

          const elText = (await el.getText()).replace(/\u00A0/g, ' ').trim()
          return elText === text.replace(/\u00A0/g, ' ').trim()

If you're able to, I'd recommend giving v4 a shot! Just got universal apps on MacOS working, so it's about 3x faster on M1 now.

https://github.com/SeleniumHQ/selenium-ide/releases/tag/v4.0.1-beta.11

heandping commented 1 month ago

Thanks for your help @toddtarsi , it works I tried to use /\u00A0/g instead of /\n/g in the replace function, error gone, which makes me think a little about this, so the original text which is 'QA auto testing medicine\n240205-1 - 14105', \n is not a real \n, it may be a non-breaking space, so when I try to replace \n, it failed.

toddtarsi commented 1 month ago

Very interesting! I don't mean to sound confused, but if you have any other optimizations to propose to remove non-intentional characters, let me know and I can look at doing em. I'm still feeling out the adjustments here!

toddtarsi commented 2 days ago

Closing this, glad you got through it @heandping!