详细代码如下:

public class MyEventListener extends AbstractWebDriverEventListener  {
    @Override
    public void onException(Throwable throwable, WebDriver driver) {
        Throwable cause = throwable.getCause();
        if(cause instanceof ScreenshotException){
            String filePath = "F:/screenshot";
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-hh-mm-ss");
            String daString = format.format(new Date());
            File of = new File(filePath+daString+"-exception.png");
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(of);
                out.write(new Base64Encoder().decode(((ScreenshotException) cause).getBase64EncodedScreenshot()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally{
                try {
                    out.close();
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
    }

public void setUp() throws Exception{
        DesiredCapabilities capabilities = new DesiredCapabilities();
         WebDriverEventListener eventListener = new MyEventListener();
          driver = new EventFiringWebDriver(new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities)).register(eventListener);
//      driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
         Thread.sleep(6000);

    }

不能成功截图保存在文件中,麻烦大神帮忙解答下


↙↙↙阅读原文可查看相关链接,并与作者交流