本文整理汇总了Java中com.gargoylesoftware.htmlunit.html.HtmlPage类的典型用法代码示例。如果您正苦于以下问题:Java HtmlPage类的具体用法?Java HtmlPage怎么用?Java HtmlPage使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HtmlPage类属于com.gargoylesoftware.htmlunit.html包,在下文中一共展示了HtmlPage类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: searchDuck
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
public static List<String> searchDuck (String keyword) {
List<String> searchResults = new ArrayList<>();
try{
WebClient webClient = new WebClient(BrowserVersion.CHROME);
HtmlPage page = webClient.getPage("https://duckduckgo.com/html/?q=" + keyword);
List<HtmlAnchor> l = page.getByXPath("//a[@class='result__url']");
for(HtmlAnchor a: l) {
searchResults.add(a.getHrefAttribute());
}
}
catch(Exception e){
System.err.println(e);
}
return searchResults;
}
示例2: isAlreadyLoadedByAncestor
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
private boolean isAlreadyLoadedByAncestor(
final URL url,
HtmlPage page ) {
WebWindow window = page.getEnclosingWindow();
while (window != null) {
if (url.sameFile(window.getEnclosedPage().getWebResponse().getWebRequest().getUrl())) {
return true;
}
if (window == window.getParentWindow()) {
window = null;
} else {
window = window.getParentWindow();
}
}
return false;
}
示例3: mockLogin
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
public String mockLogin(URL url, String username, String password) {
try {
HtmlPage page1 = webClient.getPage(url.getURLValue());
// find the login form
HtmlForm form = page1.getForms().get(0);
// fill in the input
HtmlInput hi = form.getInputByName("commit");
HtmlTextInput textField = form.getInputByName("login");
HtmlPasswordInput pass = form.getInputByName("password");
textField.click();
textField.setValueAttribute(username);
pass.click();
pass.setValueAttribute(password);
// push the button
HtmlPage page2 = hi.click();
return page2.asXml();
} catch(IOException ioe) {
ioe.printStackTrace();
return null;
}
}
示例4: scrapingBaseExperiment
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
/**
* Scraping base experiment. To be used as starting point.
*
* First things first:
* <ol>
* <li>
* Make sure you're able to retrieve the page. Try again with JS disabled if
* the client crashes.
* </li>
* <li>
* Make sure you're actually getting what you want, and that there are no
* redirect-, proxy-, or login shenanigans or what not. Have a look at the
* retrieved text.
* </li>
* <li>
* Also consider disabling CSS to speed things up a bit.
* </li>
* </ol>
*/
@Test
public void scrapingBaseExperiment() {
assertOutputDirectoryExists();
String url = "http://www.google.com";
try (WebClient client = new WebClient(BrowserVersion.CHROME)) {
// client.getOptions().setCssEnabled(false);
// client.getOptions().setJavaScriptEnabled(false);
try {
HtmlPage page = client.getPage(url);
System.out.println("HtmlPage:");
System.out.println(page.asText());
System.out.print("\n");
} catch (IOException ex) {
System.err.println("WARNING: failed to visit: " + url);
ex.printStackTrace(System.err);
}
}
}
示例5: allowAccess
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
private HtmlPage allowAccess(WebClient webClient, HtmlPage allowAccessPage) throws IOException {
HtmlButton allowAccessButton = (HtmlButton) allowAccessPage.getElementById("submit_approve_access");
if (allowAccessButton == null) {
throw new RuntimeException("Cannot find allow access button in html page :\n" + allowAccessPage.asXml());
}
webClient.waitForBackgroundJavaScriptStartingBefore(WAIT_DELAY_MS);
// allowAccessButton.click() does not work because
// allowAccessButton.isVisible() is false
// for some reason (click() was working with htmlunit 2.23)
HtmlPage tokenPage = clickButtonIgnoringVisibility(allowAccessButton);
return tokenPage;
}
示例6: neverDispose
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
@Test
public void neverDispose() throws Throwable {
final IOException error = new IOException("to be thrown");
Disposable disposable = spy(new ThrowDisposable(error));
@SuppressWarnings("deprecation")
AsyncResourceDisposer.WorkItem item = disposer.disposeAndWait(disposable).get();
Set<AsyncResourceDisposer.WorkItem> remaining = disposer.getBacklog();
assertEquals(1, remaining.size());
assertThat(remaining.iterator().next(), equalTo(item));
assertEquals(error, ((Disposable.State.Thrown) item.getLastState()).getCause());
verify(disposable).dispose();
assertThat(disposer.getBacklog(), not(IsEmptyCollection.<AsyncResourceDisposer.WorkItem>empty()));
int itemId = item.getId();
HtmlPage page = j.createWebClient().goTo(disposer.getUrl());
page = page.getFormByName("stop-tracking-" + itemId).getInputByName("submit").click();
assertThat(page.getWebResponse().getContentAsString(), containsString(disposer.getDisplayName())); // Redirected back
assertThat(disposer.getBacklog(), emptyCollectionOf(AsyncResourceDisposer.WorkItem.class));
}
示例7: shouldShowPetIndexPage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
@Test
public void shouldShowPetIndexPage() throws Exception {
HtmlPage ownerIndexPage = getPage("/pets");
assertEquals(13.0, ownerIndexPage.getByXPath("count(//div[@class='card'])").get(0));
String content = ownerIndexPage.asText();
assertTrue(content.contains("Leo"));
assertTrue(content.contains("Owned by: George Franklin"));
assertTrue(content.contains("Birthday: 2010-09-07"));
assertTrue(content.contains("Type: cat"));
List viewEditDeleteLinks = ownerIndexPage.getByXPath("//div[@class='card'][1]//@href");
assertEquals("/owners/1", ((DomAttr) viewEditDeleteLinks.get(0)).getValue());
assertEquals("/pets/1", ((DomAttr) viewEditDeleteLinks.get(1)).getValue());
assertEquals("/pets/1/edit", ((DomAttr) viewEditDeleteLinks.get(2)).getValue());
assertEquals("/pets/1/delete", ((DomAttr) viewEditDeleteLinks.get(3)).getValue());
}
示例8: loginIfNecessary
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
public void loginIfNecessary() {
if (isSessionOut()) {
try {
RespWithTokenJSON respWithTokenJSON = loginBySpecialPost();
String token = null;
if (respWithTokenJSON == null || (token = respWithTokenJSON.getToken()) == null) {
throw new LoginFailedException("Failed to login " + MP_WEIXIN_LOGIN
+ "; No expected json with token responsed.");
}
HtmlPage homePage = this.webClient.getPage(MP_WEIXIN_BASE + respWithTokenJSON.getRedirect_url());
setCurrentPage(homePage);
updateIdentityUnit(token);
} catch (LoginFailedException e) {
throw e;
} catch (Exception ex) {
LOGGER.error("Request Login Page Error.", ex);
}
}
}
示例9: testConversation
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
@Test
public void testConversation() throws Exception {
HtmlPage page;
Iterator<HtmlElement> it;
page = webClient.getPage(webUrl + "resources/converse/start");
it = page.getDocumentElement().getHtmlElementsByTagName("p").iterator();
final String secret = it.next().asText();
System.out.println(secret);
page = webClient.getPage(webUrl + "resources/converse/" +
page.getDocumentElement().getHtmlElementsByTagName("a")
.iterator().next().getAttribute("href"));
it = page.getDocumentElement().getHtmlElementsByTagName("p").iterator();
assertTrue(secret.equals(it.next().asText()));
page = webClient.getPage(webUrl + "resources/converse/stop");
it = page.getDocumentElement().getHtmlElementsByTagName("p").iterator();
assertFalse(secret.equals(it.next().asText()));
}
示例10: testFormOk
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
/**
* Retrieve a form and submit it making sure the CSRF hidden field is present
*
* @throws Exception an error occurs or validation fails.
*/
@Test
public void testFormOk() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");
HtmlForm form = (HtmlForm) page1.getDocumentElement().getHtmlElementsByTagName("form").get(0);
// Check hidden input field
HtmlElement input = form.getHtmlElementsByTagName("input").get(1);
assertTrue(input.getAttribute("type").equals("hidden"));
assertTrue(input.getAttribute("name").equals(CSRF_PARAM));
assertTrue(input.hasAttribute("value")); // token
// Submit form
HtmlSubmitInput button = (HtmlSubmitInput) form.getHtmlElementsByTagName("input").get(0);
HtmlPage page2 = button.click();
Iterator<HtmlElement> it = page2.getDocumentElement().getHtmlElementsByTagName("h1").iterator();
assertTrue(it.next().asText().contains("CSRF Protection OK"));
}
示例11: testFormFail
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
/**
* Retrieves a form, removes CSRF hidden field and attempts to submit. Should
* result in a 403 error.
*
* @throws Exception an error occurs or validation fails.
*/
@Test
public void testFormFail() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");
HtmlForm form = (HtmlForm) page1.getDocumentElement().getHtmlElementsByTagName("form").get(0);
// Remove hidden input field to cause a CSRF validation failure
HtmlElement input = form.getHtmlElementsByTagName("input").get(1);
form.removeChild(input);
// Submit form - should fail
HtmlSubmitInput button = (HtmlSubmitInput) form.getHtmlElementsByTagName("input").get(0);
try {
button.click();
fail("CSRF validation should have failed!");
} catch (FailingHttpStatusCodeException e) {
// falls through
}
}
示例12: testFormHeaderOk
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
/**
* Checks that CSRF validation works if token sent as header instead of
* form field.
*
* @throws Exception an error occurs or validation fails.
*/
@Test
public void testFormHeaderOk() throws Exception {
HtmlPage page1 = webClient.getPage(webUrl + "resources/csrf");
// Check response and CSRF header
WebResponse res = page1.getWebResponse();
assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
assertNotNull(res.getResponseHeaderValue(CSRF_HEADER));
WebRequest req = new WebRequest(new URL(webUrl + "resources/csrf"));
req.setHttpMethod(HttpMethod.POST);
req.setAdditionalHeader(CSRF_HEADER, res.getResponseHeaderValue(CSRF_HEADER));
res = webClient.loadWebResponse(req);
assertEquals(Response.Status.OK.getStatusCode(), res.getStatusCode());
}
示例13: setAttribute
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
public static boolean setAttribute(HtmlPage htmlPage, String xpath,
String key, String value) {
boolean flag = true;
try {
HtmlElement element = htmlPage.getFirstByXPath(xpath);
if (element != null) {
element.click();
element.setAttribute(key, "");
element.setAttribute(key, value);
} else {
logger.error("setAttribute element is null xpath " + xpath);
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
flag = false;
}
return flag;
}
示例14: getEverything
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
private Map<String, String> getEverything(final Account a, final HtmlPage page) {
Map<String, String> values = new HashMap<>();
try {
AccountXpaths xpaths = a.getAccountXpaths();
values.put("messages", xpathUtils.checkXpathListForString(xpaths.getMessages(), page));
values.put("alerts", xpathUtils.checkXpathListForString(xpaths.getAlerts(), page));
values.put("posts", xpathUtils.checkXpathListForString(xpaths.getPosts(), page));
values.put("ratings", xpathUtils.checkXpathListForString(xpaths.getRatings(), page));
return values;
} catch (Exception e) {
e.printStackTrace();
}
return values;
}
示例15: testGetWithCorrectCredentials
import com.gargoylesoftware.htmlunit.html.HtmlPage; //导入依赖的package包/类
@Test
public void testGetWithCorrectCredentials() throws Exception {
logger.info("start get request with correct credentials");
loginForm.getInputByName("j_username").setValueAttribute("u1");
loginForm.getInputByName("j_password").setValueAttribute("p1");
HtmlSubmitInput submitButton = loginForm.getInputByName("submitButton");
HtmlPage page2 = submitButton.click();
assertEquals("Form-based Security - Success", page2.getTitleText());
}
开发者ID:PacktPublishing,项目名称:Mastering-Java-EE-Development-with-WildFly,代码行数:10,代码来源:SecureFormTestCase.java