您的位置:首页 > 产品设计 > UI/UE

使用phantomjs进行无界面UI自动化测试

2016-02-23 22:11 417 查看
PhantomJS(http://phantomjs.org/) 是一个基于WebKit的服务器端JavaScript API。它全面支持web而不需浏览器支持,其快速、原生支持各种Web标准:DOM处理, CSS选择器, JSON, Canvas, 和SVG。 PhantomJS 可以用于页面自动化,网络监测,网页截屏以及无界面测试等。

0.下载安装(以Mac为例)

下载地址:http://phantomjs.org/download.html 下载后进行解压

将 phantomjs-2.0.0-macosx/bin/phantomjs 文件放到 ~/bin/ 路径下,以便于进行工具统一管理

创建软链接,在终端输入 " ln -s ~/bin/phantomjs /usr/local/bin/ " ,用于从终端启动phantomjs

1.运行demo,验证安装是否正常

在终端进入 phantomjs-2.0.0-macosx/examples 目录

运行 " phantomjs hello.js "

输出 " Hello, world! " ,安装成功

2.使用phantomjs访问web,并进行截图

编写测试脚本test.js

var page=require('webpage').create();
page.open('http://leettest.com',function(){
page.viewportSize={width:1920,height:968};
page.render('leettest.png');
phantom.exit();
});


运行测试脚本

phantomjs test.js

查看截图,验证phantomjs访问

3.使用selenium驱动phantomjs,在不启动浏览器的情况下进行UI自动化测试

编写测试脚本(Java)

public static void main(String[] args) throws IOException {
WebDriver driver = new PhantomJSDriver();
driver.get("http://www.baidu.com");
driver.manage().window().maximize();
if(driver.getTitle().contains("百度")){
TakesScreenshot shot = ((TakesScreenshot)driver);
File srcFile=shot.getScreenshotAs(OutputType.FILE);
srcFile.renameTo(new File("/Users/wp/Documents/demo.png"));
}
driver.close();
}


依赖jar包:



运行测试,查看页面截图
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: