Showing posts with label TestNG. Show all posts
Showing posts with label TestNG. Show all posts

Friday, 3 January 2014

Running Selenium Script from console/command prompt using .class instead of .java file - TestNG

Purpose: Running Selenium Script from console/command prompt using .class instead of .java file with out JDK - TestNG

Some times we need to run our automation script at client box to demonstrate our ability to automate but we don't want to share our code with out JDK ( required - JRE). We can achieve this by following below steps. With this we can run the script from .class instead of .java file.

We can achieve this using both JUnit and TestNG. In this example we are working with TestNG

Here my example is GoogleConsole.java

Step 1: Prepare required scrip using any IDE and compile
Step 2: Open command prompt in another system and check whether JRE is available
Step 3: Prepare testing.xml file as follows

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Google">
  <test name="GoogleConsloe">
    <classes>
      <class name="GoogleConsole"/>
    </classes>
  </test>
</suite>

Step 3: Copy selenium standalone server, TestNG.jar, .class and testing.xml files to a folder(D:\Google\)
Step 4: Open command prompt and change to D:\Google\
Step 5: Set path using the following command
set classpath=%classpath%;D:\Google\selenium-server-standalone-2.39.0.jar;D:\Google\testng-6.8.jar;.;

*Note: update the paths of the .jar files and .class file according to your path.

Step 6: Run the class file using the following command

java org.testng.TestNG java org.testng.TestNG testng.xml

Now you can see the bowser will open and perform the required operation without code and eclipse
 

Sunday, 17 November 2013

Prioritizing Selenium Test cases using TestNG annotations

Purpose: Prioritizing Selenium Test cases using TestNG annotations

Some times we may need to priorities our test cases based on some requirements. Following code will help you all.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class PriorityParam {
 public WebDriver driver;
 public String baseURL;

 @BeforeMethod
 public void setup()
 {
  driver = new FirefoxDriver();
  driver.manage().window().maximize();
  baseURL = "http://mail.google.com";
 }

 @AfterMethod
 public void tearDown()
 {
  driver.quit();
 }

 @Test(priority=3)
 public void testPriorityParam() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 3");
 }

 @Test(priority=2)
 public void testPriorityParam1() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 2");
 }

 @Test(priority=4)
 public void testPriorityParam2() throws InterruptedException
 {
  driver.get(baseURL);
  Thread.sleep(3000);
  System.out.println("Priority 4");
 }

Tuesday, 17 September 2013

Executing Sequence of Selenium Test Cases in a Cusomized Sequence

Purpose: Executing Sequence of Selenium WebDriver Test Cases in a Cusomized Sequence

While automating any application we come across a situation where we need to execute multiple test cases in a particular sequence. But when it comes to execute the test cases they will execute randomly. In order to execute the test cases in our pre requisite order we need to pass a parameter to @Test method priority and a value(Lower priorities will be scheduled first).

Following code will be useful to accomplish the requirement:
 
 import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class OrderOfTestCase {

 public String baseURL = "http://www.google.com";
 public WebDriver driver = new FirefoxDriver();

 @BeforeTest
 public void launchBrowser()
 {
  driver.get(baseURL);
 }

 @AfterTest
 public void closeBrowser()
 {
  driver.quit();
 }

 @BeforeMethod
 public void getTitle()
 {
  System.out.println(driver.getTitle());
 }
 @AfterMethod
 public void goToPreviousPage()
 {
  driver.navigate().back();
 }

 @Test(priority = 0)
 public void images() throws InterruptedException
 {
  driver.findElement(By.linkText("Images")).click();
  Thread.sleep(3000);
  System.out.println(driver.getTitle());
 }

 @Test(priority = 1)
 public void maps() throws InterruptedException
 {
  driver.findElement(By.linkText("Maps")).click();
  Thread.sleep(3000);
  System.out.println(driver.getTitle());
 }

 @Test(priority = 2)
 public void mail() throws InterruptedException
 {
  driver.findElement(By.linkText("Gmail")).click();
  Thread.sleep(3000);
  System.out.println(driver.getTitle());
 }
}

