Search in sources :

Example 56 with Page

use of com.gargoylesoftware.htmlunit.Page in project Payara by payara.

the class MicroProfileRestClientFaultToleranceIT method testRetry.

@Test
public void testRetry() throws Exception {
    Page page = webClient.getPage(base + "/api/client");
    Assert.assertEquals(HttpServletResponse.SC_OK, page.getWebResponse().getStatusCode());
}
Also used : Page(com.gargoylesoftware.htmlunit.Page) Test(org.junit.Test)

Example 57 with Page

use of com.gargoylesoftware.htmlunit.Page in project support-core-plugin by jenkinsci.

the class SupportActionTest method takeSnapshotAndMakeSureSomethingHappens.

/*
     * Integration test that simulates the user action of clicking the button to generate the bundle.
     * <p>
     * If any warning is reported to j.u.l logger, treat that as a sign of failure, because
     * support-core plugin works darn hard to try to generate something in the presence of failing
     * {@link Component} impls.
     */
@Test
public void takeSnapshotAndMakeSureSomethingHappens() throws Exception {
    j.createSlave("agent1", "test", null).getComputer().connect(false).get();
    j.createSlave("agent2", "test", null).getComputer().connect(false).get();
    RingBufferLogHandler checker = new RingBufferLogHandler();
    Logger logger = Logger.getLogger(SupportPlugin.class.getPackage().getName());
    logger.addHandler(checker);
    try {
        WebClient wc = j.createWebClient();
        HtmlPage p = wc.goTo(root.getUrlName());
        HtmlForm form = p.getFormByName("bundle-contents");
        HtmlButton submit = (HtmlButton) form.getElementsByTagName("button").get(0);
        Page zip = submit.click();
        File zipFile = File.createTempFile("test", "zip");
        IOUtils.copy(zip.getWebResponse().getContentAsStream(), Files.newOutputStream(zipFile.toPath()));
        ZipFile z = new ZipFile(zipFile);
        // check the presence of files
        // TODO: emit some log entries and see if it gets captured here
        assertNotNull(z.getEntry("about.md"));
        assertNotNull(z.getEntry("nodes.md"));
        assertNotNull(z.getEntry("nodes/master/thread-dump.txt"));
        if (SystemPlatform.LINUX == SystemPlatform.current()) {
            List<String> files = Arrays.asList("proc/swaps.txt", "proc/cpuinfo.txt", "proc/mounts.txt", "proc/system-uptime.txt", "proc/net/rpc/nfs.txt", "proc/net/rpc/nfsd.txt", "proc/meminfo.txt", "proc/self/status.txt", "proc/self/cmdline", "proc/self/environ", "proc/self/limits.txt", "proc/self/mountstats.txt", "sysctl.txt", "dmesg.txt", "userid.txt", "dmi.txt");
            for (String file : files) {
                assertNotNull(file + " was not found in the bundle", z.getEntry("nodes/master/" + file));
            }
        }
    } finally {
        logger.removeHandler(checker);
        for (LogRecord r : checker.getView()) {
            if (r.getLevel().intValue() >= Level.WARNING.intValue()) {
                Throwable thrown = r.getThrown();
                if (thrown != null)
                    thrown.printStackTrace(System.err);
                fail(r.getMessage());
            }
        }
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Matchers.containsString(org.hamcrest.Matchers.containsString) RingBufferLogHandler(hudson.util.RingBufferLogHandler) Logger(java.util.logging.Logger) WebClient(org.jvnet.hudson.test.JenkinsRule.WebClient) HtmlButton(com.gargoylesoftware.htmlunit.html.HtmlButton) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) ZipFile(java.util.zip.ZipFile) LogRecord(java.util.logging.LogRecord) ZipFile(java.util.zip.ZipFile) File(java.io.File) Test(org.junit.Test)

Example 58 with Page

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

the class HiddenHtmlElement method clickAndDownloadFile.

/**
 * Click the element and download file
 */
protected void clickAndDownloadFile() {
    WebWindow currentWindow = null;
    Field currentWindowField = null;
    boolean fieldAccessibleState = false;
    try {
        currentWindowField = htmlUnitDriver.getClass().getDeclaredField("currentWindow");
        fieldAccessibleState = currentWindowField.isAccessible();
        currentWindowField.setAccessible(true);
        currentWindow = (WebWindow) currentWindowField.get(htmlUnitDriver);
    } catch (Exception e) {
        throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
    } finally {
        if (currentWindowField != null) {
            currentWindowField.setAccessible(fieldAccessibleState);
        }
    }
    String elementXPath = properties.getProperty("xpath");
    // find element and download the file
    HtmlPage page = (HtmlPage) currentWindow.getEnclosedPage();
    List<?> foundElementsList = page.getByXPath(elementXPath);
    if (foundElementsList != null && !foundElementsList.isEmpty()) {
        InputStream in = null;
        FileOutputStream fos = null;
        try {
            com.gargoylesoftware.htmlunit.html.HtmlElement element = (com.gargoylesoftware.htmlunit.html.HtmlElement) foundElementsList.get(0);
            // Use generic Page. Exact page type returned depends on the MIME type set in response header
            Page result = element.click();
            String fileName = null;
            String contentDisposition = result.getWebResponse().getResponseHeaderValue("Content-Disposition");
            if (contentDisposition != null) {
                Matcher m = contentDispositionPattern.matcher(contentDisposition);
                if (m.matches()) {
                    fileName = m.group(1);
                    log.debug("Download file name extracted from the 'Content-Disposition' header is " + fileName);
                }
            }
            if (fileName == null) {
                String url = result.getWebResponse().getWebRequest().getUrl().getFile().trim();
                Matcher m = urlFileNamePattern.matcher(url);
                if (m.matches()) {
                    fileName = m.group(1);
                    log.debug("Download file name extracted from the request URL is " + fileName);
                } else {
                    fileName = String.valueOf(new Date().getTime()) + ".bin";
                    log.debug("Downloaded file name constructed the current timestamp is " + fileName);
                }
            }
            in = result.getWebResponse().getContentAsStream();
            String fileAbsPath = UiEngineConfigurator.getInstance().getBrowserDownloadDir() + fileName;
            fos = new FileOutputStream(new File(fileAbsPath), false);
            byte[] buff = new byte[BUFFER_LENGTH];
            int len;
            while ((len = in.read(buff)) != -1) {
                fos.write(buff, 0, len);
            }
            fos.flush();
            log.info("Downloaded file: " + fileAbsPath);
        } catch (IOException e) {
            throw new SeleniumOperationException("Error downloading file", e);
        } finally {
            IoUtils.closeStream(fos);
            IoUtils.closeStream(in);
        }
    } else {
        throw new ElementNotFoundException("Can't find element by XPath: " + elementXPath);
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) HtmlElement(com.axway.ats.uiengine.elements.html.HtmlElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) IOException(java.io.IOException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) IOException(java.io.IOException) Date(java.util.Date) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Field(java.lang.reflect.Field) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 59 with Page

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

the class HiddenHtmlPrompt method clickOk.

@Override
@PublicAtsApi
public void clickOk(final String promptValue) {
    isProcessed = false;
    webClient.setPromptHandler(new PromptHandler() {

        @Override
        public String handlePrompt(Page currentPage, String promptText, String defaultValue) {
            isProcessed = true;
            return promptValue;
        }
    });
}
Also used : Page(com.gargoylesoftware.htmlunit.Page) PromptHandler(com.gargoylesoftware.htmlunit.PromptHandler) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 60 with Page

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

the class HiddenHtmlPrompt method clickCancel.

@Override
@PublicAtsApi
public void clickCancel() {
    isProcessed = false;
    webClient.setPromptHandler(new PromptHandler() {

        @Override
        public String handlePrompt(Page currentPage, String promptText, String defaultValue) {
            isProcessed = true;
            return null;
        }
    });
}
Also used : Page(com.gargoylesoftware.htmlunit.Page) PromptHandler(com.gargoylesoftware.htmlunit.PromptHandler) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

Page (com.gargoylesoftware.htmlunit.Page)62 Test (org.junit.Test)39 WebClient (com.gargoylesoftware.htmlunit.WebClient)33 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)15 PublicAtsApi (com.axway.ats.common.PublicAtsApi)9 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)5 File (java.io.File)5 IOException (java.io.IOException)5 URL (java.net.URL)5 JenkinsRule (org.jvnet.hudson.test.JenkinsRule)5 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)4 ConfirmHandler (com.gargoylesoftware.htmlunit.ConfirmHandler)4 Map (java.util.Map)4 JSONObject (net.sf.json.JSONObject)4 RsaJsonWebKey (org.jose4j.jwk.RsaJsonWebKey)4 JsonWebSignature (org.jose4j.jws.JsonWebSignature)4 JwtClaims (org.jose4j.jwt.JwtClaims)4 JwtConsumer (org.jose4j.jwt.consumer.JwtConsumer)4 JwtConsumerBuilder (org.jose4j.jwt.consumer.JwtConsumerBuilder)4 JsonWebStructure (org.jose4j.jwx.JsonWebStructure)4