Reputation: 57
I am using key word driven and data driven framework. Expected string is coming from excel sheet and Actual from webpage even though both variable print exactly same strings still the Test script fails. It does "PASS" for strings with no space like "Resign" but "FAIL" for string with space like "Property Search" below is the result which prints PASS till "Resign" after that it fails ignore the numbers its for debugging. I also included xpath used in result.
Result generated
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[1]
Tool tip text present :- Revals
Revals
1
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[2]/a[contains(text(),'Managed Client Accounts')]
Tool tip text present :- Managed Client Accounts
Managed Client Accounts
1
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[3]
Tool tip text present :- Resigns
Resigns
1
By.xpath: .//[@id='dashboard-dropdown']/li[4]/a
Tool tip text present :- Outstanding AR
Outstanding AR
4
By.xpath: .//[@id='menuCreateDashboard']
Tool tip text present :- Manage Dashboard
Manage Dashboard
4
By.xpath: html/body/div[2]/div/div[2]/ul/li[2]/a
Tool tip text present :- Property Search
Property Search
4
By.xpath: html/body/div[2]/div/div[2]/ul/li[3]/a
Tool tip text present :- Tax Resource Database
Tax Resource Database
4
By.xpath: html/body/div[2]/div/div[2]/ul/li[4]/a[contains(text(),'Quick Links')]
Tool tip text present :- Quick Links
Quick Links
4
Here is the code:
public String verify_Text(String locatorType, String value, String data){
try
{
By locator;
locator = locatorValue(locatorType, value);
System.out.println(locator);
WebElement element = driver.findElement(locator);
//WebElement element = driver.findElement(By.cssSelector("#header>h1 a"));
// Get tooltip text
String toolTipText = element.getText();
System.out.println("Tool tip text present :- " + toolTipText);
System.out.println(data);
// Compare toll tip text
if(toolTipText.contentEquals(data))
{
System.out.println("1");
return PASS;
}
}
catch(Exception e)
{
LOG.error(Executor.currentSheet + ":" + e);
getScreenshot("verify_Link", data);
System.out.println("3");
return FAIL;
}
getScreenshot("verify_Link", data);
System.out.println("4");
return FAIL;
}
Upvotes: 0
Views: 1618
Reputation: 57
line of code which worked for me
if(toolTipText.replaceAll("\\s+","").equalsIgnoreCase(data.trim().replaceAll("\\s+","")))
I realized after using this code and debugging is data passed from excel sheet had space in between two letter whereas there was no space on webelement tool tip .But this I was not able to figure it out using inspect element as its not visible. before using replaceall("\s+","")
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[5]/a Tool tip text present :- ManageDashboard Manage Dashboard
after using replaceAll("\s+","") and removing space from data coming from excel sheet
By.xpath: html/body/div[2]/div/div[2]/ul/li[1]/ul/li[5]/a Tool tip text present :- ManageDashboard ManageDashboard
Upvotes: 1
Reputation: 25597
In my opinion, your function is trying to do way too much and is overly complicated for the simple task you are trying to accomplish. I think at least part of the issue is that you are using contentEquals()
instead of equals()
.
You are using Java. There is a great library that will take care of all the different comparisons you would want to do with it called JUnit. Use that to do your comparisons and clean up the code and you can use code like the below.
String expectedTooltip = "some value from Excel";
By locator = By.cssSelector("#header > h1 a");
getScreenshot("verify_Link", expectedTooltip);
Assert.assertEquals("verify tooltip", expectedTooltip, getTooltip(locator));
and a support function that just gets the tooltip from the element specified by the locator. You don't want to pass around strings and convert them to locators. Use the By
class instead. It makes things simpler. I added a .trim()
here to get rid of any unintended spaces at either end of the string.
public String getTooltip(By locator)
{
return driver.findElement(locator).getText().trim();
}
If you still have issues with string not matching, there may be non-printable characters in some of those strings that you will have to remove/replace before performing the comparisons. One way to figure this out is to loop through each string and print the ASCII values for each character and see where the difference is.
Upvotes: 0
Reputation: 1233
Sometimes when you fetch the text from WebElements, they tend to have a leading or trailing space(s) in them. A better comparison would be
toolTipText.trim().equals(data.trim())
If this still doesn't work, maybe remove all the spaces from the strings and then compare. Like this:
toolTipText = toolTipText.trim().replaceAll("\\p{Z}", "");
data = data.trim().replaceAll("\\p{Z}", "");
toolTipText.equals(data)
Here, the string Outstanding AR
will become OutstandingAR
after replaceAll
. This is certainly an overkill, but if it works, then it's evident that the spaces in the string is what caused the problem.
Upvotes: 0
Reputation: 66
I don't have enough rep to comment but I am suggesting some things to try:
You could trim the string, perhaps there is a char that is not visible in one of the Strings.
if(toolTipText.trim().contentEquals(data.trim()))
Or you could try to use normalize-space to remove tab, carriage return and line feed characters.
/*/a[normalize-space() = 'Hello World']
Upvotes: 0