Sunday, 8 January 2017

Extent Reports:

How to add lightbox to your extent reports?

Lightbox in Javascript: A typical lightbox image display. Lightbox is a JavaScript library that displays images and videos by filling the screen, and dimming out the rest of the web page. (Source: Wikipedia0

Issue while using Extent reports:

For failed cases screenshot will be captured and whenever user opens report.html for failed cases we can see images (local machine). Now if user shares the report to other person, if other person tries to access the images for failed cases it doesnt shows up :(

So here is one of the solutions which i found is implementing lightbox using javasript
 For Extent report initialization just use below code

public static void ExtentReportsInitialize(){
    extent.init(reportLocation + "Report.html",      true,DisplayOrder.BY_OLDEST_TO_LATEST,GridType.MASONRY);
    extent.config().insertJS("$(document).ready(function(){"
  + " $('img.report-img').each(function(){"
  + "$(this).attr('data-featherlight','');"
  + " $(this).attr('src',$(this).attr('src').replace('file:///','')); "
  + "$(this).attr('href',$(this).attr('src'));"
  + " $(this).attr('data-featherlight','image');"
  + " })});");
   extent.config().addCustomStyles(css);
}

public static String createScreenshot(WebDriver driver) {
              // generate screenshot as a file object
        return "data:image/png;base64,"+((TakesScreenshot)driver).getScreenshotAs(OutputType.BASE64);
     
    }


With this code we can take the screenshot for all the failed cases and embed the screenshot in the report itself and click on image from the report will open the light box. With this no need to always maintain ‘Screenshot’  folder in separate. Images will be shown even if we share just ‘report.html’ file to any user (As images are embedded within the report).

if you have any question please comment below.

Thanks!

Sunday, 23 October 2016

Step 1: Download MicrosoftWebDriver.exe from "https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ "

Below is the sample code to launch edge browser using Microsoft driver

package com.Sample;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.testng.annotations.Test;
public class EdgeBrowserTest {
String driverPath = "Path Microsoft edge driver"; // D:\\microsoftdriver\\
public WebDriver driver;
@Test(priority=0)
public void launchEdgeBrowser() {
System.out.println("===== Microsoft Edge browser =======");
System.setProperty("webdriver.edge.driver", driverPath+"MicrosoftWebDriver.exe");
driver = new EdgeDriver();
}
@Test(priority=1)
public void openEdgeBrowser() {
driver.navigate().to("http://seleniummansion.blogspot.com/");
}
@Test(priority=2)
public void closeDriver() {
if(driver!=null) {
driver.close();
}
} }
Below is the sample code to launch web browser in Firefox using selenium 3.0

package com.sample;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
public class SampleTest {
String driverPath = "<path to gecko driver executable>"; // D:\\geckofolder\\
public WebDriver driver;
@Test
public void launchBrowser() {
System.out.println("===== launching firefox browser ==== ");
System.setProperty("webdriver.gecko.driver", driverPath+"geckodriver.exe");
driver = new FirefoxDriver();
}
@Test
public void openApplication() {
driver.navigate().to("http://seleniummansion.blogspot.com");
}
@Test
public void closeDriver() {
if(driver!=null) {
driver.close();
driver.quit();
}
}
}
Step 1: Download the geckodriver from ' https://github.com/mozilla/geckodriver/releases '
Step 2: Now you need to specify the system property with the path

                     System.setProperty("webdriver.gecko.driver","path of geckodriver.exe");
                     WebDriver driver = new FirefoxDriver();
Your done... 

Friday, 24 April 2015

ExtentReports (by Anshoo Arora) is a HTML reporting library for Selenium WebDriver for Java and creates easy to use, attractive reports. It shows test and step summary, test steps and status in a toggle view for quick analysis.
View ExtentReports sample here.

Index

What’s new in version 1.4

The following have been added to the the latest version:
Versions 1.2 and earlier are no longer supported. If you are using an earlier version, please consider upgrading.
  1. *New grid type available: MASONRY. View sample. To use this grid system, simply add GridType.MASONRY to init():
    extent.init(filePath, replaceExisting, gridType);
    extent.init(filePath, replaceExisting, displayOrder, gridType);
    
  2. You can now add labels to your log events:
    extent.log(LogStatus.INFO, "Example: <span class='label success'>success</span>");
    extent.log(LogStatus.INFO, "Example: <span class='label failure'>fail</span>");
    extent.log(LogStatus.INFO, "Example: <span class='label info'>info</span>");
    extent.log(LogStatus.INFO, "Example: <span class='label warn'>warning</span>");
  3. Insert code-blocks using the <pre> tag:
  4. You can now change title for your charts using:
    extent.config()
        .chartTitle(Chart.TEST, "Your New Title")
        .chartTitle(Chart.TEST_SET, "Your New Title");
    
  5. It is possible to change the size of images by specifying a size in percentage:
    extent.config().setImageSize("30%");
    
    Default size is 50%.
  6. It is now possible to hide the caller class by this simple setting:
    extent.config().displayCallerClass(false);
    

Owner / Developer

This library is developed and maintained by:
  • Anshoo Arora

Contributors

  • Bas Dijkstra

Download

Download the jar from this link.

Maven / pom.xml

Simply add the following to your pom.xml:
<dependency>
  <groupId>com.relevantcodes</groupId>
  <artifactId>extentreports</artifactId>
  <version>1.4</version>
</dependency>
You can view Extent’s pom.xml here.

Basic Usage

ExtentReports is very simple to use. Below is some version-specific basic usage to get you started using this library.
You can also view a Selenium test that Madhu has created with usage at his blog.
// version 1.3+
import com.relevantcodes.extentreports.*;
 
public class Main {
    // *REQUIRED
    // put this in every class 
    // * Main.class will become TheClassName.class
    static final ExtentReports extent = ExtentReports.get(Main.class); 
 
    public static void main(String[] args) {
        extent.init("C:\\Extent.html", true);
 
        // *REQUIRED
        // startTest( testName )
        //    creates a toggle for the given test, adds all log events under it    
        extent.startTest("Main");
 
        // log(logStatus, stepName, details)
        extent.log(LogStatus.PASS, "StepName", "PASS Details"); 
 
        // log(LogStatus, details)
        extent.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)");
 
        // report with snapshot
        // log(logStatus, stepName, details, screenCapturePath)
        extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");
 
        // only report a snapshot, without status
        extent.attachScreenshot("pathtoImg.png", "This step attaches a screenshot without status.");
 
        // start other tests..
    }
}
It is not required to call endTest() version 1.0 onwards. However, calling it at the end of run session is still recommended.
Its not required to call the init method before each test. Its recommended to use “init” method only once, at the beginning of the run session to set the reporting path.
By default, the oldest test appears at the top. To change this behavior, or to allow the latest test to appear at the top, use the code below. Report started in one order will remain in that order unless the report is over-written or a new report is created.
extent.init(filePath, true, DisplayOrder.BY_LATEST_TO_OLDEST);

Initializing Report

To initialize, simple call the init method:
init(filePath, replaceExisting)
init(filePath, overwriteExisting, DisplayOrder)
init(filePath, overwriteExisting, GridType)
init(filePath, overwriteExisting, DisplayOrder, GridType)

where:
    filePath:  path of the file, in .htm or .html format
    overwriteExisting:  (true|false) 
        true:  the file will be replaced with brand new markup, 
                and all existing data will be lost. Use this option to create a 
                brand new report
        false:  existing data will remain, new tests will be appended to the existing 
                report
    DisplayOrder:  this determines the order in which your tests will be displayed
        BY_OLDEST_TO_LATEST (default) - oldest test at the top, newest at the end
        BY_LATEST_TO_OLDEST - newest test at the top, oldest at the end
    GridType:  determines the type of grid to be used
        STANDARD (default) - standard grid with 1 test per row
        MASONRY - creates a masonry style grid with 2 tests per row

Creating step logs

It is possible to log your execution steps in 3 ways:
// log(logStatus, stepName, details)
extent.log(LogStatus.PASS, "StepName", "PASS Details"); 

// log(LogStatus, details) (VERSION 1.2+ only)
extent.log(LogStatus.INFO, "This step shows usage of log(logStatus, details)");

// log(logStatus, stepName, details, screenCapturePath)
extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");

Appending to an existing report

Easy! Simply use overwriteExisting = false setting to initialize and your tests will be appended to your existing report:
extent.init("path-to-file", false);

Adding a Test Description

You can insert a description for your test using:
// startTest(String testName, String testDescription)
extent.startTest("Test - With Description", "This description will show up under Test.");

Inserting a Snapshot

You can insert snapshot as a link or directly into the report using:
// log(LogStatus logStatus, String stepName, String details, String screenCapturePath)
extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");

Attaching a Snapshot without Status

You can attach a snapshot to the report (v1.2+) without having to specify status using:
// void attachScreenshot(path, details)
extent.attachScreenshot("pathToImg.png", "Attach screenshot without status.");
Attaching a screen-shot is not counted as a step and will not show under Execution Info either.

Using Relative paths for snapshots

Relative paths starting with “.” and “/” characters are supported:
// ./pathToImg.png
extent.log(LogStatus.INFO, "Image", "Image example:", "./pathToImg.png");

// /pathToImg.png
extent.log(LogStatus.INFO, "Image", "Image example:", "/pathToImg.png");
If you using an absolute path, “file:///” will be automatically be appended for the image to load.

Inserting a Label

You can add labels to your log events using <span class='label labelName'>label message</span>.

There are 4 labels available:
  • success
  • failure
  • info
  • warn
extent.log(LogStatus.INFO, "Example: <span class='label success'>success</span>");
extent.log(LogStatus.INFO, "Example: <span class='label failure'>fail</span>");
extent.log(LogStatus.INFO, "Example: <span class='label info'>info</span>");
extent.log(LogStatus.INFO, "Example: <span class='label warn'>warning</span>");
You can use HTML anywhere in this report. Simply do this to insert a link:
extent.log(LogStatus.PASS, "Link", "Usage: <a href='http://relevantcodes.com'>link</a>.");

Inserting Custom HTML

Just like above, you can insert any HTML tag to your report:
extent.log(LogStatus.INFO, "HTML", "Usage: <span style='font-weight:bold;'>BOLD</span>");

Customization

You can customize the report as you want. Changes can be easily made to the overall CSS by bringing your own custom css, changes to the icons can be made by picking your own from font-awesome etc. Below is some basic usage to demonstrate this library’s customization.

Using custom CSS

You have options to use custom CSS directly (version 1.0+) in the document or bring in your own stylesheet.
// custom styles
String style = "p{font-size:20px;}" +
            ".test{background-color:#000 !important;color:#fff !important;}";
 
extent.config().addCustomStyles(style);
// custom stylesheet
extent.config().addCustomStylesheet("C:\\css.css");

Changing Report Title

It is possible to change the title of the report file (v1.2+) using:
extent.config().documentTitle("MyTestReport");

Themes*

ExtentReports can be heavily themed/modified to suit a layout as per your needs. The example below shows a Cucumber like color-scheme:
String css = "#topbar { background-color: #8bb1ec; }" +
        ".topbar-items-right span { color: white; }" +
        ".menu span { color: darkgreen; }" +
        ".menu-item-selected span { border-bottom: 1px solid green; }" +
        "#dashboard { background-color: transparent; }" +
        ".test { border: 1px solid lightseagreen; }" + 
        ".description { background-color: transparent; border-left: 2px solid orange; padding: 2px 15px;}" + 
        ".name { color: darkgreen; }" + 
        ".extent-table { border: 1px solid #bbb; }" + 
        ".extent-table th { background: none repeat scroll 0 0 olivedrab; color: #fff; }" + 
        ".extent-table td { border-bottom: 1px solid #bbb; }";

extent.config().addCustomStyles(css);
The above will result in the following output. Its a small change, but quite different from the original.

Using custom JS

Just like having the ability to change document styles, you can also add your own custom scripts.
extent.config().insertJS("$('.test').click(function(){ alert('test clicked'); });");
Its possible to add/remove the Extent footer using the following configuration:
// remove the footer
extent.config().useExtentFooter(false);
 
// use the footer
extent.config().useExtentFooter(true);

Change status icons

Not a lot of people would do this, but you can choose to use your own icons for log status (PASS, FAIL, WARNING etc.) by choosing one of the icons from Fontawesome website: http://fortawesome.github.io/Font-Awesome/icons/.
extent.config().statusIcon(LogStatus.PASS, "check-circle");

Changing the top-level summary

You can remove or add your own summary by using the following config:
extent.config().reportHeadline("My custom report summary.");

Change chart title

It is possible (v1.4+) to customize the chart-title text by using the following config:
extent.config()
    .chartTitleText(Chart.TEST, "Your New Title")
    .chartTitleText(Chart.TEST_SET, "Your New Title");

Change Screenshot size

The default screenshot size attached to the report is 50%. It is possible to change screenshot size (v1.4+) by specifying a percentage:
extent.config().setImageSize("30%");

CallerClass display setting

It is now possible to hide the caller class by this simple setting:
extent.config().displayCallerClass(false);

Feature Requests

Think of any new features? Would you like to see this library in another language? Please drop a comment here and let me know.
Click here to see feature requests for upcoming version 1.2

Important version info

The below are version specific changes that you will find helpful.

Version 1.4

There are no usage changes in this version. A few new methods added, see the whats new section for more info.

Version 1.3

There are no usage changes in this version.

Version 1.2

There are no usage changes in this version.

Version 1.1

Includes all the features added in previous versions.
  • Configuration() is now deprecated, use config() instead:
    // 1.0 and earlier
    extent.configuration().documentHead().addCustomStyles(css);
    extent.configuration().footer().useExtentFooter(false);
    extent.configuration().header().introSummary("This is my new summary");;
    
    // release 1.1
    extent.config()
        .addCustomStyles(css)
        .useExtentFooter(false)
        .reportHeadline("This is my new summary");
    
  • introSummary(String) has been renamed to reportHeadline(String)
    // 1.0 and earlier
    extent.configuration().header().introSummary("This is my new summary");;
    
    // release 1.1
    extent.config().reportHeadline("This is my new summary");
    

Version 1.0

Includes all the features added in previous versions.
  • It is no longer required to call endTest after each test. Its still recommend to use it at the very end of run-session, but not required. See below example:
    extent.startTest("Test 1");
    extent.log(LogStatus.PASS, "Step", "Details");
     
    // no need to call endTest here, you can now just start the next test
    // if you are already using endTest, you don't need to remove it or change your code
     
    extent.startTest("Test 2");
    extent.log(LogStatus.INFO, "Step", "Details");
     
    // .. more tests
  • scripts().insertJS(script) introduced in this version
  • documentHead().addCustomStyles(styles) introduced in this version
  • footer().removeExtentFooter() is deprecated in this version, utilize useExtentFooter(false) instead
  • footer().addExtentFooter() is deprecated in this version, utilize useExtentFooter(true) instead

Version 0.94+

  • Ability to add snapshots directly to the report using log method:
    extent.log(LogStatus.INFO, "Image", "Image example:", "C:\\img.png");
  • Ability to add description for the test when using startTest:
    extent.startTest("Test - With Description", "This description will show up under Test.");
     

    For More information visit: http://relevantcodes.com/extentreports-for-selenium/

Saturday, 18 April 2015

What is Selenium Grid?


Selenium Grid is a part of the Selenium Suite that specializes on running multiple tests across different browsers, operating systems, and machines in parallel.
Selenium Grid has 2 versions - the older Grid 1 and the newer Grid 2. We will only focus on Grid 2 because Grid 1 is gradually being deprecated by the Selenium Team.
Selenium Grid uses a hub-node concept where you only run the test on a single machine called a hub, but the execution will be done by different machines called nodes.

When to Use Selenium Grid?

You should use Selenium Grid when you want to do either one or both of following :
  • Run your tests against different browsers, operating systems, and machines all at the same time.This will ensure that the application you are testing is fully compatible with a wide range of browser-OS combinations.
  • Save time in execution of your test suites. If you set up Selenium Grid to run, say, 4 tests at a time, then you would be able to finish the whole suite around 4 times faster.

Grid 1.0 Vs Grid 2.0

Following are the main differences between Selenium Grid 1 and 2.
Grid 1 Grid 2
Selenium Grid 1 has its own remote control that is different from the Selenium RC server. They are two different programs. Selenium Grid 2 is now bundled with the Selenium Server jar file
You need to install and configure Apache Ant first before you can use Grid 1. You do not need to install Apache Ant in Grid 2.
Can only support Selenium RC commands/scripts. Can support both Selenium RC and WebDriver scripts.
You can only automate one browser per remote control. One remote control can automate up to 5 browsers.

What is a Hub and Node?

The Hub

  • The hub is the central point where you load your tests into.
  • There should only be one hub in a grid.
  • The hub is launched only on a single machine, say, a computer whose OS is Windows 7 and whose browser is IE.
  • The machine containing the hub is where the tests will be run, but you will see the browser being automated on the node.

The Nodes

  • Nodes are the Selenium instances that will execute the tests that you loaded on the hub.
  • There can be one or more nodes in a grid.
  • Nodes can be launched on multiple machines with different platforms and browsers.
  • The machines running the nodes need not be the same platform as that of the hub.

How to Install and Use Grid 2.0?

In this section, you will use 2 machines. The first machine will be the system that will run the hub, while the other machine will run a node. For simplicity, let us call the machine where the hub runs as "Machine A" while the machine where the node runs will be "Machine B". It is also important to note their IP addresses. Let us say that Machine A has an IP address of 192.168.1.3 while Machine B has an IP of 192.168.1.4.
Step 1
Download the Selenium Server by here.
Step 2
You can place the Selenium Server .jar file anywhere in your HardDrive.But for the purpose of this tutorial, place it on the C drive of both Machine A and Machine B. After doing this, you are now done installing Selenium Grid. The following steps will launch the hub and the node.
Step 3
  • We are now going to launch a hub. Go to Machine A. Using the command prompt, navigate to the root of Machine A's - C drive ,because that is the directory where we placed the Selenium Server.
  • On the command prompt, type java -jar selenium-server-standalone-2.30.0.jar -role hub
  • The hub should successfully be launched. Your command prompt should look similar to the image below
Step 4
Another way to verify whether the hub is running is by using a browser. Selenium Grid, by default, uses Machine A's port 4444 for its web interface. Simply open up a browser and go to http://localhost:4444/grid/console
Also, you can check if Machine B can access the hub's web interface by launching a browser there and going to where "iporhostnameofmachineA" should be the IP address or the hostname of the machine where the hub is running. Since Machine A's IP address is 192.168.1.3, then on the browser on Machine B you should type http://192.168.1.3:4444/grid/console
Step 5
  • Now that the hub is already set up, we are going to launch a node. Go to Machine B and launch a command prompt there.
  • Navigate to the root of Drive C and type the code below. We used the IP address 192.168.1.3 because that is where the hub is running. We also used port 5566 though you may choose any free port number you desire.
  • When you press Enter, your command prompt should be similar to the image below.
Step 6
Go to the Selenium Grid web interface and refresh the page. You should see something like this.
At this point, you have already configured a simple grid. You are now ready to run a test remotely on Machine B.

Designing Test Scripts That Can Run on the Grid

To design test scripts that will run on the grid, we need to use DesiredCapabilites and the RemoteWebDriver objects.
  • DesiredCapabilites is used to set the type of browser and OS that we will automate
  • RemoteWebDriver is used to set which node (or machine) that our test will run against.
To use the DesiredCapabilites object, you must first import this package
To use the RemoteWebDriver object, you must import these packages.

Using the DesiredCapabilites Object

Go to the Grid's web interface and hover on an image of the browser that you want to automate. Take note of the platform and the browserName shown by the tooltip.
In this case, the platform is "XP" and the browserName is "firefox".
We will use the platform and the browserName in our WebDriver as shown below (of course you need to import the necessary packages first).

Using the RemoteWebDriver Object

Import the necessary packages for RemoteWebDriver and then pass the DesiredCapabilities object that we created above as a parameter for the RemoteWebDriver object.

Running a Sample Test Case on the Grid

Below is a simple WebDriver TestNG code that you can create in Eclipse on Machine A. Once you run it, automation will be performed on Machine B.
 The test should pass.

Summary

  • Selenium Grid is used to run multiple tests simultaneously in different browsers and platforms.
  • Grid uses the hub-node concept.
  • The hub is the central point wherein you load your tests.
  • Nodes are the Selenium instances that will execute the tests that you loaded on the hub.
  • To install Selenium Grid, you only need to download the Selenium Server jar file - the same file used in running Selenium RC tests.
  • There are 2 ways to verify if the hub is running: one was through the command prompt, and the other was through a browser
  • To run test scripts on the Grid, you should use the DesiredCapabilities and the RemoteWebDriver objects.
  • DesiredCapabilites is used to set the type of browser and OS that we will automate
  • RemoteWebDriver is used to set which node (or machine) that our test will run against.