Reputation: 759
I have to verify the tool tip message displayed in a table using selenium web driver.I have taken the actions class and used build.perform() but it returns an empty string. The problem is , html code is in such a way that the tool tip tags are incorporated with the span tag. Please let me know how to resolve this issue.
<span class="abcd" data-qtip="<table><tr><td>xxx as of 05/04/2017 </td><td>$1.00</td></tr><tr><td>yyy</td><td>$1.00</td></tr><tr><tdzzz</td><td>$0.00</td></tr><tr><td>xyz</td><td>0.00%</tr></td></table>"/>
xxx,yyy,zzz and xyz are the texts I need to verify.
Method I used:
public void mouseOver(String loc) {
Actions ToolTip1 = new Actions(driver);
WebElement pr = findElement(loc);
System.out.println(pr);
pause(2000);
ToolTip1.moveToElement(pr).perform();
pause(2000);
String toolTipMsg = pr.getText();
pause(1000);
System.out.println(toolTipMsg);
}
where loc = xpath of the element.
Any help on this is appreciated.
Upvotes: 1
Views: 1053
Reputation: 3384
try this:
Actions ToolTip1 = new Actions(driver);
WebElement pr = findElement(loc);
System.out.println(pr);
ToolTip1.moveToElement(pr).perform();
pause(2000);
String toolTipMsg = driver.findElement(by.cssSelector("span.abcd")).getAttribute("data-qtip");
System.out.println(toolTipMsg);
Upvotes: 1
Reputation: 2019
I was looking for a better solution and i came up with this.
Step 1: Store the result in a string,
String Tooltip = driver.findElement(By.xpath("//span[@class='abcd']")).getAttribute("data-qtip").toString();
Step 2: Use some regex pattern to get the data only from "td" tags and add it to a ArrayList.
ArrayList<String> lists = new ArrayList<>();
Pattern p = Pattern.compile("(<td>(.*?)<\\/td>)");
Matcher m = p.matcher(toolTip);
while(m.find())
{
String tag2 = m.group(1);
String tag = tag2.substring(4,(tag2.length()-5)); //removes the <td> and </td> tags in the matched string
lists.add(tag);
System.out.println(tag);
}
Now the lists will have all the table data's. Iterate through the list elements and compare it to the expected String. Thanks.
Upvotes: 0
Reputation: 2019
After doing mousehover to the webelement, use the getAttribute method to get the value of "data-qtip". Below line can help you.
String toolTip = driver.findElement(By.xpath("//span[@class='abcd']")).getAttribute("data-qtip").toString();
Store the expected message in another string and compare that with toolTip.
Since in this case, the string toolTip is in Html and we need to parse it to required format and compare it to the expected string. The below code parses the toolTip and gives the String which needs to be validated.
System.out.println(toolTip);
String[] word1 = toolTip.split("<td>");
String a = word1[1].split(" ")[0];
System.out.println(a);
String b = word1[3].split("</")[0];
System.out.println(b);
String c = word1[6].split("</")[0];
System.out.println(c);
String d = word1[4].split("<td")[1].split("<")[0];
System.out.println(d);
We can even parse it a better way but this works. Later validate a, b, c, d with the expected messages. Thanks
Upvotes: 1