Monday, 8 April 2013

Writing simple Data Driven Framework scripts using TestNG

Purpose: Writing simple Selenium Scripts by implementing Data Driven Framework using TestNG

Data Driven Framework scripts means executing a set of steps with multiple sets of data. Selenium is not have any default way of implementing but by using TestNG support we can implement it successfully.

Step 1: JXL API Jar - We can download it form the following URL
http://sourceforge.net/projects/jxls/files/ and download latest version

Step 2: Prepare Data Source - Open an excel and prepare data source as shown below


In this example we are discussing reading inputs from an excel sheet and writing the results in reports sheet.

Step 3: Following example will demonstrate how to access from an excel sheet and how to write the results into an excel sheet.

package com.google.excel;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GMail{
 private WebDriver driver;
 private String baseURL;
 private FileInputStream fi;
 private Sheet s;
 private Workbook wb;

 @BeforeClass
 public void setup() throws BiffException, IOException
 {
  driver = new FirefoxDriver();
  baseURL = "http://www.gmail.com";
  fi = new FileInputStream("TestDate\\Book1.xls");
  wb = Workbook.getWorkbook(fi);
  s = wb.getSheet(0);
 }

 @AfterClass
 public void teardown()
 {
  driver.quit();
 }
 @Test
 public void testGmail() throws InterruptedException
 {
  for(int row=0; row <s.getRows();row++)
  {
    String username = s.getCell(0, row).getContents();
    System.out.println("Username "+username);
    driver.get(baseURL);
    driver.findElement(By.name("Email")).sendKeys(username);
    String password= s.getCell(1, row).getContents();
    System.out.println("Password "+password);
    driver.findElement(By.name("Passwd")).sendKeys(password);
    Thread.sleep(10000);
    Thread.sleep(30000);
    writeExcel(0, 0, "Passes");
  }
 }

public void writeExcel(int a, int b, String text) {
    try {
        File excelFile = new File("TestDate\\Results.xls");
        WritableWorkbook book;
        WritableSheet sheet;
        Workbook existingBook = null;
        if (!excelFile.exists()) {
            book = Workbook.createWorkbook(excelFile);
            sheet = book.createSheet("TESTRESULTS", 0);
        } else {
            existingBook = Workbook.getWorkbook(excelFile);
            book = Workbook.createWorkbook(excelFile, existingBook);
            sheet = book.getSheet("TESTRESULTS");
        }
        Label i = new Label(a, b, text);
        sheet.addCell(i);
        book.write();
        book.close();
        if (existingBook != null)
            existingBook.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}



















 

Capturing all Links in a page, filtering and visiting filtered pages.

Purpose: Capturing all Links in a page, filtering and visiting filtered pages.


In general while testing any application we are facing a situation where we need to visit specific link available on the Web Page. The Following script help floks how to handle it.

In the following example, I am going to visit 'www.google.com', captures all the links available on the page, filter the required link and visit that link page.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

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

  WebDriver driver = new FirefoxDriver();
  driver.get("http://www.tata.com/");
  driver.manage().window().maximize();
//  Extract all links from the webpage using selenium webdriver
  List<WebElement> all_links_webpage = driver.findElements(By.tagName("a"));

//  Print total no of links on the webpage
  System.out.println("Print total no of links on the webpage---------------------------------------------");
  System.out.println(all_links_webpage.size());

//  Filter the Link and visit that link
  System.out.println("Print Links------------------------------------------------------------------------");
  for(int i=0;i<all_links_webpage.size();i++)
  {
   Thread.sleep(1000);
   if(all_links_webpage.get(i).getText().toLowerCase().contains("contact"))
   {
    System.out.println("Search Resutl found");
   String link = all_links_webpage.get(i).getAttribute("href");
   driver.navigate().to(link);
   Thread.sleep(3000);
   break;
   }
  }
 driver.quit();
 }
}