0

I am performing string comparison in selenium automation. For a field, I am getting this value from UI:

Actual Value: Institutional Investors & Consultants — Canada

And same value, I have stored as a String expected value in my test.

Expected Value: Institutional Investors & Consultants — Canada

When I perform, testng assertEqual method to this, this is failing with this error message:

Actual Result : Institutional Investors & Consultants — Canada 
Expected Result : Institutional Investors & Consultants — Canada

I tried to remove these special character with:

replaceAll("â€", "-")

but failed with the same message.

Try setting the encoding in the constructor of string:

subject = new String(subject.getBytes("UTF-8"), "UTF-8");

This also didn't work.

I have apache common lang3 and jsoup also. If anyone knows any good method from there to tackle this, let me know, as first I want to avoid regex and use standard libraries.

Code:

if(loc contains ("Canada")) {
 Assert.assertEquals("somelocator.getText().trim()", "ExpectedValue");
}

Moreover, just simple println is prining as:

Institutional Investors & Consultants — Canada for Institutional Investors & Consultants — Canada.

When I print it in different console, then it prints fine.

QualityMatters
  • 895
  • 11
  • 31

2 Answers2

1

Your second code snippet probably failed because as it seems you forgot to include the ” sign in the repaceAll call: Here you see that there are different possibilities:

replaceAll('—', '—') 
replaceAll('–', '–')
replaceAll('•', '-')

Different length hyphens have different encodings and therefore won't compare as equal.

Tanque
  • 315
  • 3
  • 16
  • 1
    Thanks for this. It might help someone. But It wouldn't work if your file encoding is set differently at global level. – QualityMatters Oct 28 '21 at 09:31
  • 1
    Yes you are right, I just thought I have to mention this as this approach was one you used while trying to figure out your current issues – Tanque Oct 28 '21 at 09:40
0

The issue got fixed in IDE (Intelij)->

  1. ctrl+alt+s (Setting) -> Editor-> File Encoding -> Change file encoding to UTF-8. By default , it is set to windows-1252

It need to change for files individually as well from there.

  1. The same issue was coming in Jenkin (windows) machine also. It got fixed by: adding encoding in pom.xml file at 3 places mentioned in this answer.
QualityMatters
  • 895
  • 11
  • 31