Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
heandping opened this issue May 18, 2024 · 3 comments
Open

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

heandping opened this issue May 18, 2024 · 3 comments

Comments

@heandping
Copy link

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
Copy link
Contributor

@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
Copy link
Author

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
Copy link
Contributor

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants