@Override public void enrich(final SearchContext searchContext, Object target) { Collection<Field> fields = ReflectionHelper.getFieldsWithAnnotation(target.getClass(), JavaScript.class); for (Field field : fields) { GrapheneContext grapheneContext = searchContext == null ? null : ((GrapheneProxyInstance) searchContext).getGrapheneContext(); if (grapheneContext == null) { grapheneContext = GrapheneContext.getContextFor(ReflectionHelper.getQualifier(field.getAnnotations())); } if (!field.isAccessible()) { field.setAccessible(true); } try { field.set(target, JSInterfaceFactory.create(grapheneContext, field.getType())); } catch (Exception e) { throw new IllegalStateException( "Can't inject value to the field '" + field.getName() + "' declared in class '" + field.getDeclaringClass().getName() + "'", e); } } }
/** * Creates and inserts a new button to DOM, clicks on it and removes it. * * @param browser */ public static void performUniversalBlur(WebDriver browser) { // reuse FirefoxKeyboardWorkaround FirefoxKeyboardWorkaround fkw = JSInterfaceFactory.create(GrapheneContext.lastContext(), FirefoxKeyboardWorkaround.class); fkw.insertButtonWorkaround(); new Actions(browser).moveToElement(fkw.getButtonElement()).click().perform(); }
@Test public void testWithoutSources() { Document document = JSInterfaceFactory.create(context, Document.class); List<WebElement> elements = document.getElementsByTagName("html"); Assert.assertNotNull(elements); Assert.assertEquals(1, elements.size()); }
@Test public void test() { browser.navigate().to("http://127.0.0.1:4444"); Document document = JSInterfaceFactory.create(Document.class); List<WebElement> elements = document.getElementsByTagName("html"); assertNotNull(elements); assertEquals(1, elements.size()); }
/** * When using Firefox and body element is focused, focus on the page from url/address bar, * otherwise nothing happens. Workaround for Selenium issue <a * href="https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/7937">#7937</a> */ public static void performFirefoxKeyboardWorkaround(WebDriver browser) { if (browser instanceof FirefoxDriver) { FirefoxKeyboardWorkaround fkw = JSInterfaceFactory.create(GrapheneContext.lastContext(), FirefoxKeyboardWorkaround.class); // is body element focused and workaround was not invoked? if (!fkw.isInvoked() && fkw.getActiveElementTagName().equalsIgnoreCase("body")) { // insert the button element fkw.insertButtonWorkaround(); // click on the inserted button to focus on the page and remove the button from DOM (after // click) fkw.getButtonElement().click(); } } }
@Test public void testAbstractClass() { Document3 document = JSInterfaceFactory.create(context, Document3.class); WebElement element = unwrapWebElement(browser.findElement(By.tagName("h1"))); Assert.assertEquals(element, document.getHeader()); }
@Test(expected = IllegalArgumentException.class) public void testWithoutSourceAndWithInterfaceDependencies() { JSInterfaceFactory.create(context, Document2.class).getTitle(); }
@Test public void testWithInterfaceDependencies() { HelloWorld2 helloWorld = JSInterfaceFactory.create(context, HelloWorld2.class); Assert.assertEquals("Hello World!", helloWorld.hello()); }
@Test public void testWithSources() { HelloWorld helloWorld = JSInterfaceFactory.create(context, HelloWorld.class); Assert.assertEquals("Hello World!", helloWorld.hello()); }