Java Code Examples for com.gargoylesoftware.htmlunit.BrowserVersion#CHROME

The following examples show how to use com.gargoylesoftware.htmlunit.BrowserVersion#CHROME . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PageFunctionTest.java    From tool.accelerate.core with Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
// TODO: This method of testing does not work for angular, need to find an alternative method of testing
public void techFormTest() {
    final WebClient webClient = new WebClient(BrowserVersion.CHROME);
    HtmlPage page;
    String port = System.getProperty("liberty.test.port");
    try {
        page = webClient.getPage("http://localhost:" + port + "/start/");
        DomElement techForm = page.getElementById("techTable");
        DomElement formBody = techForm.getFirstElementChild();
        int count = formBody.getChildElementCount();
        // We expect there to be more than one child element, otherwise the 
        // javascript has not created the tech table properly.
        assertTrue("Expected more than one element in the tech table, instead found " + count, count > 1);
    } catch (Exception e){
        org.junit.Assert.fail("Caught exception: " + e.getCause().toString());
    } finally {
        webClient.close();
    }
}
 
Example 2
Source File: HostExtractor.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * The entry point.
 * @param args optional proxy hostname and port
 * @throws Exception if an error occurs
 */
public static void main(final String[] args) throws Exception {
    final Set<String> set = new HashSet<>();
    try (WebClient webClient = new WebClient(BrowserVersion.CHROME)) {
        if (args.length > 1) {
            final ProxyConfig proxyConfig = new ProxyConfig(args[0], Integer.parseInt(args[1]));
            proxyConfig.addHostsToProxyBypass("localhost");
            webClient.getOptions().setProxyConfig(proxyConfig);
        }
        fillMDNWebAPI(webClient, set);
        fillMDNJavaScriptGlobalObjects(webClient, set);
        final String testRoot = "src/test/java/";
        ensure(new File(testRoot + HostClassNameTest.class.getName().replace('.', '/') + ".java"), set);
    }
}
 
Example 3
Source File: JavaScriptConfigurationTest.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * See issue 1890.
 *
 * @throws Exception if the test fails
 */
@Test
public void original() throws Exception {
    final BrowserVersion browserVersion = BrowserVersion.CHROME;

    test(browserVersion);
}
 
Example 4
Source File: BrowserVersionClassRunner.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void setAlertsStandards(final WebTestCase testCase, final Method method) {
    final AlertsStandards alerts = method.getAnnotation(AlertsStandards.class);
    if (alerts != null) {
        String[] expectedAlerts = NO_ALERTS_DEFINED;
        if (isDefined(alerts.value())) {
            expectedAlerts = alerts.value();
        }
        else {
            if (browserVersion_ == BrowserVersion.INTERNET_EXPLORER) {
                expectedAlerts = firstDefined(alerts.IE(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX_60) {
                expectedAlerts = firstDefined(alerts.FF60(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX_68) {
                expectedAlerts = firstDefined(alerts.FF68(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX) {
                expectedAlerts = firstDefined(alerts.FF(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.CHROME) {
                expectedAlerts = firstDefined(alerts.CHROME(), alerts.DEFAULT());
            }
        }
        testCase.setExpectedAlerts(expectedAlerts);
    }
    else {
        setAlerts(testCase, method);
    }
}
 
Example 5
Source File: HtmlUnitDownloader.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
public HtmlUnitDownloader() {

        this.webClient = new WebClient(BrowserVersion.CHROME);
        this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        this.webClient.getOptions().setThrowExceptionOnScriptError(false);
        this.webClient.getOptions().setRedirectEnabled(false);
        this.webClient.getOptions().setCssEnabled(false);
        this.webClient.setJavaScriptTimeout(1000);
        this.webClient.getOptions().setJavaScriptEnabled(true);
        this.webClient.setAjaxController(new NicelyResynchronizingAjaxController());//设置支持AJAX
    }
 
Example 6
Source File: htmlunitTest.java    From crawler-jsoup-maven with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
    
    // 屏蔽HtmlUnit等系统 log
    LogFactory.getFactory().setAttribute("org.apache.commons.logging.Log","org.apache.commons.logging.impl.NoOpLog");
    java.util.logging.Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF);
    java.util.logging.Logger.getLogger("org.apache.http.client").setLevel(Level.OFF);
    
    String url = "https://www.newsmth.net/nForum/#!section/Estate";
    System.out.println("Loading page now-----------------------------------------------: "+url);
    
    /* HtmlUnit 模拟浏览器 */
    WebClient webClient = new WebClient(BrowserVersion.CHROME);
    webClient.getOptions().setJavaScriptEnabled(true);              // 启用JS解释器,默认为true  
    webClient.getOptions().setCssEnabled(false);                    // 禁用css支持  
    webClient.getOptions().setThrowExceptionOnScriptError(false);   // js运行错误时,是否抛出异常
    webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
    webClient.getOptions().setTimeout(10 * 1000);                   // 设置连接超时时间
    HtmlPage page = webClient.getPage(url);
    webClient.waitForBackgroundJavaScript(30 * 1000);               // 等待js后台执行30秒

    String pageAsXml = page.asXml();
    
    /* Jsoup解析处理 */
    // Document doc = Jsoup.parse(pageAsXml, "https://bluetata.com/");
    Document doc = Jsoup.parse(pageAsXml);  
    //Elements pngs = doc.select("img[src$=.png]");                   // 获取所有图片元素集
    
    Elements eles = doc.select("td.title_1");
    // 其他操作
    System.out.println(eles.toString());
}
 
Example 7
Source File: HtmlUnitDownloder.java    From gecco-htmlunit with MIT License 5 votes vote down vote up
public HtmlUnitDownloder() {
	this.webClient = new WebClient(BrowserVersion.CHROME);
	this.webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
	this.webClient.getOptions().setThrowExceptionOnScriptError(false);
	this.webClient.getOptions().setRedirectEnabled(false);
	this.webClient.getOptions().setCssEnabled(false);
	this.webClient.setJavaScriptTimeout(1000);
	//this.webClient.setJavaScriptErrorListener(new GeccoJavaScriptErrorListener());
}
 
Example 8
Source File: FileUploadITests.java    From Spring with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() {
	this.driver = new HtmlUnitDriver(BrowserVersion.CHROME);
}
 
Example 9
Source File: BrowserVersionClassRunner.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private void setAlerts(final WebTestCase testCase, final Method method) {
    final Alerts alerts = method.getAnnotation(Alerts.class);
    String[] expectedAlerts = {};
    if (alerts != null) {
        expectedAlerts = NO_ALERTS_DEFINED;
        if (isDefined(alerts.value())) {
            expectedAlerts = alerts.value();
        }
        else {
            if (browserVersion_ == BrowserVersion.INTERNET_EXPLORER) {
                expectedAlerts = firstDefined(alerts.IE(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX_60) {
                expectedAlerts = firstDefined(alerts.FF60(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX_68) {
                expectedAlerts = firstDefined(alerts.FF68(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX) {
                expectedAlerts = firstDefined(alerts.FF(), alerts.DEFAULT());
            }
            else if (browserVersion_ == BrowserVersion.CHROME) {
                expectedAlerts = firstDefined(alerts.CHROME(), alerts.DEFAULT());
            }
        }
    }

    if (isRealBrowser()) {
        final BuggyWebDriver buggyWebDriver = method.getAnnotation(BuggyWebDriver.class);
        if (buggyWebDriver != null) {
            if (isDefined(buggyWebDriver.value())) {
                expectedAlerts = buggyWebDriver.value();
            }
            else {
                if (browserVersion_ == BrowserVersion.INTERNET_EXPLORER) {
                    expectedAlerts = firstDefinedOrGiven(expectedAlerts,
                                        buggyWebDriver.IE(), buggyWebDriver.DEFAULT());
                }
                else if (browserVersion_ == BrowserVersion.FIREFOX_60) {
                    expectedAlerts = firstDefinedOrGiven(expectedAlerts,
                                        buggyWebDriver.FF60(), buggyWebDriver.DEFAULT());
                }
                else if (browserVersion_ == BrowserVersion.FIREFOX_68) {
                    expectedAlerts = firstDefinedOrGiven(expectedAlerts,
                                        buggyWebDriver.FF68(), buggyWebDriver.DEFAULT());
                }
                else if (browserVersion_ == BrowserVersion.FIREFOX) {
                    expectedAlerts = firstDefinedOrGiven(expectedAlerts,
                                        buggyWebDriver.FF(), buggyWebDriver.DEFAULT());
                }
                else if (browserVersion_ == BrowserVersion.CHROME) {
                    expectedAlerts = firstDefinedOrGiven(expectedAlerts,
                                        buggyWebDriver.CHROME(), buggyWebDriver.DEFAULT());
                }
            }
        }
    }
    else {
        final HtmlUnitNYI htmlUnitNYI = method.getAnnotation(HtmlUnitNYI.class);
        if (htmlUnitNYI != null) {
            if (browserVersion_ == BrowserVersion.INTERNET_EXPLORER) {
                expectedAlerts = firstDefinedOrGiven(expectedAlerts, htmlUnitNYI.IE());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX_60) {
                expectedAlerts = firstDefinedOrGiven(expectedAlerts, htmlUnitNYI.FF60());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX_68) {
                expectedAlerts = firstDefinedOrGiven(expectedAlerts, htmlUnitNYI.FF68());
            }
            else if (browserVersion_ == BrowserVersion.FIREFOX) {
                expectedAlerts = firstDefinedOrGiven(expectedAlerts, htmlUnitNYI.FF());
            }
            else if (browserVersion_ == BrowserVersion.CHROME) {
                expectedAlerts = firstDefinedOrGiven(expectedAlerts, htmlUnitNYI.CHROME());
            }
        }
    }

    testCase.setExpectedAlerts(expectedAlerts);
}
 
Example 10
Source File: GSWorker.java    From slr-toolkit with Eclipse Public License 1.0 4 votes vote down vote up
public GSWorker(PrintWriter output, String asQ, String asEpq, String asOq, String asEq, String asOcct,
		String asSauthors, String asPublication, String asYlo, String asYhi) {
	webClient = new WebClient(BrowserVersion.CHROME);
	webClient.getOptions().setJavaScriptEnabled(true);
	webClient.setAjaxController(new NicelyResynchronizingAjaxController());
	webClient.setAjaxController(new AjaxController() {
		@Override
		public boolean processSynchron(HtmlPage page, WebRequest request, boolean async) {
			return true;
		}
	});
	webClient.getOptions().setScreenHeight(768);
	webClient.getOptions().setScreenWidth(1024);
	webClient.getCookieManager().setCookiesEnabled(true);

	base = scholar + "scholar?";
	try {
		if (asQ == null) {
			asQ = "";
		}
		base += "as_q=" + URLEncoder.encode(asQ.replace(" ", "+"), ENCODING) + "&";

		if (asEpq == null) {
			asEpq = "";
		}
		base += "as_epq=" + URLEncoder.encode(asEpq.replace(" ", "+"), ENCODING) + "&";

		if (asOq == null) {
			asOq = "";
		}
		base += "as_oq=" + URLEncoder.encode(asOq.replace(" ", "+"), ENCODING) + "&";

		if (asEq == null) {
			asEq = "";
		}
		base += "as_eq=" + URLEncoder.encode(asEq.replace(" ", "+"), ENCODING) + "&";

		if (asOcct == null) {
			asOcct = "";
		}
		base += "as_occt=" + URLEncoder.encode(asOcct.replace(" ", "+"), ENCODING) + "&";

		if (asSauthors == null) {
			asSauthors = "";
		}
		base += "as_sauthors=" + URLEncoder.encode(asSauthors.replace(" ", "+"), ENCODING) + "&";

		if (asPublication == null) {
			asPublication = "";
		}
		base += "as_publication=" + URLEncoder.encode(asPublication.replace(" ", "+"), ENCODING) + "&";

		if (asYlo == null) {
			asYlo = "";
		}
		base += "as_ylo=" + URLEncoder.encode(asYlo.replace(" ", "+"), ENCODING) + "&";

		if (asYhi == null) {
			asYhi = "";
		}
		base += "as_yhi=" + URLEncoder.encode(asYhi.replace(" ", "+"), ENCODING) + "&";
	} catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
	base += "hl=de&as_sdt=0%2C5";

	this.out = output;
	out.write("@Comment { Base-URL: " + base + " }\n\n");
	out.flush();
}
 
Example 11
Source File: WebDriverCreateTaskITests.java    From spring4-sandbox with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
	driver = new HtmlUnitDriver(BrowserVersion.CHROME);
}