top of page

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

  • Writer: khyati sehgal
    khyati sehgal
  • Sep 17, 2014
  • 2 min read

This blog will only focus on areas in which coders usually search over the 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 screenshots in selenium WebDriver?

If we have to prove something to either a developer or a Product owner then we generally 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 screenshot, there could be other reasons as well, I am sure there are.. but the major is to give another person a clear picture of what happened and how? Selenium also provides certain features via which we can take screenshots 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);
FileUtils.copyFile(scrFile,
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 maximize screen in selenium?

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

How to delete cookies with selenium?

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

How to delete text in selenium?

public static void removeTextUsingBackSpace(WebElement element)
{
for (int i = 0; i  0)
{
element.sendKeys(Keys.BACK_SPACE);
}
} 

How to press enter in selenium?

public static void pressEnter() throws InterruptedException, IOException
{
Actions action = new Actions(getDriver());
action.sendKeys(Keys.RETURN);
action.perform();
}
 

How to press escape in selenium?

public static void  pressEscape() throws InterruptedException, IOException
{
Actions action = new Actions(getDriver());
action.sendKeys(Keys.ESCAPE).perform();
}
 

How to get the 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 the Current Date in selenium?


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

コメント


bottom of page