Search in sources :

Example 1 with WebClient

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

the class InplantedTest method testWeb.

@Test
public void testWeb() throws Exception {
    System.out.println("test web");
    File f = new File(System.getProperty("basedir"));
    f = new File(f, "target");
    f = new File(f, "test-classes");
    ScatteredArchive.Builder builder = new ScatteredArchive.Builder("hello", f);
    builder.addClassPath(f.toURI().toURL());
    builder.resources(f);
    ScatteredArchive war = builder.buildWar();
    System.out.println("War content");
    Enumeration<String> contents = war.entries();
    while (contents.hasMoreElements()) {
        System.out.println(contents.nextElement());
    }
    Port http = server.createPort(8080);
    ContainerBuilder b = server.createConfig(ContainerBuilder.Type.web);
    server.addContainer(b);
    EmbeddedWebContainer embedded = (EmbeddedWebContainer) b.create(server);
    embedded.bind(http, "http");
    DeployCommandParameters dp = new DeployCommandParameters(f);
    String appName = server.getDeployer().deploy(war, dp);
    WebClient webClient = new WebClient();
    Page page = webClient.getPage("http://localhost:8080/test-classes/hello");
    System.out.println("Got response " + page.getWebResponse().getContentAsString());
    Assert.assertTrue("Servlet returned wrong content", page.getWebResponse().getContentAsString().startsWith("Hello World"));
    server.getDeployer().undeploy(appName, null);
}
Also used : DeployCommandParameters(org.glassfish.api.deployment.DeployCommandParameters) EmbeddedWebContainer(org.glassfish.api.embedded.web.EmbeddedWebContainer) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) File(java.io.File) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Example 2 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project archiva by apache.

the class WebdriverUtility method newWebDriver.

public static WebDriver newWebDriver(String seleniumBrowser, String seleniumHost, int seleniumPort, boolean seleniumRemote) {
    log.info("WebDriver {}, {}, {}, {}", seleniumBrowser, seleniumHost, seleniumPort, seleniumRemote);
    if (seleniumRemote && StringUtils.isEmpty(seleniumHost)) {
        throw new IllegalArgumentException("seleniumHost must be set, when seleniumRemote=true");
    }
    try {
        if (StringUtils.contains(seleniumBrowser, "chrome")) {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("start-maximized");
            if (seleniumRemote) {
                DesiredCapabilities capabilities = DesiredCapabilities.chrome();
                capabilities.setCapability(ChromeOptions.CAPABILITY, options);
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"), capabilities);
            } else {
                return new ChromeDriver(options);
            }
        }
        if (StringUtils.contains(seleniumBrowser, "safari")) {
            if (seleniumRemote) {
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"), DesiredCapabilities.safari());
            } else {
                return new SafariDriver();
            }
        }
        if (StringUtils.contains(seleniumBrowser, "iexplore")) {
            if (seleniumRemote) {
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"), DesiredCapabilities.internetExplorer());
            } else {
                new InternetExplorerDriver();
            }
        }
        if (StringUtils.contains(seleniumBrowser, "firefox")) {
            if (seleniumRemote) {
                return new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"), DesiredCapabilities.firefox());
            } else {
                return new FirefoxDriver();
            }
        }
        DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
        capabilities.setJavascriptEnabled(true);
        capabilities.setVersion("firefox-52");
        WebDriver driver;
        if (seleniumRemote) {
            driver = new RemoteWebDriver(new URL("http://" + seleniumHost + ":" + seleniumPort + "/wd/hub"), capabilities);
        } else {
            driver = new HtmlUnitDriver(capabilities) {

                @Override
                protected WebClient modifyWebClient(WebClient client) {
                    client.getOptions().setThrowExceptionOnFailingStatusCode(false);
                    client.getOptions().setThrowExceptionOnScriptError(false);
                    client.getOptions().setCssEnabled(true);
                    return client;
                }
            };
        }
        return driver;
    } catch (MalformedURLException e) {
        throw new RuntimeException("Initializion of remote driver failed");
    }
}
Also used : WebDriver(org.openqa.selenium.WebDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) MalformedURLException(java.net.MalformedURLException) InternetExplorerDriver(org.openqa.selenium.ie.InternetExplorerDriver) RemoteWebDriver(org.openqa.selenium.remote.RemoteWebDriver) DesiredCapabilities(org.openqa.selenium.remote.DesiredCapabilities) WebClient(com.gargoylesoftware.htmlunit.WebClient) URL(java.net.URL) SafariDriver(org.openqa.selenium.safari.SafariDriver) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) ChromeOptions(org.openqa.selenium.chrome.ChromeOptions) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver)

