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.
HTML code :
<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.
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
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);
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.
Related
How can I use selenium java to verify that the data table are masked after the 4th character.
I have used the below code to extract the WebElements from the GUI.
List IDds = driver.findElements(By.xpath("//tbody/tr/td[1]"));
Example of data:
1111$$$$
2222$$
Find all the elements you need in a loop, or give a CSS expression that returns a list of web elements:
WebElement myElm = driver.findElementByCSS("CSSExpression");
String text = myElm.getAttribute("Text");
// or
// String text = myElm.getAttribute("Value");
Char C = text.charAt(4);
if (C=='$')
// Good
else
// bad
You can also find the first location in the string where you see $ and make sure all the other chars are the same.
I am using below code to give date as input:
Selecting Journey Date
WebElement journeydate = driver.findElement(By.xpath("//select[#name='lccp_day']")).sendkeys(20);
I believe issue is because of sendkeys is not accepting integers.
Please give suggestion what else I can use to give input as integers for any text box.
Using JAVA Compiler 1.8 and Firefox version 42.
You could use
String.valueOf(20);
to add your integer value as a String.
And yes, judging by the documentation sendKeys only accepts CharSequences.
Instead of this
//Selecting Journey Date
WebElement journeydate = driver.findElement(By.xpath("//select[#name='lccp_day']")).sendkeys(20);
use this
//Selecting Journey Date
WebElement journeydate = driver.findElement(By.xpath("//select[#name='lccp_day']")).sendkeys("20");
SendKeys Function accepts only string. so you need to type case your int to string before passing it to Senkeys.
Nick has shown you one way.
You can also perform same like below :-
int i=0;
String j = Integer.toString(i);
driver.findElement(By.name("q")).sendKeys(j);
OR
Integer obj = new Integer(i);
String str4 = obj.toString();
driver.findElement(By.name("q")).sendKeys(str4);
OR
String str5 = new Integer(i).toString();
driver.findElement(By.name("q")).sendKeys(str5);
OR
String str6 = new Integer(1234).toString();
driver.findElement(By.name("q")).sendKeys(str6);
Hope it will help you :)
Have been at this for a while so I think it's time for some much needed help. I have the following if statement in an action listener('but' - being the Search button that gets clicked for the following to happen).I have an arrayList(used to save data from database) in my class, calling a specific record works perfectly. I am trying to create a search textField(named 'tf' below). I have been able to search only by typing an exact match to what ever is being searched, but I want to be able to use partial matching on any String in tf. Everything that's commented out is different stuff that I have been testing, therefore can be ignored.
The statement:
if( srch.getText().equals("You have chosen to search by movie: " )
... this is used to make sure that the person is searching only when the panel includes this title, as there is a genre search as well on the same panel. Only the title changes.
if(source.equals(but)){
//String b = a.get(0).get(1);
String result = tf.getText();
Pattern pat = Pattern.compile("[a-z]+");
Matcher m = pat.matcher(result);
//boolean bool = m.matches();
int i = 0;
//al.contains(result) && pat.matcher(a.get(i).get()).matches())
// && z.contains(result)
if( srch.getText().equals("You have chosen to search by movie: " )){
for (ArrayList<String> al : a){
if (pat.m(a.get(i).get(0)).matches()){
System.out.println(a.get(i).get(0)); //movie name
System.out.println(a.get(i).get(1)); //movie desc
}
i++;
}
//ta.setText(b);
}
else{
ta.setText("Please try searching by movie");
}
}
In summary, everything works fine. I just need a regex code to find partial matches as well and a way to add that into my loops. I've added as little code as possible as not to waste anyone's time, so please let me know if any other is needed.Many thanks in advance. Eventually any full or partial matches will display the movie name and description.
I think there is no need of regex.
String.contains(String chars) returns true if the source string have any occurrence of the argument string.
For example:
String str = "ABCDEFG";
if(str.contains("CD")){
System.out.println("Present");
}
I have been trying to get a number from a html string, but I cannot come up with a way to do it properly, I already looked for instructions for Jsoup, but I don't really understand how it works.
This is one of the strings I have to parse:
<span class="b">014:</span>
What I'm trying to get as output is 014, the name of the link. I need to get the number in a String variable, not Integer by the way.
Or this
<span class="b">08:</span>
For the 08.
The main problem I'm finding is that two things change in the string, the number after /paline/percorso/ and the number which is the name of the link. Could someone please help me?
If all your samples are like this, you can do this with simple string functions:
final String input = "<span class=\"b\">014:</span>";
final int i2 = input.lastIndexOf( "</a>" );
final int i1 = input.lastIndexOf( '>', i2 ) + 1;
final String result = input.substring( i1, i2 );
System.out.println( result );
I'd probably try a simple regex. Although depending on your string, it may be more complex than a simple quick and dirty regex.
String html = "<span class="b">014:</span>"
html.replaceAll( ".*<a.*>([0-9]*)</a.*", "$1" );
You can try something like:
Elements resultLinks = doc.select("span.b > a");
for (Element e:resultLinks)
String yourText=e.text();
Is there a way to put raw HTML inside of a Label widget with GWT? The constructor and setText() methods automatically escape the text for HTML (so < appears as <, etc).
What I need is something like:
String matched = "two";
List<String> values = Arrays.asList("one", "two", "three");
StringBuilder sb = new StringBuilder();
for (String v : values){
if (v.equals(matched)){
sb.append("<b>" + v + "<b>");
} else {
sb.append(v);
}
sb.append(", ");
}
Label label = new Label();
label.setRawText(sb.toString());
//div contains the following HTML: "one, <b>two</b>, three, "
I want to output a comma-separated list of Strings, but I want one of those Strings to be bolded. Thanks.
Use the HTML class (Or more likely: The InlineHTML class instead of the Label class. InlineHTML works like label, except that you can give it html.
And just a security warning: if part of the input to your InlineHTML object is input by the user, remember to strip html out of that part, so users can't insert their own scripts into your code.
Sorry, I'm going to answer my own question because I found what I was looking for.
The SafeHtmlBuilder class is perfect for this. You tell it what strings you want to escape and what strings you do not want to escape. It works like StringBuilder because you call append methods:
String matched = "two";
List<String> values = Arrays.asList("one", "two", "three <escape-me>");
SafeHtmlBuilder builder = new SafeHtmlBuilder();
for (String v : values){
if (v.equals(matched)){
builder.appendHtmlConstant("<b>");
builder.appendEscaped(v);
builder.appendHtmlConstant("</b>");
} else {
builder.appendEscaped(v);
}
builder.appendEscaped(", ");
}
HTML widget = new HTML();
widget.setHTML(builder.toSafeHtml());
//div contains the following HTML: "one, <b>two</b>, three <escape-me>, "
Note that the appendHtmlConstant method expects a complete tag. So if you want to add attributes to the tag whose values change during runtime, it won't work. For example, this won't work (it throws an IllegalArgumentException):
String url = //...
SafeHtmlBuilder builder = new SafeHtmlBuilder();
builder.appendHtmlConstant("<a href=\""); //throws IllegalArgumentException
builder.appendEscaped(url);
builder.appendHtmlConstant("\">link</a>");
Either create a label and set it to bold:
Label label = new Label("text");
label.getElement().getStyle().setFontWeight(FontWeight.BOLD);
Or you can create a SpanElement and set its innerHtml.
Here's example to put a space in a widget, e.g. Label:
public void setTextNbsp( final Widget w ) {
w.getElement().setInnerHTML( " " );
}
Other HTML entities could be used as well. Take care with this not to open security holes. SafeHtml, etc. might need consideration if doing something more dynamic.
I think you should use SpanElement where you don't want the html to be escaped and label where you want then to be escaped and put them on a vertical panel so that they would appear as a single line of text.