Executing Headless Browser Test In Different Browsers And Version In Selenium

We have learnt how to execute JavaScript In HtmlUnit driver test In previous post. I am also suggesting you to read how to create and run first HtmlUnit driver test In THIS POST where I have described advantages of HtmlUnit driver against all other driver Instances. One of them Is we can
choose the browser and Its version to run selenium WebDriver test.

HtmlUnit driver allows you to run your tests In different browser but In hidden mode. That means If your selected browser to run your test Is Google chrome then test will be executed In Google chrome browser but you will not see Google chrome browser on your screen during test execution. You can choose any of the bellow given browsers and versions to run your WebDriver tests In It using HtmlUnit driver.
  • FIREFOX_17
  • FIREFOX_24
Syntax to select browser to run test using HtmlUnit driver Is as bellow.
//Initializing HtmlUnitDriver to run test with INTERNET_EXPLORER_11 browser.
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_11);

Create and run bellow given example In your eclipse to see execution practically.
package Testing_Pack;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.gargoylesoftware.htmlunit.BrowserVersion;

public class htmlDriver {
 HtmlUnitDriver driver;
 String pagetitle;

 public void setup() throws Exception {
  //Initializing HtmlUnitDriver to run test with INTERNET_EXPLORER_11 browser.
  HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.INTERNET_EXPLORER_11);
  //Enabling JavaScript of HtmlUnitDriver.
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  //To hide warnings logs from execution console.
  Logger logger = Logger.getLogger("");
  //Opening URL In HtmlUnitDriver.

 public void tearDown() throws Exception {
  //Closing HtmlUnitDriver.

 public void googleSearch() {  
  //Get current page title using javascript executor.
  JavascriptExecutor javascript = (JavascriptExecutor) driver;
  String pagetitle=(String)javascript.executeScript("return document.title");  
  System.out.println("My Page Title Is  : "+pagetitle);
  // To locate table.
  WebElement mytable = driver.findElement(By.xpath(".//*[@id='post-body-8228718889842861683']/div[1]/table/tbody"));
  // To locate rows of table.
  List<WebElement> rows_table = mytable.findElements(By.tagName("tr"));
  // To calculate no of rows In table.
  int rows_count = rows_table.size();

  // Loop will execute till the last row of table.
  for (int row = 0; row < rows_count; row++) {
   // To locate columns(cells) of that specific row.
   List<WebElement> Columns_row = rows_table.get(row).findElements(By.tagName("td"));
   // To calculate no of columns(cells) In that specific row.
   int columns_count = Columns_row.size();
   System.out.println("Number of cells In Row " + row + " are "+ columns_count);

   // Loop will execute till the last cell of that specific row.
   for (int column = 0; column < columns_count; column++) {
    // To retrieve text from that specific cell.
    String celtext = Columns_row.get(column).getText();
    System.out.println("Cell Value Of row number " + row+ " and column number " + column + " Is " + celtext);

Above example will be executed In IE11 browser virtually to extract data from web table. It will print extracted data In console. Next post will show you how to configure PhantomJS GhostDriver with eclipse to run selenium WebDriver test case In It..

No comments:

Post a Comment