Monday 20 January 2014

Some Useful methods in Selenium WebDriver



isElementPresent/IsText Present :- used to verify if an element is present on the page.

way 1
        boolean isElementPresent(WebDriver driver, By by){
                 try{
                                driver.findElement(by);
                                return true;
                 }catch(NoSuchElementFoundException e){
                                // capture screenshot 
                                return false;
                   }
                 }

way 2
if(driver.findElements(By. any locator stratergy).size()>0){
   return true;
 }else{
   return false;
}
}
 Find if the expected text is present on the page

driver.PageSource.Contains("TEXT that you expect on the page")

Working with Alerts
1.       Fetch the text message.

public static String getAlertMessage(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.accept();
} catch (Exception e) {
message = null;
}
return message;
}
2.       click on cancel/no option on the alert
public static String cancelAlertBox(final WebDriver driver) {
String message = null;
try {
Alert alert = driver.switchTo().alert();
message = alert.getText();
alert.dismiss();
} catch (Exception e) {
message = null;
}
return message;
}
Reading ToolTip
public static String tooltipText(WebDriver driver, By locator){
String tooltip =     driver.findElement(locator).getAttribute("title");
                return tooltip;
}
Selecting Radio Button 

public static void selectRadioButton(WebDriver driver, By locator, String value){ 
List select = driver.findElements(locator);

for (WebElement element : select){

if (element.getAttribute("value").equalsIgnoreCase(value)){

element.click();

}

}
 Selecting CheckBox 

public static void selectCheckboxes(WebDriver driver, By locator,String value)
{
                List abc = driver.findElements(locator);
                List list = new ArrayListArrays.asList(value.split(",")));
                for (String check : list){
                                for (WebElement chk : abc){
                                                if(chk.getAttribute("value").equalsIgnoreCase(check)){
                                                                chk.click();
                                                }
                                }
                }
}

Selecting Dropdown value
public static void selectDropdown(WebDriver driver, By locator, String value){
           new Select (driver.findElement(locator)).selectByVisibleText(value);
 }
public static void selectDropdown(WebDriver driver, By locator, int  index){
           new Select (driver.findElement(locator)).selectByIndex(index);
 }

Uploading file
public static void uploadFile(WebDriver driver, By locator, String path){
driver.findElement(locator).sendKeys(path);
}
Downloading file

1.       Here we will click on a link and will download the file with a predefined name at some specified location.
public static void downloadFile(String href, String fileName) throws Exception{
   URL url = null;
   URLConnection con = null;
   int i;
   url = new URL(href);
   con = url.openConnection();
// Here we are specifying the location where we really want to save the file.
   File file = new File(".//OutputData//" + fileName);
   BufferedInputStream bis = new BufferedInputStream(con.getInputStream());
   BufferedOutputStream bos = new BufferedOutputStream(
   new FileOutputStream(file));
   while ((i = bis.read()) != -1) {
                   bos.write(i);
   }
   bos.flush();
   bis.close();
}