Sunday, December 28, 2014


In this tutorial I will explain you how to download txt, pdf, csv, excel and doc file using firefox profile. You can write a selenium script to download file, if you know the MIME type of the file that you wanted to download.





Download File Using Selenium WebDriver

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

public class Training {
 static WebDriver driver;

 public static void main(String[] args) throws InterruptedException {

  FirefoxProfile firefoxprofile = new FirefoxProfile();

  firefoxprofile.setPreference("browser.download.dir", "D:\\download");
  firefoxprofile.setPreference("browser.download.folderList", 2);

  firefoxprofile
    .setPreference(
      "browser.helperApps.neverAsk.saveToDisk",
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"
        + "application/pdf;"
        + "application/vnd.openxmlformats-officedocument.wordprocessingml.document;"
        + "text/plain;" + "text/csv");
  firefoxprofile.setPreference(
    "browser.download.manager.showWhenStarting", false);
  firefoxprofile.setPreference("pdfjs.disabled", true);

  driver = new FirefoxDriver(firefoxprofile);

  driver.get("reditblog.blogspot.in/2014/12/how-to-download-file-using-selenium.html");

  driver.findElement(By.name("text")).click();
  Thread.sleep(5000);
  driver.findElement(By.name("pdf")).click();
  Thread.sleep(5000);

  driver.findElement(By.name("CSV")).click();
  Thread.sleep(5000);

  driver.findElement(By.name("Excel File")).click();
  Thread.sleep(5000);

  driver.findElement(By.name("Doc")).click();
  Thread.sleep(5000);

  driver.quit();

 }
}






In this tutorial I will explain how to click on WebElement using JavaScriptExecutor. Sometimes click() does not work then you can use JavaScriptExecutor to click button or WebElement.





Click WebElement Using JavaScriptExecutor

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Training {
 static WebDriver driver;

 public static void main(String[] args) throws InterruptedException {

  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  driver.get("http:// www.google .com/");
  driver.findElement(By.name("q")).sendKeys("http://reditblog.blogspot.in");

  Thread.sleep(5000);
  WebElement element1 = driver.findElement(By.name("btnK"));
  JavascriptExecutor executor1 = (JavascriptExecutor) driver;
  executor1.executeScript("arguments[0].click();", element1);
  Thread.sleep(5000);
  WebElement element2 = driver.findElement(By.linkText("Reditblog"));
  JavascriptExecutor executor2 = (JavascriptExecutor) driver;
  executor2.executeScript("arguments[0].click();", element2);
  Thread.sleep(5000);
  driver.quit();
 }

}





Tuesday, December 16, 2014


If the WebDriver does not get web element then the WebDriver will wait for specified time and WebDriver will not try to search the element during the specified time. Once the specified time is over, WebDriver will start searching the web element only one time before throwing the exception.





Selenium Implicit Wait Example


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {
 static WebDriver driver;

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http://www. w3schools. com/js/tryit. asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();
 }

}





If a particular web element takes more than a minute to load then you may want to wait for the element to load properly and then go to the next line of code. In selenium, it is possible with Explicit Wait.





Selenium Explicit Wait Example


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Training {
 static WebDriver driver;

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www. w3schools. com/js/tryit. asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  WebElement element1 = (new WebDriverWait(driver, 10))
    .until(ExpectedConditions.elementToBeClickable(By
      .tagName("button")));
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();
 }

}





Monday, December 15, 2014


This article shows you how to take screenshot using Selenium Webdriver. Screen shot may help you to identify and debug the problem.





Take Screenshot Using Selenium Webdriver

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {
 static WebDriver driver;

 public static void main(String[] args) {

  driver = new FirefoxDriver();
  driver.get("http://www.google.com");
  driver.manage().window().maximize();
  try {
   driver.findElement(
     By.xpath("//*[@id='Blog1]"))
     .sendKeys("test");

  } catch (Exception e) {
   System.out.println("Please look at D:\\screenshot.png for screenshot!");
   try {
    getscreenshot();
   } catch (Exception e1) {
    System.out.println(e1);
   }
  }
 }

 public static void getscreenshot() throws Exception {
  File scrFile = ((TakesScreenshot) driver)
    .getScreenshotAs(OutputType.FILE);

  FileUtils.copyFile(scrFile, new File("D:\\screenshot.png"));
 }

}







In this tutorial, I will tell you how to write text in the forms. Selenium webdriver provides a method called sedKeys() which help us to write text in the forms automatically.





import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class Training {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.get("https:// login.yahoo. com/?.src=ym&.intl=us&.lang=en-US&.done=https%3a// mail.yahoo. com");
  driver.manage().window().maximize();
  driver.findElement(By.id("login-username")).sendKeys("some text");
  driver.findElement(By.id("login-passwd")).sendKeys("some password");

 }

}






Suppose we have a requirement to drag a webelement from one location and drop in another using a selenium script. In selenium webdriver, we can perform drag and drop or mouse operations by using Actions class.





import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class Training {

 public static void main(String[] args) {

  WebDriver driver = new FirefoxDriver();
  driver.get("http: //jqueryui. com/droppable/");
  driver.manage().window().maximize();
  driver.switchTo().frame(0);
  driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);
  WebElement dragElement = driver.findElement(By.id("draggable"));
  WebElement dropElement = driver.findElement(By.id("droppable"));

  Actions builder = new Actions(driver); 
  Action dragAndDrop = builder.clickAndHold(dragElement)
    .moveToElement(dropElement).release(dropElement).build();
  dragAndDrop.perform();

 }

}