Tuesday, September 27, 2016

Capture Screenshots

Capture Screenshots

Screenshot capture functionality helps us to grab the screenshot at run time when required, inparticularly when a failure happens. With the help of screenshots and log messages we will be able to analyse the results better

Screenshots are configured differently for local executions and Selenium Grid(remote) Executions. Let us take a look at each one them with an example

Localhost Execution

In the below example, we will take a screenshot after calculating the percentage. Ensure that you give a valid path to save the screenshot.

import java.io.File; import java.io.IOException; import java.util.concurrent.TimeUnit; import org.apache.commons.io.FileUtils; import org.openqa.selenium.*; import org.openqa.selenium.firefox.FirefoxDriver; public class webdriverdemo { public static void main(String[] args) throws IOException { WebDriver driver = new FirefoxDriver(); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to("http://www.calculator.net/"); //Maximize the browser driver.manage().window().maximize(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Percent Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Click Calculate Button driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Get the Result Text based on its xpath String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:\\screenshots\\screenshots1.jpg")); //Print a Log In message to the screen System.out.println(" The Result is " + result); //Close the Browser. driver.close(); } }

Output

Upon Executing the script, the screenshot is saved in the 'D:\screenshots' folder with the name 'screenshots1.jpg' as shown below.

Selenium Grid - Screenshot Capture

When working with Selenium Grids, we should ensure that we are taking the screenshot correctly from the remote system. We will make use of augmented driver.

Example

We will execute the script on a firefox node attached to a hub. More on configuring hub and nodes, please refer to Selenium Grids Chapter.

package TestNG; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.TakesScreenshot; import java.util.concurrent.TimeUnit; import org.openqa.selenium.*; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeTest; import org.testng.annotations.Parameters; import org.testng.annotations.Test; import java.io.File; import java.net.URL; import java.net.MalformedURLException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.remote.RemoteWebDriver; import java.io.IOException; public class TestNGClass { public WebDriver driver; public String URL, Node; protected ThreadLocal<RemoteWebDriver> threadDriver = null; @Parameters("browser") @BeforeTest public void launchapp(String browser) throws MalformedURLException { String URL = "http://www.calculator.net"; if (browser.equalsIgnoreCase("firefox")) { System.out.println(" Executing on FireFox"); String Node = "http://10.112.66.52:5555/wd/hub"; DesiredCapabilities cap = DesiredCapabilities.firefox(); cap.setBrowserName("firefox"); driver = new RemoteWebDriver(new URL(Node), cap); //Puts a Implicit wait, Will wait for 10 seconds before throwing exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //Launch website driver.navigate().to(URL); driver.manage().window().maximize(); } else { throw new IllegalArgumentException("The Browser Type is Undefined"); } } @Test public void calculatepercent() throws IOException { driver.findElement(By.xpath(".//*[@id='menu']/div[3]/a")).click(); // Click on Math Calculators driver.findElement(By.xpath(".//*[@id='menu']/div[4]/div[3]/a")).click(); // Click on Percent Calculators // Make use of augmented Driver to capture Screenshots. WebDriver augmentedDriver = new Augmenter().augment(driver); File screenshot = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("D:\\screenshots\\remotescreenshot1.jpg")); // Please note - Screenshot would be saved on the system where the script is executed and NOT on remote machine. driver.findElement(By.id("cpar1")).sendKeys("10"); // Enter value 10 in the first number of the percent Calculator driver.findElement(By.id("cpar2")).sendKeys("50"); // Enter value 50 in the second number of the percent Calculator driver.findElement(By.xpath(".//*[@id='content']/table/tbody/tr/td[2]/input")).click(); // Click Calculate Button String result = driver.findElement(By.xpath(".//*[@id='content']/p[2]/span/font/b")).getText(); // Get the Result Text based on its xpath System.out.println(" The Result is " + result); //Print a Log In message to the screen if(result.equals("5")) { System.out.println(" The Result is Pass"); } else { System.out.println(" The Result is Fail"); } } @AfterTest public void closeBrowser() { driver.quit(); } }

Output

Upon executing the script, the screenshot is captured and saved in the specified location as shown below.

No comments:

Post a Comment