Search This Blog

Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Sunday, 1 November 2015

TestNg Data Provider Annotation


While executing test cases with a multiple data sets, it is always ideal to parameterize the test cases.

The parameterization of the test data is always a smart task to be taken into consideration based on number of parameters like
 - Number of Iterations
 - Frequency of data is updated
 - Maintenance of test data
 - and many more...


Using TestNg, we can achieve this in multiple ways like
 - Passing as an input from the XML file
 - Using the DataProvider annotation

Using XML configuration, we can configure the variable and data in the xml as shown below:
<parameter name="username"  value="Martin">

And in the Test, the variable can be used like this :


@Parameters({ "username" })
@Test
public void testSingleString(String firstName) {
  System.out.println("Invoked testString " + firstName);
  assert "Martin".equals(firstName);
}



Passing data to the Test using @DataProvider annotation in TestNg
Code snippet to create the test data

@DataProvider(name = "sample")
public static Object[][] getData() {
 // Any code to fetch data.
 Object[][] a = { 
   { 1, "name" }, 
   { 2, "Place" },
   { 3, "email" },
   { 4, "skype" }
   };
 return a;
}

And the test looks like this:

 @Test(dataProvider = "sample")
 public static void runTest(int number, String s) {
  System.out.println("The value of a is : " + number + " and the string is : " + s);

 }
The output will be shown as :



Friday, 23 October 2015

Capture full screen screenshot using Selenium + Java

It is always a good idea to have a screenshot for investigating what was the state of the application at the time of exception/halt of execution.

Although Selenium WebDriver provides default functions to take the screenshots of the browser at the exception. See example here

The alternate is to capture the full screenshot (Screenshot covering the full desktop) at the time of exception or error occurred on the screen.

Note: When the test execution is failing because of "Model Pop Up" on the browser, default function of the WebDriver will not be able to capture the Model Pop Up in the screenshot.

Git Hub Link



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

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class FullScreenShot {
    final static String fileLocation = "D:\\ScreenShot\\";


    public static void main(String[] args) {
        try {
            WebDriver driver = new FirefoxDriver();
            driver.get("http://www.testautomation4all.com");
            driver.findElement(By.id("some-non-existing-element")).click();
        } catch (Exception e) {
            captureEnirePageScreenshot("Unexpected error");
        }

    }

    public static void captureEnirePageScreenshot(String screenShotName) {
        try {
            // Capture the dimensions of the screen
            Dimension d = Toolkit.getDefaultToolkit().getScreenSize();

            // Convert the dimensions into rectangle object type
            Rectangle rec = new Rectangle(d);

            // Take the screenshot using the Robot instance
            Robot robot = new Robot();

            BufferedImage myImage = robot.createScreenCapture(rec);

            File img = new File(fileLocation + screenShotName + ".png");

            ImageIO.write(myImage, "png", img);

            System.out.println("Done");

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}