Unable to take screenshot in Selenium

I am trying to capture a screenshot for each failure occurrence and written following code, but this is not working.

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test   
    public void Testone(){
        driver.get("http://www.google.com/");           
    }

    @AfterMethod(alwaysRun=true)
    public void catchExceptions(ITestResult result){
        System.out.println("result"+result);
        String methodName = result.getName();
        System.out.println(methodName);

        if(!result.isSuccess()){         
            try { 
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile,new File("C:screenshot2.png" ));
            } catch (IOException e1) {
                e1.printStackTrace();
            }       
        }
    }
}

This is failing at

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


stack trace:

[TestNG] Running:
C:Documents and Settings537310Local SettingsTemptestng-eclipse-1576306112testng-customsuite.xml

result[TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}]
FAILED CONFIGURATION: @AfterMethod catchExceptions([TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}])
net.sf.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->Class org.openqa.selenium.remote.Augmenter$CompoundHandler can not access a member of class org.openqa.selenium.firefox.FirefoxDriver with modifiers "protected"
at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)

list of imports:

package com.example.tests;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;  

The stacktrace you shared is not the stacktrace, but I think the testng log.
The example you provided actually works. I just made the test fail, because in the @AfterMethod a screenshot is taken only if the test fails: if(!result.isSuccess())
Then when I ran the example again, I got:
java.io.FileNotFoundException: C:screenshot2.png (Access is denied)
Then I changed the location of the picture to be on D: where the permissions are correct, and it worked end to end, I can see the screenshot.

Cheers

import java.io.File;
import java.io.IOException;

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.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test
    public void Testone() {

        driver.get("http://www.google.com/");
        assert false;

    }

    @AfterMethod(alwaysRun = true)
    public void catchExceptions(ITestResult result) {
        System.out.println("result" + result);
        String methodName = result.getName();
        System.out.println(methodName);

        if (!result.isSuccess()) {

            try {

                File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile, new File("C:screenshot2.png"));
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }
    }
}

Hi sinisa229 mihajlovski,

Your script is working properly. but there is a slight change in your script. If i won't comment the line "assert false", it is giving error.


Try This :

 WebDriver augmentedDriver = new Augmenter().augment(driver);
    File screenshot = ((TakesScreenshot)augmentedDriver).
                        getScreenshotAs(OutputType.FILE);

In place of

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

链接地址: http://www.djcxy.com/p/11548.html

上一篇: android locationManager继续在removeUpdates之后调用onLocationChanged

下一篇: 无法在Selenium中截屏