/** * Wraps the {@link WebDriver} instance provided by {@link Drone} in a proxy and sets the driver into context */ @Override public WebDriver enhance(WebDriver driver, Class<? extends Annotation> qualifier) { GrapheneContext grapheneContext = GrapheneContext.setContextFor(configuration.get(), driver, qualifier); Class<?>[] interfaces = GrapheneProxyUtil.getInterfaces(driver.getClass()); return grapheneContext.getWebDriver(interfaces); }
/** * Wraps the {@link WebDriver} instance provided by {@link Drone} in a proxy and sets the driver into context */ @Override public WebDriver enhance(WebDriver driver, Class<? extends Annotation> qualifier) { GrapheneContext grapheneContext = GrapheneContext.setContextFor(configuration.get(), driver, qualifier); Class<?>[] interfaces = GrapheneProxyUtil.getInterfaces(driver.getClass()); return grapheneContext.getWebDriver(interfaces); }
@Before public void before() throws Throwable { Answer invoke = new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return ((InvocationContext) invocation.getArguments()[0]).invoke(); } }; when(interceptor1.intercept(Mockito.any(InvocationContext.class))).thenAnswer(invoke); when(interceptor2.intercept(Mockito.any(InvocationContext.class))).thenAnswer(invoke); context = GrapheneContext.setContextFor(new GrapheneConfiguration(), driver, Default.class); }
@Before public void prepareTest() { // given MockitoAnnotations.initMocks(this); instance = JSInterfaceFactory.create(GrapheneContext.setContextFor(new GrapheneConfiguration(), executor, Default.class), TestingInterface.class); when(executor.executeScript(Mockito.anyString())).then(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { return answer; } }); when(executor.executeScript("return true;")).thenReturn(true); }
@Before public void prepareTest() { // given MockitoAnnotations.initMocks(this); instance = JSInterfaceFactory.create(GrapheneContext.setContextFor(new GrapheneConfiguration(), executor, Default.class), TestingInterface.class); when(executor.executeScript("return true;")).thenReturn(true); }
@Before public final void setUp() { driver = mock(HtmlUnitDriver.class, withSettings().extraInterfaces(HasInputDevices.class)); mouse = mock(Mouse.class); keyboard = mock(Keyboard.class); webElement = mock(HtmlUnitWebElement.class); GrapheneContext.setContextFor(new GrapheneConfiguration(), driver, Default.class); GrapheneRuntime.pushInstance(new DefaultGrapheneRuntime()); when(((HasInputDevices) driver).getMouse()).thenReturn(mouse); when(((HasInputDevices) driver).getKeyboard()).thenReturn(keyboard); }
@Test public void test_execution_with_named_method() { // given TestingDriverStub executor = spy(new TestingDriverStub()); when(executor.executeScript("return true;")).thenReturn(true); // when TestingInterface instance = JSInterfaceFactory.create(GrapheneContext.setContextFor(new GrapheneConfiguration(), executor, Default.class), TestingInterface.class); instance.namedMethod(); // then verify(executor, times(1)).executeScript(invocation("TestingInterface", "anotherMethodName")); }
@Test public void test_execution() { // given TestingDriverStub executor = spy(new TestingDriverStub()); when(executor.executeScript("return true;")).thenReturn(true); // when TestingInterface instance = JSInterfaceFactory.create(GrapheneContext.setContextFor(new GrapheneConfiguration(), executor, Default.class), TestingInterface.class); instance.method(); // then verify(executor, times(1)).executeScript(invocation("TestingInterface", "method")); }
@Test public void test_execution_with_base() { // given TestingDriverStub executor = spy(new TestingDriverStub()); when(executor.executeScript("return true;")).thenReturn(true); // when TestingInterfaceWithBase instance = JSInterfaceFactory.create(GrapheneContext.setContextFor(new GrapheneConfiguration(), executor, Default.class), TestingInterfaceWithBase.class); instance.method(); // then verify(executor, times(1)).executeScript(invocation("base", "method")); } }
@Test public void generated_webelement_implements_WrapsElement_interface() { TestPage page = new TestPage(); getGrapheneEnricher().enrich(page); assertTrue(page.element instanceof WrapsElement); GrapheneContext.setContextFor(new GrapheneConfiguration(), driver, Default.class); when(driver.findElement(Mockito.any(By.class))).thenReturn(element); WebElement wrappedElement = ((WrapsElement) page.element).getWrappedElement(); GrapheneContext.removeContextFor(Default.class); assertThat(wrappedElement, is(instanceOf(GrapheneElement.class))); assertEquals(element, ((GrapheneElement)wrappedElement).getWrappedElement()); }
@Before public void prepareServiceLoader() throws NoSuchFieldException, IllegalArgumentException, IllegalAccessException { grapheneEnricher = new GrapheneEnricher(); Instance<GrapheneConfiguration> configuration = new Instance() { @Override public Object get() { return new GrapheneConfiguration(); } }; webElementEnricher = new WebElementEnricher(configuration); pageObjectEnricher = new PageObjectEnricher(); pageFragmentEnricher = new PageFragmentEnricher(configuration); when(serviceLoaderInstance.get()).thenReturn(serviceLoader); when(serviceLoader.all(TestEnricher.class)).thenReturn(Arrays.asList(grapheneEnricher)); when(serviceLoader.all(SearchContextTestEnricher.class)).thenReturn(Arrays.asList(webElementEnricher, pageObjectEnricher, pageFragmentEnricher)); for (Object o: Arrays.asList(grapheneEnricher, webElementEnricher, pageObjectEnricher, pageFragmentEnricher)) { Field serviceLoaderField; if (o instanceof SearchContextTestEnricher) { serviceLoaderField = AbstractSearchContextEnricher.class.getDeclaredField("serviceLoader"); } else { serviceLoaderField = o.getClass().getDeclaredField("serviceLoader"); } if (!serviceLoaderField.isAccessible()) { serviceLoaderField.setAccessible(true); } serviceLoaderField.set(o, serviceLoaderInstance); } GrapheneContext.setContextFor(new GrapheneConfiguration(), browser, Default.class); GrapheneRuntime.pushInstance(new DefaultGrapheneRuntime()); }
GrapheneContext.setContextFor(new GrapheneConfiguration(), driver, Default.class); GrapheneRuntime.pushInstance(new DefaultGrapheneRuntime());