Example 3 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project archiva by apache.

the class AbstractRepositoryServletProxiedTestCase method assertServerSetupCorrectly.

protected void assertServerSetupCorrectly(RemoteRepoInfo remoteRepo) throws Exception {
    WebClient client = newClient();
    int status = client.getPage(remoteRepo.url).getWebResponse().getStatusCode();
    assertThat(status).isEqualTo(HttpServletResponse.SC_OK);
}
Also used : WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 4 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project JSCover by tntim96.

the class HtmlUnitMergeTest method saveUnloadedJS.

private void saveUnloadedJS() throws IOException {
    WebClient webClient = new WebClient();
    HtmlPage page = webClient.getPage("http://localhost:9001/jscoverage.html");
    storeReport(webClient, page);
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient)

Example 5 with WebClient

use of com.gargoylesoftware.htmlunit.WebClient in project JSCover by tntim96.

the class HtmlUnitMergeTest method shouldMergeUnloadedButtonSavedAndJSSavedReports.

@Test
public void shouldMergeUnloadedButtonSavedAndJSSavedReports() throws Exception {
    File jsonFile = new File(reportDir + "/jscoverage.json");
    if (jsonFile.exists())
        jsonFile.delete();
    saveUnloadedJS();
    saveReportByJavaScript();
    saveReportByButton();
    String json = ioUtils.toString(jsonFile);
    assertThat(json, containsString("/root.js"));
    assertThat(json, containsString("/level1/level2/level2.js"));
    String url = "file:///" + new File(reportDir + "/jscoverage.html").getAbsolutePath();
    WebClient webClient = new WebClient();
    HtmlPage page = webClient.getPage(url);
    webClient.waitForBackgroundJavaScript(1000);
    assertEquals("53%", page.getElementById("summaryTotal").getTextContent());
    assertEquals("33%", page.getElementById("branchSummaryTotal").getTextContent());
    assertEquals("50%", page.getElementById("functionSummaryTotal").getTextContent());
    verifyCoverage(page, "/root.js", "80%", "50%", "100%");
    verifyCoverage(page, "/root-empty.js", "N/A", "N/A", "N/A");
    verifyCoverage(page, "/level1/level1.js", "75%", "50%", "N/A");
    verifyCoverage(page, "/level1/level2/level2.js", "0%", "0%", "0%");
    verifyCoverage(page, "/level1/level2/level2-empty.js", "N/A", "N/A", "N/A");
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) WebClient(com.gargoylesoftware.htmlunit.WebClient) Test(org.junit.Test)

Aggregations

WebClient (com.gargoylesoftware.htmlunit.WebClient)182 Test (org.junit.Test)110 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)55 Page (com.gargoylesoftware.htmlunit.Page)33 TextPage (com.gargoylesoftware.htmlunit.TextPage)21 Before (org.junit.Before)20 HtmlSubmitInput (com.gargoylesoftware.htmlunit.html.HtmlSubmitInput)17 URL (java.net.URL)14 JsonObject (javax.json.JsonObject)13 RunAsClient (org.jboss.arquillian.container.test.api.RunAsClient)13 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)12 SpecAssertion (org.jboss.test.audit.annotations.SpecAssertion)11 HtmlSpan (com.gargoylesoftware.htmlunit.html.HtmlSpan)10 ReadContext (com.jayway.jsonpath.ReadContext)10 File (java.io.File)9 IOException (java.io.IOException)8 NicelyResynchronizingAjaxController (com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController)7 Test (org.junit.jupiter.api.Test)7 HtmlForm (com.gargoylesoftware.htmlunit.html.HtmlForm)6 DefaultCredentialsProvider (com.gargoylesoftware.htmlunit.DefaultCredentialsProvider)5