PageAction extends AbstractSeleniumAction { @Override protected void execute(SeleniumBrowser browser, TestContext context) { final WebPage pageToUse; if (StringUtils.hasText(type)) { try { pageToUse = (WebPage) Class.forName(context.replaceDynamicContentInString(type)).newInstance(); } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { throw new CitrusRuntimeException(String.format("Failed to access page type '%s'", context.replaceDynamicContentInString(type)), e); } } else { pageToUse = page; } PageFactory.initElements(browser.getWebDriver(), pageToUse); if (StringUtils.hasText(action)) { if (action.equals("validate") && (validator != null || pageToUse instanceof PageValidator)) { if (validator != null) { validator.validate(pageToUse, browser, context); } if (pageToUse instanceof PageValidator) { ((PageValidator) pageToUse).validate(pageToUse, browser, context); } } else { ReflectionUtils.doWithMethods(pageToUse.getClass(), method -> { if (method.getName().equals(action)) { if (method.getParameterCount() == 0 && arguments.size() == 0) { ReflectionUtils.invokeMethod(method, pageToUse); } else if (method.getParameterCount() == 1 && method.getParameters()[0].getParameterizedType().getTypeName().equals(TestContext.class.getName())) { ReflectionUtils.invokeMethod(method, pageToUse, context); } else if (method.getParameterCount() == arguments.size()) { ReflectionUtils.invokeMethod(method, pageToUse, context.resolveDynamicValuesInList(arguments).toArray()); } else if (method.getParameterCount() == arguments.size() + 1) { Object[] args = Arrays.copyOf(arguments.toArray(), arguments.size() + 1); args[arguments.size()] = context; ReflectionUtils.invokeMethod(method, pageToUse, context.resolveDynamicValuesInArray(args)); } else { throw new CitrusRuntimeException("Unsupported method signature for page action - not matching given arguments"); } } }); } } } PageAction(Builder builder); WebPage getPage(); String getAction(); PageValidator getValidator(); String getType(); List<String> getArguments();  }
@Test public void testExecutePageValidation() throws Exception { when(inputElement.getAttribute("value")).thenReturn("TestUser"); PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("validate") .page(new UserFormPage()) .build(); action.execute(context); }
@Test public void testExecutePageType() throws Exception { when(inputElement.getAttribute("value")).thenReturn("TestUser"); PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("validate") .type(UserFormPage.class.getName()) .build(); action.execute(context); }
@Test public void testExecutePageValidator() throws Exception { PageValidator validator = Mockito.mock(PageValidator.class); when(inputElement.getAttribute("value")).thenReturn("TestUser"); UserFormPage userForm = new UserFormPage(); PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("validate") .page(userForm) .validator(validator) .build(); action.execute(context); verify(validator).validate(userForm, seleniumBrowser, context); }
@Test public void testExecuteAction() throws Exception { PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("setUserName") .argument("Citrus") .page(new UserFormPage()) .build(); action.execute(context); verify(inputElement).clear(); verify(inputElement).sendKeys("Citrus"); }
@Test public void testExecuteActionWithArguments() throws Exception { when(webDriver.findElement(By.id("form"))).thenReturn(formElement); PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("submit") .page(new TestPage()) .build(); action.execute(context); action = new PageAction.Builder() .browser(seleniumBrowser) .action("submitWithContext") .page(new TestPage()) .build(); action.execute(context); action = new PageAction.Builder() .browser(seleniumBrowser) .action("submitWithArgument") .argument("ok") .page(new TestPage()) .build(); action.execute(context); action = new PageAction.Builder() .browser(seleniumBrowser) .action("submitWithArgumentAndContext") .argument("ok") .page(new TestPage()) .build(); action.execute(context); verify(formElement, times(4)).submit(); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Unsupported method signature for page action.*") public void testExecuteActionNotMatchingArguments() throws Exception { when(webDriver.findElement(By.id("form"))).thenReturn(formElement); PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("submit") .page(new TestPage()) .argument("Citrus") .build(); action.execute(context); verify(inputElement).clear(); verify(inputElement).sendKeys("Citrus"); verify(formElement).submit(); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testExecuteValidationFailed() throws Exception { PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("validate") .page(new UserFormPage()) .build(); action.execute(context); }
@Test(expectedExceptions = CitrusRuntimeException.class, expectedExceptionsMessageRegExp = "Failed to access page type.*") public void testInvalidPageType() throws Exception { PageAction action = new PageAction.Builder() .browser(seleniumBrowser) .action("validate") .type(UserFormPage.class.getPackage().getName() + ".UnknownPage") .build(); action.execute(context); }