Search in sources :

Example 41 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project shiro by apache.

the class ContainerIntegrationIT method logIn.

@Test
public void logIn() throws FailingHttpStatusCodeException, MalformedURLException, IOException, InterruptedException {
    HtmlPage page = webClient.getPage(getBaseUri() + "login.jsp");
    HtmlForm form = page.getFormByName("loginform");
    form.<HtmlInput>getInputByName("username").setValueAttribute("root");
    form.<HtmlInput>getInputByName("password").setValueAttribute("secret");
    page = form.<HtmlInput>getInputByName("submit").click();
    // This'll throw an expection if not logged in
    page.getAnchorByHref("/logout");
}
Also used : HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) Test(org.junit.Test)

Example 42 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project spring-boot-quick by vector4wang.

the class BaiduCrawlerUrl method searchResult.

public static void searchResult(String search, String keyword, int page) throws IOException {
    client.getOptions().setJavaScriptEnabled(true);
    client.getOptions().setCssEnabled(false);
    client.getOptions().setThrowExceptionOnScriptError(false);
    while (true) {
        // 限制,如果数据采集错误,或者无限的时候,就限制100页
        if (page == 100) {
            break;
        }
        String url = "http://www.baidu.com/s?pn=" + (page - 1) * pageSize + "&wd=" + keyword;
        HtmlPage result = client.getPage(url);
        String contentAsString = result.getWebResponse().getContentAsString();
        try {
            parseContent(contentAsString);
        } catch (Exception e) {
            e.printStackTrace();
            break;
        }
        page++;
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) IOException(java.io.IOException) FailingHttpStatusCodeException(com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException)

Example 43 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project testcases by coheigea.

the class OIDCTest method loginToClientsPage.

// Runs as BeforeClass: Login to the OIDC Clients page + create a new client
private static void loginToClientsPage(String rpPort, String oidcPort, String idpPort) throws Exception {
    String url = "https://localhost:" + oidcPort + "/fediz-oidc/console/clients";
    String user = "alice";
    String password = "ecila";
    // Login to the client page successfully
    WebClient webClient = setupWebClient(user, password, idpPort);
    HtmlPage loginPage = login(url, webClient);
    final String bodyTextContent = loginPage.getBody().getTextContent();
    Assert.assertTrue(bodyTextContent.contains("Registered Clients"));
    String clientUrl = "https://localhost:" + rpPort + "/fedizdoubleit/auth/rp/complete";
    // Now try to register a new client
    HtmlPage registeredClientPage = registerNewClient(webClient, url, "consumer-id", clientUrl, clientUrl);
    String registeredClientPageBody = registeredClientPage.getBody().getTextContent();
    Assert.assertTrue(registeredClientPageBody.contains("Registered Clients"));
    Assert.assertTrue(registeredClientPageBody.contains("consumer-id"));
    HtmlTable table = registeredClientPage.getHtmlElementById("registered_clients");
    storedClientId = table.getCellAt(1, 1).asText().trim();
    Assert.assertNotNull(storedClientId);
    // Now get the Client Secret
    final HtmlPage clientPage = webClient.getPage(url + "/" + storedClientId);
    HtmlTable clientPageTable = clientPage.getHtmlElementById("client");
    storedClientSecret = clientPageTable.getCellAt(1, 2).asText().trim();
    Assert.assertNotNull(storedClientSecret);
    webClient.close();
}
Also used : HtmlTable(com.gargoylesoftware.htmlunit.html.HtmlTable) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 44 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project testcases by coheigea.

the class OIDCTest method registerNewClient.

private static HtmlPage registerNewClient(WebClient webClient, String url, String clientName, String redirectURI, String clientAudience) throws Exception {
    HtmlPage registerPage = webClient.getPage(url + "/register");
    final HtmlForm form = registerPage.getForms().get(0);
    // Set new client values
    final HtmlTextInput clientNameInput = form.getInputByName("client_name");
    clientNameInput.setValueAttribute(clientName);
    final HtmlSelect clientTypeSelect = form.getSelectByName("client_type");
    clientTypeSelect.setSelectedAttribute("confidential", true);
    final HtmlTextInput redirectURIInput = form.getInputByName("client_redirectURI");
    redirectURIInput.setValueAttribute(redirectURI);
    final HtmlTextInput clientAudienceURIInput = form.getInputByName("client_audience");
    clientAudienceURIInput.setValueAttribute(clientAudience);
    final HtmlButton button = form.getButtonByName("submit_button");
    return button.click();
}
Also used : HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlTextInput(com.gargoylesoftware.htmlunit.html.HtmlTextInput) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSelect(com.gargoylesoftware.htmlunit.html.HtmlSelect) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton)

Example 45 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project testcases by coheigea.

the class SAMLSSOTest method login.

private static String login(String url, String user, String password, String idpPort) throws IOException {
    final WebClient webClient = new WebClient();
    webClient.getOptions().setUseInsecureSSL(true);
    webClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", Integer.parseInt(idpPort)), new UsernamePasswordCredentials(user, password));
    webClient.getOptions().setJavaScriptEnabled(false);
    final HtmlPage idpPage = webClient.getPage(url);
    webClient.getOptions().setJavaScriptEnabled(true);
    Assert.assertEquals("IDP SignIn Response Form", idpPage.getTitleText());
    final HtmlForm form = idpPage.getFormByName("samlsigninresponseform");
    final HtmlSubmitInput button = form.getInputByName("_eventId_submit");
    final XmlPage rpPage = button.click();
    return rpPage.asXml();
}
Also used : HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlSubmitInput(com.gargoylesoftware.htmlunit.html.HtmlSubmitInput) AuthScope(org.apache.http.auth.AuthScope) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)159 Test (org.junit.Test)114 WebClient (com.gargoylesoftware.htmlunit.WebClient)51 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)25 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)23 HtmlSpan (com.gargoylesoftware.htmlunit.html.HtmlSpan)21 File (java.io.File)17 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)15 Matchers.containsString (org.hamcrest.Matchers.containsString)13 IOException (java.io.IOException)11 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)10 FreeStyleProject (hudson.model.FreeStyleProject)9 URL (java.net.URL)9 Page (com.gargoylesoftware.htmlunit.Page)8 HtmlTextInput (com.gargoylesoftware.htmlunit.html.HtmlTextInput)7 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)6 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)6 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)5 HtmlButton (com.gargoylesoftware.htmlunit.html.HtmlButton)5 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)5