My Thoughts: July 2013

Saturday, July 13

Verifying ToolTip information by enabling javascript in browser


These days most of websites are using the ToolTip to provide information to end user.

ToolTip(also known as ScrrenTip) will be visible to the user whenever he/she mouseovers on specific object and it just displays information about the object(button,textbox,link,image etc..).

These tooltips works only when javascript is enabled.

So automating tooltip involves :

  •  Verifying that tooltip presents when we mouse over on specific object 
  •  Verifying that the text that present in the tooltip is correct.
In this site  if you mouse over on "Download" link we will see a tooltip.

ToolTip Screenshot


Here is the program which will check the existence of tooltip and also the text present in the tooltip. In this program I have used HtmlDriver because it provides inbuilt methods for enabling and disabling of javascript.

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class HTMLdriver {
 public static void main(String args[]){
 
 HtmlUnitDriver driver = new HtmlUnitDriver();
 driver.setJavascriptEnabled(true);
 driver.get("http://bassistance.de/jquery-plugins/jquery-plugin-tooltip/");
 WebElement element=driver.findElement(By.linkText("Download"));
 Actions builder = new Actions(driver);  // Configure the Action  
 Action mouseOver =builder.moveToElement(element).build(); // Get the action  
 mouseOver.perform(); // Execute the Action 
   if(driver.findElement(By.id("tooltip")).isDisplayed()){
    System.out.println("Tooltip is coming up fine");
    System.out.println("Here is the tool tip text : "+driver.findElement(By.id("tooltip")).getText());
   }else{
    System.out.println("There is no Tool tip text");
   }

 }
}

If you want to check the correctness of the program then disable javascript and run the program.In this case script will fail as tooltip failed to appear when we mouseover on Download link.
driver.setJavascriptEnabled(false);