软件自动化测试全套视频教程

2019-06-10 08:05
34

 

 

package com.sqt.phantomJs_Selenium_Demo;

 

 

import java.io.File;

import java.io.IOException;

import java.util.List;

import java.util.concurrent.TimeUnit;

 

 

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.phantomjs.PhantomJSDriver;

import org.openqa.selenium.phantomjs.PhantomJSDriverService;

import org.openqa.selenium.remote.DesiredCapabilities;

 

 

/**

 * Hello world!

 *

 */

public class App {

public static void main(String[] args) throws InterruptedException {

DesiredCapabilities dcaps = new DesiredCapabilities();

// 驱动支持(第二参数表明的是你的phantomjs引擎所在的路径,使用whereis phantomjs可以查看)

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,

"D:\\Java\\phantomjs-2.1.1\\bin\\phantomjs.exe");

 

 

String ip = "**.**.**.**:****";

// 启用磁盘缓存(在桌面服务缓存存储位置,默认为false)。也接受:[yes|no]。

// --ignore-ssl-errors=[true|false]忽略SSL错误,例如过期或自签名证书错误(默认为false)。也接受:[yes|no]。

// --proxy=address:port指定要使用的代理服务器(例如--proxy=192.168.1.42:8080)。

String[] values = { "--disk-cache=yes", "--ignore-ssl-errors=true", "--proxy=" + ip };

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, values);

 

 

 

// 定义是否在页面中执行脚本(默认为true)。

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "javascriptEnabled", true);

// 定义本地资源(例如来自文件)是否可以访问远程URL(默认为false)。

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "localToRemoteUrlAccessEnabled",

false);

// 定义是否应启用Web安全性(默认为true)。

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "webSecurityEnabled", false);

// 定义是否应针对跨站脚本尝试监视加载请求(默认为false)。

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "XSSAuditingEnabled", false);

// (以毫秒为单位)定义了超时之后所请求的资源将停止尝试并继续处

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "resourceTimeout", "10000");

// 定义是否加载内联图像(默认为true)

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "loadImages", false);

//

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "User-Agent",

"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4");

 

 

/* ------设置浏览器请求头 ------*/

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept",

"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language",

"zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");

dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Connection", "keep-alive");

 

 

 

// 创建无界面浏览器对象

PhantomJSDriver driver = new PhantomJSDriver(dcaps);

 

 

try {

driver.get("http://www.ip138.com/");

 

Thread.sleep(1000);

imgDownload(driver, "D:\\Java\\img\\page.png");

} catch (Exception e) {

e.printStackTrace();

} finally {

driver.quit();

}

}

 

 

public static void click(PhantomJSDriver driver, WebElement element) {

Actions action = new Actions(driver);

action.click(element);

}

 

 

public static void imgDownload(PhantomJSDriver driver, String path) {

 

 

// PhantomJSDriver driver = (PhantomJSDriver) element;

File imgFile = driver.getScreenshotAs(OutputType.FILE);

 

 

try {

FileUtils.copyFile(imgFile, new File(path));

} catch (IOException e) {

System.out.println("截图失败!!");

}

}

 

 

}

 

点我获取

 

 

帖子评论