1)屏幕截图

接口函数是 TakesScreenshot

示例:

import java.io.File;

import org.apache.commons.io.FileUtils;

public void testTakesScreenshot() throws Exception{

  //TakesScreenshot接口提供了getScreenshotAs 方法截取屏幕,这里指定OutputType.FILE 作为参数将截屏以文件形式返还

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

  //使用FileUtils 类的copyFile 方法保存返还的文件对象

  FileUtils.copyFile(srcFile, new File("/Selenium 2/screenshot.png"));

}

2)复制文件

文件操作类FileHandler

示例:

import java.io.File;

import java.io.IOException;

//将源目录下所有文件复制的目的目录下

FileHandler.copy(new File("/source_directory"), new File("/destination_directory"));

//将指定文件从源目录复制到目的目录

FileHandler.copy(new File("/source_directory/file.txt"), new File("/destination_directory/file.txt"));

//将以suffix.txt 为扩展名的所有文件从源目录复制到目的目录

FileHandler.copy(new File("/path/of/source_directory"), new File("/destination_directory"), "suffix.txt");

3)创建目录

FileHandler.createDir()方法创建目录

示例:

import org.openqa.selenium.io.FileHandler;

FileHandler.createDir(new File("/new_created_directory"));

4)删除目录

FileHandler.delete()方法删除目录

示例:

import java.io.File;

import org.openqa.selenium.io.FileHandler;

FileHandler.delete(new File("/new_created_directory"));

5)读取文件

FileHandler.readAsString()方法

示例:

import java.io.File;

import jave.io.IOException;

String file = FileHandler.readAsString(new File("/directory/fileToRead.txt"));

System.out.println(file);

6)压缩目录

压缩操作对应需要从远程机器获取大量的日志文件或截屏文件而言非常有效。zip类的使用方法如下,包括压缩和解压缩:

import java.io.File;

import java.io.IOException;

import org.openqa.selenium.io.Zip;

...

String FileName = "/final_directory/zipped_file.zip";

Zip zip = new Zip();

try{

  //把指定目录下所有文件打包压缩成单个 zip 文件

  zip.zip(new File("/directory_to_zip"), new File(FileName ));

  //判断一个文件是否为压缩文件

  System.out.println(Filehandle.isZipped(FileName ));

  //将一个zip 文件解压缩

  zip.unzip(new File(FileName), new File("/final_directory"));

}catch(IOException e){

  e.printStackTrace();

}

7)临时目录操作

操作临时文件系统的类TemporaryFilesystem

示例:

import java.io.File;

import org.openqa.selenium.io.TemporaryFilesystem;

...

File tempDirectory = TemporaryFilesystem.getDefaultTmpFS().createTempDir("prefix", "suffix");

//获取临时目录的绝对路径

System.out.println(tempDirectory.getAbsolutePath());

//获取临时目录可用空间

System.out.println("Free Space of Temporary Directory is : " + tempDirectory.getFreeSpace());

...

8)文件权限操作

FileHandler 调整文件权限

示例:

...

if (! FileHandler.canExecute(new File("/directory/file1.sh"))){

  try{

    FileHandler.makeExecutable(new File("/directory/file1.sh"));

  }catch (IOException e){

    e.printStackTrace();

  }

  try{

    FileHandler.makeWritable(new File("/directory/file2.txt"));

  }

  catch (IOException e){

    e.printStackTrace();

  }

}

[Selenium] WebDriver 操作文件系统的更多相关文章

  1. Selenium Webdriver——操作隐藏的元素(二)display属性

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...

  2. Selenium Webdriver——操作隐藏的元素(三)switchTo().frame()

    在web 应用中经常会遇到frame 嵌套页面的应用,页WebDriver 每次只能在一个页面上识别元素,对于frame 嵌套内的页面上的元素,直接定位是定位是定位不到的.这个时候就需要通过switc ...

  3. Selenium Webdriver——操作隐藏的元素

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...

  4. selenium webdriver操作各浏览器

    描述 本文主要是针对Chrome 62 , firefox57 ,和IE11 三个版本的操作.相关的driver .可点击以下链接.所有的driver 建议放在浏览器的目录下,本文中所有的driver ...

  5. Selenium Webdriver——操作隐藏的元素display属性

    有时候我们会碰到一些元素不可见,这个时候selenium就无法对这些元素进行操作了.例如,下面的情况: 页面主要通过“display:none”来控制整个下拉框不可见.这个时候如果直接操作这个下拉框, ...

  6. Selenium webdriver 操作日历控件

    一般的日期控件都是input标签下弹出来的,如果使用webdriver 去设置日期, 1. 定位到该input 2. 使用sendKeys 方法 比如: 但是,有的日期控件是readonly的 比如1 ...

  7. Selenium webdriver 操作chrome 浏览器

    Step1: 下载chromedriver. 下载路径: http://chromedriver.storage.googleapis.com/index.html 选择一个合适的下载即可.我下载的是 ...

  8. Selenium webdriver 操作IE浏览器

    V1.0版本:直接新建WebDriver使用 import org.openqa.selenium.WebDriver; import org.openqa.selenium.ie.InternetE ...

  9. [Selenium] WebDriver 操作 HTML5 中的 drag/drop

    以 jQuery UI 官方网站元素测试,地址:http://jqueryui.com/draggable/ 示例: package com.learningselenium.html5; impor ...

随机推荐

  1. Java 并发编程中的 CyclicBarrier 用于一组线程互相等待

    Java 5 引入的 Concurrent 并发库软件包中的 CyclicBarrier 是一个同步辅助类,它允许一组线程互相等待,直到到达某个公共屏障点 (common barrier point) ...

  2. css,世界上没有绝对简单的事情

    引文 自从学了前端的基础,自认为是没什么css是能难倒我的,可是事实是,世界上没有绝对简单的事情,实际上还有好多的东西等待我们去发掘. 详解 1.有些浏览器不完全支持css3,现在可以用 modern ...

  3. python多线程(三)

    原文:http://www.cnblogs.com/tqsummer/archive/2011/01/25/1944771.html 一.Python中的线程使用: Python中使用线程有两种方式: ...

  4. 嵌入式cpu架构

    原文:http://www.kaixin001.com/repaste/11007221_7220618944.html### 内容: 目前主要CPU架构有ARM.X86/Atom.MIPS.Powe ...

  5. java list实现树形结构

    1.javabean import java.util.List; public class TreeNode { private String id; private String parentId ...

  6. C# 读写bat文件

    读: var batFile = "D:\\test.bat"; if (File.Exists(batFile)) { using (var sr = new StreamRea ...

  7. DELPHI方法注释的标准写法

    /// <summary> /// 查询数据 /// </summary> /// <param name="accountno">帐套号< ...

  8. 转: CentOS 6 使用 yum 安装MongoDB及服务器端配置

    转: http://www.cnblogs.com/shanyou/archive/2012/07/14/2591838.html CentOS 6 使用 yum 安装MongoDB及服务器端配置   ...

  9. C/C++知识要点2——STL中Vector、Map、Set容器的实现原理

    1.Vector是顺序容器.是一个动态数组.支持随机存取.插入.删除.查找等操作,在内存中是一块连续的空间.在原有空间不够情况下自己主动分配空间.添加为原来的两倍.vector随机存取效率高,可是在v ...

  10. leetcode Valid Palindrome C++&amp;python 题解

    题目描写叙述 Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...