1. 程式人生 > >HtmlUnit執行自定javascript指令碼並回調java方法

HtmlUnit執行自定javascript指令碼並回調java方法

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.LoggerFactory;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebWindow;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import net.sourceforge.htmlunit.corejs.javascript.Context;
import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
import net.sourceforge.htmlunit.corejs.javascript.ScriptableObject;
import net.sourceforge.htmlunit.corejs.javascript.annotations.JSConstructor;
import net.sourceforge.htmlunit.corejs.javascript.annotations.JSFunction;
import net.sourceforge.htmlunit.corejs.javascript.annotations.JSGetter;

public class TestHtmlUnitDriver {
	public static void main(String[] args) {
		HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME, true);
		// WebDriver driver = new HtmlUnitDriver(BrowserVersion.CHROME,true);
		LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
		loggerContext.getLogger("com.gargoylesoftware.htmlunit").setLevel(Level.valueOf("ERROR"));
		((HtmlUnitDriver) driver).setJavascriptEnabled(true);

		try {
			driver.get("https://www.baidu.com");
			(new WebDriverWait(driver, 10, 500)).until(new ExpectedCondition<Boolean>() {
				public Boolean apply(WebDriver d) {
					
					Context ctx = Context.getCurrentContext();
					//Scriptable scope = ctx.initStandardObjects();
					try {
						WebWindow window = (WebWindow) ReflectUtil.invokeMethod(d, "getCurrentWindow");
						Scriptable scope = window.getScriptableObject();
						ScriptableObject.defineClass(scope, Counter.class);
					} catch (Exception e) {
						e.printStackTrace();
					}
					//Scriptable testCounter = ctx.newObject(scope, "Counter");
					System.out.println(((JavascriptExecutor) driver).executeScript("return new Counter(123312).getCount()"));;
					Object element = ((JavascriptExecutor) driver)
							.executeScript("return document.getElementsByTagName(\"html\")");
					System.out.println(element);
					return element != null;
				}
			});

		} catch (Exception e) {
			e.printStackTrace();
		}
		driver.quit();
		// JavascriptExecutor javascript = (JavascriptExecutor) driver;
		// Object obj = javascript.executeScript("return
		// document.getElementsByTagName(\"html\")");
		// Object obj =
		// ((HtmlUnitDriver)driver).executeScript("document.getElementsByTagName(\"html\")");
		// System.out.println(obj);
	}

	public static class Counter extends ScriptableObject {
		private static final long serialVersionUID = 438270592527335642L;

		// The zero-argument constructor used by Rhino runtime to create instances
		public Counter() {
		}

		// @JSConstructor annotation defines the JavaScript constructor
		@JSConstructor
		public Counter(int a) {
			count = a;
		}

		// The class name is defined by the getClassName method
		@Override
		public String getClassName() {
			return "Counter";
		}

		// The method getCount defines the count property.
		@JSGetter
		@JSFunction
		public int getCount() {
			return count++;
		}

		// Methods can be defined the @JSFunction annotation.
		// Here we define resetCount for JavaScript.
		@JSFunction
		public void resetCount() {
			count = 0;
		}
		private int count;
	}
}