InterviewSolution
| 1. |
How can we take a screenshot in Selenium? |
|
Answer» While executing the test cases, the test case may fail. The way while executing the test case manually we take a SCREENSHOT and place it in RESULT repository, same way things can be done by using Selenium WebDriver. We may need to capture SCREENSHOTS using Selenium WebDriver in scenarios -
Selenium is having an INTERFACE named TakesScreenshot to take the screenshot. This Instance has one method getScreenShotAs() will be used to take a screenshot during test execution and saves that screenshot at the desired location that we provide in the code. Syntax to capture and save the screenshot is - File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);For ex. File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(screenshot1.png); Syntax to store it in our local drive is - FileUtils.copyFile(screenshotFile, new File("filename_with_path")); For ex. FileUtils.copyFile(screenshotFile, new File("c:\\screenshot1.png")); package Managescreenshot; import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class CaptureScreenshot { @Test public static void captureScreenMethod() throws Exception{ System.setProperty("webdriver.gecko.driver","c://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.manage().window().MAXIMIZE(); driver.get("https://www.facebook.com/capture-screenshot-using-selenium-webdriver"); File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File("C:\\screenshot1.png")); driver.close(); driver.quit(); } }When the script fails, to know where was the error in script capture a screenshot of the web page when the test case fails. By seeing screenshot, We can easily identify where exactly the script is failed. To take a screenshot of the failed test, we will place the entire code in try-catch block by placing the test steps in a try block and then screen capture statement in the catch block. In try block, if a test step fails then it goes to the catch block and captures a screenshot of the web page. package software testing material; import java.io.File; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.Test; public class CaptureFailScreenshot { public static void captureFailScreenMethod() throws Exception{ System.setProperty("webdriver.gecko.driver","C://Selenium Environment//Drivers//geckodriver.exe"); WebDriver driver = new FirefoxDriver(); driver.manage().window().maximize(); try{ driver.get("https://www.facebook.com"); driver.navigate().refresh(); driver.findElement(By.id("emaile")).sendKeys("testmail@gmail.com"); //Statement with incorrect id }catch(Exception e){ File screenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshotFile, new File("C:\\incorrectuser.png")); } driver.close(); driver.quit(); } } |
|