Module 6: Selenium Automation Framework Common Utilities. Initializing Browsers & Capturing Screenshots

Hi Guys! Now we are ready to build some common utilities for our Selenium Automation framework. Today our major focus would be on creating a utility to Initialize our Web Browser (Chrome/Firefox) and Capture screenshots.

Cross browser Testing plays a key role in automation. This utility will help us execute the same automated tests across all browsers we configure hence avoiding any redundancy.

Even if we are logging a Bug manually in any bug tracking tool like JIRA , Screenshots play an important role. They help developers understand the logged defect and provides more clarity than text. Visual Content like Images/ Videos is always soothing for our eyes than mere text. 


Step 1:  Create a New Java Package:

Create a utility package as in Screenshot below: (Right Click on src/main/java folder >>> Select New >>> Select Package). Name that package as "utility". Congratulations on creating your very 1st java package. So simple Isn't it? 

Just for better understanding, Consider your Project as a "House". Packages are different Rooms in that "House" and Classes are the elements like Bed, Chair, Tables etc. present in that Room


 
Step 2: Create a new Class  (Right Click on package Utility >>> Select New >>> Select Class). Name that class as "InitializeBrowserCaptureScreenshot"

Now our project structure looks like:

InitializeBrowserCaptureScreenshot
We are going to launch browser using this file on the basis of parameters passed in our testng.xml file (We will discuss testng.xml in later tutorials, there would be a separate module for that).

This file contains the a method/function to initialize driver for Chrome or Firefox based on on the basis of parameters passed in testng.xml file.


We would add one more metod in this class to capture screenshot and store it in .png format with name as method name in “Screenshots” directory.

For initializing the browser, driver path will used from data.properties

We will use below code snippet to use driver paths:

Properties prop= new Properties();
FileInputStream fis= new FileInputStream("Properties\\data.properties");
prop.load(fis);
String browserName=browser;
String ChromedriverPath = prop.getProperty("ChromedriverPath");
For Screenshots we will create a function with below code.(We will pass method name as parameter our function so that we are able to identify which Screenshot belongs to which Success/Failure).

File source = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("./Screenshots/"+methodName+".png"));
Simply copy the code from below snippet in your class
InitializeBrowserCaptureScreenshot.java and we are done for today. You will get lots of errors on pasting below code snippet. Don't worry! Try to fix them by yourself, else I'll give a Bonus Module tomorrow on how to fix them.

Hint: You need to add dependencies for libraries being used in your pom.xml file

Stay tuned for next utility on Listeners tomorrow. Happy Testing :)


Comments