下面列出了怎么用org.openqa.selenium.phantomjs.PhantomJSDriverService的API类实例代码及写法,或者点击链接到github查看源代码。
protected Capabilities createCapabilities() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, createProxy());
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
getPhantomJsExecutablePath());
if (getPhantomJsCliArgs() != null && getPhantomJsCliArgs().trim().length() > 0) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliParamsToStringArray(getPhantomJsCliArgs()));
}
if (getPhantomJsGhostdriverCliArgs() != null && getPhantomJsGhostdriverCliArgs().trim().length() > 0) {
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, cliParamsToStringArray(getPhantomJsGhostdriverCliArgs()));
}
return capabilities;
}
@BeforeClass
public static void startServer() throws ServletException {
// INIT WEB SERVER (TOMCAT)
server = new EmbeddedServer(8080, "/20150118-test-selenium");
server.start();
// INIT WEB BROWSER (SELENIUM + PHANTOMJS)
driver = new PhantomJSDriver(
new DesiredCapabilities(ImmutableMap.of( //
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, //
new PhantomJsDownloader().downloadAndExtract()
.getAbsolutePath())));
baseUrl = "http://localhost:8080/20150118-test-selenium";
driver.manage().timeouts().implicitlyWait(5, SECONDS);
}
@Before
public void createConfig() throws IOException {
config = new PhantomJSDriverConfig();
config.setPhantomJsExecutablePath(System.getProperty(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY));
JMeterVariables variables = new JMeterVariables();
JMeterContextService.getContext().setVariables(variables);
try {
config.threadStarted();
} catch (UnreachableBrowserException ignored) {
// TODO: mock it properly
}
}
public PhantomJSDriverService getPhantomJsService() {
return new PhantomJSDriverService.Builder()
.usingAnyFreePort()
.usingPhantomJSExecutable(
new File(ResourceHelper
.getResourcePath("driver/phantomjs.exe")))
.withLogFile(
new File(ResourceHelper
.getResourcePath("logs/phantomjslogs/")
+ "phantomjslogs"
+ DateTimeHelper.getCurrentDateTime() + ".log"))
.build();
}
/**
* Use phantomjs to fetch the web content.
* @return WebDriver.
*/
protected WebDriver phantomJsDriver() {
String phantomJsExecPath = System.getProperty("phantomjsExec");
if(phantomJsExecPath == null || "".equals(phantomJsExecPath)) {
phantomJsExecPath = "/usr/local/bin/phantomjs";
}
DesiredCapabilities dc = new DesiredCapabilities();
dc.setJavascriptEnabled(true);
dc.setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
phantomJsExecPath
);
return new PhantomJSDriver(dc);
}
/**
*
* @param config
* @return A FirefoxDriver.
*/
@Override
public RemoteWebDriver make(HashMap<String, String> config) {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
if (System.getProperty(PHANTOMJS_PATH_PROPERTY) != null) {
path = System.getProperty(PHANTOMJS_PATH_PROPERTY);
}
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
path);
return new PhantomJSDriver(caps);
}
private void configurePhantomJSDriver(WebDriverConfiguration webDriverCfg, DesiredCapabilities capabilitiesToAdd) {
if (!webDriverCfg.getBrowser().equals("phantomjs")) {
return;
}
String cliArgs = System.getProperty("keycloak.phantomjs.cli.args");
if (cliArgs == null) {
cliArgs = "--ignore-ssl-errors=true --web-security=false";
}
capabilitiesToAdd.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgs);
}
@Override
public WebDriver getDefaultDriver() {
// INIT WEB BROWSER (SELENIUM + PHANTOMJS)
driver = new PhantomJSDriver(new DesiredCapabilities(ImmutableMap.of( //
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, //
new PhantomJsDownloader().downloadAndExtract()
.getAbsolutePath())));
baseUrl = "http://localhost:8080/20150125-test-fluentlenium";
driver.manage().timeouts().implicitlyWait(5, SECONDS);
return driver;
}
@Override
public Document load(PageRequest pageRequest) {
if (!UrlUtil.isUrl(pageRequest.getUrl())) {
return null;
}
// driver init
DesiredCapabilities dcaps = new DesiredCapabilities();
dcaps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, !pageRequest.isValidateTLSCertificates());
dcaps.setCapability(CapabilityType.TAKES_SCREENSHOT, false);
dcaps.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
dcaps.setJavascriptEnabled(true);
if (driverPath!=null && driverPath.trim().length()>0) {
dcaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, driverPath);
}
if (pageRequest.getProxy() != null) {
dcaps.setCapability(CapabilityType.ForSeleniumServer.AVOIDING_PROXY, true);
dcaps.setCapability(CapabilityType.ForSeleniumServer.ONLY_PROXYING_SELENIUM_TRAFFIC, true);
System.setProperty("http.nonProxyHosts", "localhost");
dcaps.setCapability(CapabilityType.PROXY, pageRequest.getProxy());
}
/*dcaps.setBrowserName(BrowserType.CHROME);
dcaps.setVersion("70");
dcaps.setPlatform(Platform.WIN10);*/
WebDriver webDriver = new PhantomJSDriver(dcaps);
try {
// driver run
webDriver.get(pageRequest.getUrl());
if (pageRequest.getCookieMap() != null && !pageRequest.getCookieMap().isEmpty()) {
for (Map.Entry<String, String> item: pageRequest.getCookieMap().entrySet()) {
webDriver.manage().addCookie(new Cookie(item.getKey(), item.getValue()));
}
}
webDriver.manage().timeouts().implicitlyWait(pageRequest.getTimeoutMillis(), TimeUnit.MILLISECONDS);
webDriver.manage().timeouts().pageLoadTimeout(pageRequest.getTimeoutMillis(), TimeUnit.MILLISECONDS);
webDriver.manage().timeouts().setScriptTimeout(pageRequest.getTimeoutMillis(), TimeUnit.MILLISECONDS);
String pageSource = webDriver.getPageSource();
if (pageSource != null) {
Document html = Jsoup.parse(pageSource);
return html;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
if (webDriver != null) {
webDriver.quit();
}
}
return null;
}
public WebDriver getPhantomJsDriver(PhantomJSDriverService sev,
Capabilities cap) {
return new PhantomJSDriver(sev, cap);
}
public WebDriver getPhantomJsDriver(String hubUrl,PhantomJSDriverService sev,
Capabilities cap) throws MalformedURLException {
return new RemoteWebDriver(new URL(hubUrl), cap);
}