Search in sources :

Example 66 with HtmlPage

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

the class MoviesArquillianHtmlUnitTest method testShouldMakeSureWebappIsWorking.

@Test
public void testShouldMakeSureWebappIsWorking() throws Exception {
    final String url = "http://" + deploymentUrl.getHost() + ":" + deploymentUrl.getPort() + "/moviefun";
    WebClient webClient = new WebClient();
    HtmlPage page = webClient.getPage(url + "/setup.jsp");
    assertMoviesPresent(page);
    page = webClient.getPage(url + "/moviefun");
    assertMoviesPresent(page);
    webClient.closeAllWindows();
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 67 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project wechat by dllwh.

the class HtmlUnitHandler method crawl.

/**
 * ----------------------------------------------- [私有方法]
 */
@Override
public String crawl(String url, CrawlParameter crawlPara) {
    WebClient webClient = getWebClient(crawlPara);
    String reqtype = crawlPara.getReqmethod();
    Page page = null;
    String resource = "";
    try {
        if (StringUtils.isNotBlank(reqtype)) {
            WebRequest webRequest = new WebRequest(new URL(url));
            if ("post".equals(reqtype)) {
                webRequest.setHttpMethod(HttpMethod.POST);
            } else if ("get".equals(reqtype)) {
                webRequest.setHttpMethod(HttpMethod.GET);
            }
            Map<String, String> reqmap = crawlPara.getReqmap();
            if (MapUtils.isNotEmpty(reqmap)) {
                for (Entry<String, String> param : reqmap.entrySet()) {
                    webRequest.getRequestParameters().add(new NameValuePair(param.getKey(), param.getValue()));
                }
            }
            page = webClient.getPage(webRequest);
            resource = page.getWebResponse().getContentAsString();
        } else {
            page = webClient.getPage(url);
            resource = ((HtmlPage) page).asXml();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return resource;
}
Also used : NameValuePair(com.gargoylesoftware.htmlunit.util.NameValuePair) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL)

Example 68 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project ats-framework by Axway.

the class AbstractHtmlEngine method reloadFrames.

@PublicAtsApi
public void reloadFrames() {
    // real browsers reloads the frames automatically
    if (webDriver instanceof HtmlUnitDriver) {
        Field webClientField = null;
        boolean fieldAccessibleState = false;
        try {
            // Retrieve current WebClient instance (with the current page) from the Selenium WebDriver
            TargetLocator targetLocator = webDriver.switchTo();
            webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
            fieldAccessibleState = webClientField.isAccessible();
            webClientField.setAccessible(true);
            WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
            HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
            for (final FrameWindow frameWindow : page.getFrames()) {
                final BaseFrameElement frame = frameWindow.getFrameElement();
                // use == and not equals(...) to identify initial content (versus URL set to "about:blank")
                if (frame.getEnclosedPage().getWebResponse().getWebRequest().getUrl() == WebClient.URL_ABOUT_BLANK) {
                    String src = frame.getSrcAttribute();
                    if (src != null && !src.isEmpty()) {
                        final URL url;
                        try {
                            url = ((HtmlPage) frame.getEnclosedPage()).getFullyQualifiedUrl(src);
                        } catch (final MalformedURLException e) {
                            String message = "Invalid src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
                            final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
                            incorrectnessListener.notify(message, this);
                            return;
                        }
                        if (isAlreadyLoadedByAncestor(url, ((HtmlPage) frame.getEnclosedPage()))) {
                            String message = "Recursive src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
                            final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
                            incorrectnessListener.notify(message, this);
                            log.info("Frame already loaded: " + frame.toString());
                            return;
                        }
                        try {
                            final WebRequest request = new WebRequest(url);
                            request.setAdditionalHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9, text/*;q=0.7, */*;q=0.5");
                            if (frameWindow.getName() == null || frameWindow.getName().isEmpty()) {
                                frameWindow.setName("frame_" + page.getFrames().indexOf(frameWindow));
                            }
                            webClient.loadWebResponseInto(webClient.loadWebResponse(request), frameWindow);
                            log.info("Frame loaded: " + frame.toString());
                        } catch (IOException e) {
                            log.error("Error when getting content for " + frame.getTagName() + " with src=" + url, e);
                        }
                    }
                } else {
                    log.info("Frame already loaded: " + frame.toString());
                }
            }
        } catch (Exception e) {
            throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
        } finally {
            if (webClientField != null) {
                webClientField.setAccessible(fieldAccessibleState);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) IOException(java.io.IOException) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) Field(java.lang.reflect.Field) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) TargetLocator(org.openqa.selenium.WebDriver.TargetLocator) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 69 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project chuidiang-ejemplos by chuidiang.

the class JavascriptWithHtmlUnit method main.

public static void main(String[] args) throws Exception {
    WebClient webClient = new WebClient();
    HtmlPage page = webClient.getPage("file:files/JavascriptWithHtmlUnit.html");
    // Sale "hola", puesto que se ha ejecutado el codigo javascript de la pagina
    System.out.println(page.getElementById("unDiv").getTextContent());
    // Cambiamos el contenido usando funciones est�ndar de javascript en navegador
    page.executeJavaScript("document.getElementById(\"unDiv\").innerHTML=\"que tal?\"");
    System.out.println(page.getElementById("unDiv").getTextContent());
    // Cambiamos el contenido con una funcion javascript definida en la pagina.
    page.executeJavaScript("cambia(\"adios\")");
    System.out.println(page.getElementById("unDiv").getTextContent());
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 70 with HtmlPage

use of com.gargoylesoftware.htmlunit.html.HtmlPage in project htmlpublisher-plugin by jenkinsci.

the class HtmlFileNameTest method fileNameWithSpecialCharactersAndMultipleSlashes.

@Test
public void fileNameWithSpecialCharactersAndMultipleSlashes() throws Exception {
    final String content = "<html><head><title>test</title></head><body>Hello world!</body></html>";
    FreeStyleProject job = j.createFreeStyleProject();
    job.getBuildersList().add(new CreateFileBuilder("subdir/subdir2/#$&+,;= @.html", content));
    job.getPublishersList().add(new HtmlPublisher(Arrays.asList(new HtmlPublisherTarget("report-name", "", "subdir/subdir2/*.html", true, true, false))));
    job.save();
    j.buildAndAssertSuccess(job);
    JenkinsRule.WebClient client = j.createWebClient();
    assertEquals(content, client.getPage(job, "report-name/subdir/subdir2/%23%24%26%2B%2C%3B%3D%20%40.html").getWebResponse().getContentAsString());
    // published html page(s)
    HtmlPage page = client.getPage(job, "report-name");
    HtmlInlineFrame iframe = (HtmlInlineFrame) page.getElementById("myframe");
    assertEquals("subdir/subdir2/%23%24%26%2B%2C%3B%3D%20%40.html", iframe.getAttribute("src"));
    HtmlPage pageInIframe = (HtmlPage) iframe.getEnclosedPage();
    assertEquals("Hello world!", pageInIframe.getBody().asNormalizedText());
}
Also used : CreateFileBuilder(org.jvnet.hudson.test.CreateFileBuilder) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) JenkinsRule(org.jvnet.hudson.test.JenkinsRule) FreeStyleProject(hudson.model.FreeStyleProject) HtmlInlineFrame(com.gargoylesoftware.htmlunit.html.HtmlInlineFrame) Test(org.junit.Test)

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