Selenium - 写入网络驱动器。可以写文本文件而不是图像文件

时间:2017-09-20 13:26:05

标签: java webdriver javax.imageio smb

我很抱歉,如果这已经发布,但似乎有很多答案,所以我想我会在这里要求澄清一下。我使用以下屏幕截图Webdriver代码将屏幕截图写入我的本地驱动器,效果很好:

final Screenshot screenshot = new AShot().shootingStrategy(
new ViewportPastingStrategy(500)).takeScreenshot(driver);
final BufferedImage image = screenshot.getImage();

File outputfile = new File("//Users/me/Desktop/testfolder/saved.png");
ImageIO.write(image, "PNG", outputfile);

此文件完全写入我的本地驱动器。但是,我想写一个我有权访问的网络驱动器并测试了这个访问权限,因为我能够写一个文本文件。

驱动器看起来像这样(带有我的名字和密码):smb:// globalnerds; Carl.Lewis:Default32@file-16ca.bs.bview.com/bs-test/

我可以在那里写一个文本文件,但是当我尝试编写图像时它不起作用。

任何人都可以帮我解决这个问题吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

所以我想出了如何做到这一点并使用下面的代码像魅力一样工作。我抓取一张图片并将其写入带有凭据的共享目录。我将凭证和路径分成一行:

public void WriteMe()抛出异常{

    InputStream in = null;
    OutputStream out = null;
    try {
        //Get a picture
        File localFile = new File ("//Users/BIG.BEAR/Desktop/testfolder/saved.png");
        String remotePhotoUrl = "smb://globalnerds;BIG.BEAR:MYBEARPASSWORD@file-6666ca.haq.portview.com/testfolder/"; //The shared directory to store pictures
        SimpleDateFormat fmt = new SimpleDateFormat ("yyyyMMddHHmmssSSS_");
        SmbFile remoteFile = new SmbFile (remotePhotoUrl + "/" + fmt.format (new Date ()) + localFile.getName ());
        remoteFile.connect (); //Try to connect

        in = new BufferedInputStream (new FileInputStream (localFile));
        out = new BufferedOutputStream (new SmbFileOutputStream (remoteFile));

        byte[] buffer = new byte[4096];
        int len = 0; //Read length
        while ((len = in.read (buffer, 0, buffer.length)) != -1) {
            out.write (buffer, 0, len);
        }
        out.flush (); //The refresh buffer output stream
    } catch (Exception e) {
        String msg = "The error occurred: " + e.getLocalizedMessage ();
        System.out.println (msg);
    } finally {
        try {
            if (out != null) {
                out.close ();
            }
            if (in != null) {
                in.close ();
            }
        } catch (Exception e) {
        }
    }


}