无法获取正确窗口的屏幕截图

时间:2020-01-27 05:43:05

标签: java selenium-webdriver automation testng

此代码用于屏幕截图捕获,但是此代码未获得理想的输出。我无法截取正确窗口的屏幕截图。我的代码初始化了一个新的Chrome驱动程序,但没有得到该窗口的屏幕截图,这是我所需要的。

/----------------------------------take screenshot class--------------------------------/        
public class ScreenCapture {
    SendMailUsingAuthentication SMUA = new SendMailUsingAuthentication ();

        public void takeScreenShot() throws IOException, MessagingException   {
            String timeStamp;
            File screenShotName;
            WebDriver driver = new ChromeDriver();
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                //The below method will save the screen shot in d drive with name "screenshot.png"
                timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime()); 
                screenShotName = new File("D:\\Manzelinc\\SNST_test_Auto\\Manzel"+timeStamp+".png");
                try {
                    FileUtils.copyFile(scrFile, screenShotName);
                    SMUA.postMail(SendMailUsingAuthentication.emailList, SendMailUsingAuthentication.emailSubjectTxt, SendMailUsingAuthentication.emailMsgTxt, SendMailUsingAuthentication.emailFromAddress);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } 

/----------------------------Listener class---------------------------------------------------------/
public class ReportGeneration implements ITestListener, IReporter  {
WebDriver driver;
SendMailUsingAuthentication SMUA = new SendMailUsingAuthentication ();

public void onStart(ITestContext arg0) {
     System.out.println("Start Of Execution(TEST)->"+arg0.getName());
}
public void onTestStart(ITestResult arg0) {
    System.out.println("Test Started->"+arg0.getName());
}
public void onTestSuccess(ITestResult arg0) {
    System.out.println("Test Pass->"+arg0.getName());
}
public void onTestFailure(ITestResult arg0) {
    System.out.println("Test Failed->"+arg0.getName());
    //take Screenshot------------------------
    ScreenCapture SC = new ScreenCapture();
        try {
            SC.takeScreenShot();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    Reporter.log("<br> <img src=.\\screenshots\\Untitled.png /> <br>");
    Reporter.setCurrentTestResult(null);
}   

0 个答案:

没有答案
相关问题