本文整理汇总了Java中org.openqa.selenium.htmlunit.HtmlUnitDriver类的典型用法代码示例。如果您正苦于以下问题:Java HtmlUnitDriver类的具体用法?Java HtmlUnitDriver怎么用?Java HtmlUnitDriver使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HtmlUnitDriver类属于org.openqa.selenium.htmlunit包,在下文中一共展示了HtmlUnitDriver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: HiddenHtmlPrompt
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
public HiddenHtmlPrompt( UiDriver uiDriver ) {
super(uiDriver);
HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver;
HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
Field webClientField = null;
boolean fieldAccessibleState = false;
try {
TargetLocator targetLocator = driver.switchTo();
webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
fieldAccessibleState = webClientField.isAccessible();
webClientField.setAccessible(true);
webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
} catch (Exception e) {
throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
} finally {
if (webClientField != null) {
webClientField.setAccessible(fieldAccessibleState);
}
}
}
示例2: testHttpCsv
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
@Test
public void testHttpCsv() {
String uri = "/export?settings=%7B%22m%22%3A%22jvm_memory_used_percent%22%2C%22si%22%3A%225-MINUTE%22%2C%22t%22%3A%22HISTORY%22%2C%22f%22%3A%22CSV%22%2C%22np%22%3A-1%2C%22v%22%3Afalse%2C%22tf%22%3A%22LOCAL%22%2C%22ms%22%3Afalse%2C%22ro%22%3Afalse%2C%22te%22%3A%5B%5D%2C%22am%22%3Afalse%7D";
AtsdTest.driver.quit();//will use htmlwebdriver instead of phantomjs for this test
AtsdTest.driver = new HtmlUnitDriver();
AtsdTest.driver.navigate().to(AtsdTest.url);
Assert.assertEquals(this.generateAssertMessage("Should get login page"), driver.getTitle(), LoginService.title);
LoginService ls = new LoginService(AtsdTest.driver);
ls.login(AtsdTest.login, AtsdTest.password);
AtsdTest.driver.navigate().to(AtsdTest.url + uri);
String[] src = AtsdTest.driver.getPageSource().split("\n");
String head = src[0];
String body = src[1];
Assert.assertTrue("Csv file is incorrect", head.trim().equals("Timestamp,Value,Metric,Entity,host") && body.contains("jvm_memory_used_percent"));
AtsdTest.driver.quit();
AtsdTest.driver = null;
}
示例3: generateHtmlUnitDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
/**
* Generates a htmlunit webdriver.
*
* @return
* A htmlunit webdriver
*/
private WebDriver generateHtmlUnitDriver() {
logger.info("Generating HtmlUnit driver...");
final DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true);
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.ACCEPT);
capabilities.setJavascriptEnabled(true);
setLoggingLevel(capabilities);
// Proxy configuration
if (Context.getProxy().getProxyType() != ProxyType.UNSPECIFIED && Context.getProxy().getProxyType() != ProxyType.AUTODETECT) {
capabilities.setCapability(CapabilityType.PROXY, Context.getProxy());
}
return new HtmlUnitDriver(capabilities);
}
示例4: HiddenHtmlElement
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
public HiddenHtmlElement( UiDriver uiDriver,
UiElementProperties properties ) {
super(uiDriver, properties);
String matchingRules[] = properties.checkTypeAndRules(this.getClass().getSimpleName(),
"HiddenHtml",
RULES_DUMMY);
// generate the XPath of this HTML element
String xpath = HtmlElementLocatorBuilder.buildXpathLocator(matchingRules,
properties,
new String[]{},
"*");
properties.addInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR, xpath);
HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver;
htmlUnitDriver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
}
示例5: HiddenHtmlAlert
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
public HiddenHtmlAlert( UiDriver uiDriver ) {
super(uiDriver);
HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver;
HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
Field webClientField = null;
boolean fieldAccessibleState = false;
try {
TargetLocator targetLocator = driver.switchTo();
webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
fieldAccessibleState = webClientField.isAccessible();
webClientField.setAccessible(true);
webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
} catch (Exception e) {
throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
} finally {
if (webClientField != null) {
webClientField.setAccessible(fieldAccessibleState);
}
}
}
示例6: HiddenHtmlConfirm
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
public HiddenHtmlConfirm( UiDriver uiDriver ) {
super(uiDriver);
HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiDriver;
HtmlUnitDriver driver = (HtmlUnitDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
Field webClientField = null;
boolean fieldAccessibleState = false;
try {
TargetLocator targetLocator = driver.switchTo();
webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
fieldAccessibleState = webClientField.isAccessible();
webClientField.setAccessible(true);
webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
} catch (Exception e) {
throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
} finally {
if (webClientField != null) {
webClientField.setAccessible(fieldAccessibleState);
}
}
}
示例7: setUpDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
@BeforeClass
public static void setUpDriver() {
// Note that the logging level is a Java standard (thus the
// use of a java.util class instead of something specific
// to Selenium. You can modify these levels yourself if,
// for example, you would like to see only SEVERE errors.
// They can be set to ALL (show all messages) or OFF (no messages)
// or to any minimum level from FINEST (most verbose) to SEVERE
// (only show the most egregious of errors).
// A full lists of levels and descriptions are located at:
// https://docs.oracle.com/javase/7/docs/api/java/util/logging/Level.html
java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.NoOpLog");
driver = new HtmlUnitDriver();
}
示例8: start
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
WebDriver start(Capabilities capabilities) {
switch (this) {
case FIREFOX:
return new FirefoxDriver(capabilities);
case HTML_UNIT:
return new HtmlUnitDriver(capabilities);
case CHROME:
return new ChromeDriver(capabilities);
case EDGE:
return new EdgeDriver(capabilities);
case INTERNET_EXPLORER:
return new InternetExplorerDriver(capabilities);
case SAFARI:
return new SafariDriver(capabilities);
default:
throw new AssertionError();
}
}
示例9: takeScreenShot
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
public String takeScreenShot(String name) throws IOException {
if (driver instanceof HtmlUnitDriver) {
oLog.fatal("HtmlUnitDriver Cannot take the ScreenShot");
return "";
}
File destDir = new File(ResourceHelper.getResourcePath("screenshots/")
+ DateTimeHelper.getCurrentDate());
if (!destDir.exists())
destDir.mkdir();
File destPath = new File(destDir.getAbsolutePath()
+ System.getProperty("file.separator") + name + ".jpg");
try {
FileUtils
.copyFile(((TakesScreenshot) driver)
.getScreenshotAs(OutputType.FILE), destPath);
} catch (IOException e) {
oLog.error(e);
throw e;
}
oLog.info(destPath.getAbsolutePath());
return destPath.getAbsolutePath();
}
示例10: getWebClient
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
private WebDriver getWebClient(int portForJSCoverProxy) {
Proxy proxy = new Proxy().setHttpProxy("localhost:" + portForJSCoverProxy);
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(CapabilityType.PROXY, proxy);
caps.setJavascriptEnabled(true);
caps.setBrowserName(BrowserType.HTMLUNIT);
return new HtmlUnitDriver(caps) {
@Override
protected WebClient modifyWebClient(WebClient client) {
client.setScriptPreProcessor((htmlPage, sourceCode, sourceName, lineNumber, htmlElement) -> {
if(removeJsSnippets != null && !removeJsSnippets.isEmpty()) {
for(String toReplace : removeJsSnippets) {
sourceCode = sourceCode.replace(toReplace, "");
}
return sourceCode;
} else {
return sourceCode;
}
});
return client;
}
};
}
示例11: setUp
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
@Before
public void setUp() throws Exception {
System.out.println("Fake Google OAuth2 server up at: " + fakeGoogleServer.getEndpoint());
System.out.println("Fake Hackpad server up at: " + fakeHackpadServer.getEndpoint());
System.out.println("AwesomeAgile web application up at: " + getEndpoint());
fakeGoogleServer.setClientId(CLIENT_ID);
fakeGoogleServer.setClientSecret(CLIENT_SECRET);
fakeGoogleServer.setRedirectUriPrefixes(
ImmutableList.of("http://localhost:" + port + "/"));
fakeGoogleServer.setPerson(createUser());
fakeHackpadServer.setClientId(HACKPAD_CLIENT_ID);
fakeHackpadServer.setClientSecret(HACKPAD_CLIENT_SECRET);
fakeHackpadServer.getHackpads().clear();
fakeHackpadServer.addHackpad(
new PadIdentity(DEFINITION_OF_READY_TEMPLATE_ID),
DEFINITION_OF_READY_CONTENTS);
fakeHackpadServer.addHackpad(
new PadIdentity(DEFINITION_OF_DONE_TEMPLATE_ID),
DEFINITION_OF_DONE_CONTENTS);
driver = new HtmlUnitDriver(BrowserVersion.CHROME);
driver.setJavascriptEnabled(true);
}
示例12: testDashboardReopenBrowser
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
@Test
public void testDashboardReopenBrowser() throws Exception {
LandingPage landingPage = PageFactory.initElements(driver, LandingPage.class);
landingPage.loginWithGoogle(getEndpoint());
assertThat(driver.getWindowHandles(), hasSize(1));
landingPage.createDefinitionOfReady();
HtmlUnitDriver driverTwo = new HtmlUnitDriver(BrowserVersion.CHROME);
driverTwo.setJavascriptEnabled(true);
// Open a completely new browser with no cookies
// Verify that view button is visible for this same user,
// and we're able to open his Definition of ready hackpad
LandingPage landingPageTwo = PageFactory.initElements(driverTwo, LandingPage.class);
landingPageTwo.loginWithGoogle(getEndpoint());
landingPageTwo.waitForDefinitionOfReady();
assertTrue(landingPageTwo.isDefinitionOfReadyViewable());
landingPageTwo.viewDefinitionOfReady();
String newWindow = Iterables.getFirst(Sets.difference(
driverTwo.getWindowHandles(),
ImmutableSet.of(driverTwo.getWindowHandle())), null);
driverTwo.switchTo().window(newWindow);
HackpadPage hackpadPage = PageFactory.initElements(driverTwo, HackpadPage.class);
assertEquals(DEFINITION_OF_READY_CONTENTS, hackpadPage.getContent());
}
示例13: assertDefinitionOfReadyContents
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
private static void assertDefinitionOfReadyContents(HtmlUnitDriver driver, String contents) {
// Refresh the dashboard for user #1; he should have definition of ready viewable
LandingPage landingPage = PageFactory.initElements(driver, LandingPage.class);
driver.navigate().refresh();
String currentWindow = driver.getWindowHandle();
landingPage.waitForDefinitionOfReady();
assertTrue(landingPage.isDefinitionOfReadyViewable());
landingPage.viewDefinitionOfReady();
// Verify definition of ready contents for user #1
String newWindow = Iterables.getFirst(Sets.difference(
driver.getWindowHandles(),
ImmutableSet.of(currentWindow)), null);
driver.switchTo().window(newWindow);
HackpadPage hackpadPage = PageFactory.initElements(driver, HackpadPage.class);
assertEquals(contents, hackpadPage.getContent());
}
示例14: main
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
public static void main(String[] args) throws Exception {
Executor executor = new Executor() {
@Override
public void execute(CrawlDatum datum, CrawlDatums next) throws Exception {
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);
driver.get(datum.url());
List<WebElement> elementList = driver.findElementsByCssSelector("h3.vrTitle a");
for(WebElement element:elementList){
System.out.println("title:"+element.getText());
}
}
};
//创建一个基于伯克利DB的DBManager
DBManager manager = new BerkeleyDBManager("crawl");
//创建一个Crawler需要有DBManager和Executor
Crawler crawler = new Crawler(manager, executor);
crawler.addSeed("https://www.sogou.com/web?query=%E6%B7%98%E5%AE%9D");
crawler.start(1);
}
示例15: buildWebDriver
import org.openqa.selenium.htmlunit.HtmlUnitDriver; //导入依赖的package包/类
@Test
public void buildWebDriver() throws Exception {
MockitoAnnotations.initMocks(this);
PowerMockito.whenNew(FirefoxDriver.class).withNoArguments().thenReturn(firefoxDriver);
WebDriver driver = WebDriverFactory.createDriver("firefox");
assertTrue(driver instanceof FirefoxDriver);
PowerMockito.whenNew(InternetExplorerDriver.class).withNoArguments().thenReturn(internetExplorerDriver);
driver = WebDriverFactory.createDriver("ie");
assertTrue(driver instanceof InternetExplorerDriver);
PowerMockito.whenNew(ChromeDriver.class).withNoArguments().thenReturn(chromerDriver);
driver = WebDriverFactory.createDriver("chrome");
assertTrue(driver instanceof ChromeDriver);
PowerMockito.whenNew(HtmlUnitDriver.class).withArguments(true).thenReturn(htmlUnitDriver);
driver = WebDriverFactory.createDriver("htmlunit");
assertTrue(driver instanceof HtmlUnitDriver);
PowerMockito.whenNew(RemoteWebDriver.class)
.withArguments(new URL("http://localhost:4444/wd/hub"), DesiredCapabilities.firefox())
.thenReturn(remoteWebDriver);
driver = WebDriverFactory.createDriver("remote:localhost:4444:firefox");
assertTrue(driver instanceof RemoteWebDriver);
}