Automation, Automation Framework, Cookies, Functional Testing, Java, Java Basics, Java Class, Java Methods, Java Object, Selenium, Testing, Uncategorized, Web Automation

Part 1 – Resolution of some major-frequently-seen Selenium utilities.

This blog will only focus on areas which coders usually search over web. Here I am collating the data according to my own experience. These methods can be added in the Utility class directly to make wide use of different functionalities.

How to take screen shots in selenium WebDriver?

If we have to prove something to either a developer or a Product owner then we generally we have several ways via which we can actually show the idea to them, but what if whatever you want to show stop working when you actually want to demo it? This is silly, right? But it happens, You have to believe me!!

So for such circumstances we search for back up which will show what exactly we want to show. And that’s where we require screen shot, there could be other reasons as well, I am Sure there are.. but the major is to give other person a clear picture of what happened and how? Selenium also provides certain features via which we can take screen shots of the tests here is the small code to do it.

Method #1

// Method to take screenshot and save in proper folder. Maintains the test name associated with the failed test.
public void screenshotOnTestFailure(ITestResult testResult)
throws InterruptedException, IOException, WebDriverException
if (testResult.getStatus() == ITestResult.FAILURE)
File scrFile = ((TakesScreenshot) getDriver()).getScreenshotAs(OutputType.FILE);
new File(workingDir + "/testdata/SeleniumFailureScreenshots/" + testResult.getName() + ".jpg"));

Method #2

public static void takeSnapShot(WebDriver webdriver, String fileWithPath) throws Exception {
// Convert web driver object to TakeScreenshot
TakesScreenshot scrShot = ((TakesScreenshot) webdriver);
// Call getScreenshotAs method to create image file
File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
// Move image file to new destination
File DestFile = new File(fileWithPath);
// Copy file at destination
FileUtils.copyFile(SrcFile, DestFile);

How to maximise screen in selenium?

public static void maximizeScreen() throws InterruptedException, IOException {
java.awt.Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point position = new Point(0, 0);
Dimension maximizedScreenSize = new Dimension((int) screenSize.getWidth(), (int) screenSize.getHeight());

How to delete cookies with selenium?

public static void deleteCookies()
WebDriver driver = null;
Set cookies = driver.manage().getCookies();
if (!cookies.isEmpty())
{"Cookies Size: "+cookies.size());;
Iterator iter = driver.manage().getCookies().iterator();
while (iter.hasNext())
Cookie C =; + "\n" + C.getPath() + "\n" + C.getDomain() + "\n" + C.getValue() + "\n"
+ C.getExpiry());
cookies.clear();"Cookies Size: "+cookies.size());;

How to delete text in selenium?

public static void removeTextUsingBackSpace(WebElement element)
for (int i = 0; i  0)

How to press enter in selenium?

public static void pressEnter() throws InterruptedException, IOException
Actions action = new Actions(getDriver());

How to press escape in selenium?

public static void  pressEscape() throws InterruptedException, IOException
Actions action = new Actions(getDriver());

How to get current class and method name in selenium?

public static String getCurrentClassAndMethodNames()
final StackTraceElement e = Thread.currentThread().getStackTrace()[2];
final String s = e.getClassName();
return s.substring(s.lastIndexOf('.') + 1, s.length()) + "." + e.getMethodName();

How to get Current Date in selenium?

public static String getCurrentDate()
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy/MM/dd");
LocalDate localDate =;; // 2016/11/16
return localDate.toString();

1 thought on “Part 1 – Resolution of some major-frequently-seen Selenium utilities.”

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s