How to use FluentInjector class of org.fluentlenium.core.inject package

Best FluentLenium code snippet using org.fluentlenium.core.inject.FluentInjector

Source:FluentDriver.java Github

copy

Full Screen

...16import org.fluentlenium.core.events.ComponentsEventsRegistry;17import org.fluentlenium.core.events.EventsRegistry;18import org.fluentlenium.core.inject.ContainerContext;19import org.fluentlenium.core.inject.DefaultContainerInstantiator;20import org.fluentlenium.core.inject.FluentInjector;21import org.fluentlenium.core.script.FluentJavascript;22import org.fluentlenium.core.search.Search;23import org.fluentlenium.core.search.SearchFilter;24import org.fluentlenium.core.wait.FluentWait;25import org.fluentlenium.utils.ImageUtils;26import org.fluentlenium.utils.UrlUtils;27import org.openqa.selenium.By;28import org.openqa.selenium.Capabilities;29import org.openqa.selenium.Cookie;30import org.openqa.selenium.HasCapabilities;31import org.openqa.selenium.JavascriptExecutor;32import org.openqa.selenium.OutputType;33import org.openqa.selenium.SearchContext;34import org.openqa.selenium.TakesScreenshot;35import org.openqa.selenium.UnhandledAlertException;36import org.openqa.selenium.WebDriver;37import org.openqa.selenium.WebDriverException;38import org.openqa.selenium.WebElement;39import org.openqa.selenium.WrapsDriver;40import org.openqa.selenium.WrapsElement;41import org.openqa.selenium.support.events.EventFiringWebDriver;42import java.io.File;43import java.io.IOException;44import java.io.PrintWriter;45import java.nio.file.Paths;46import java.util.Date;47import java.util.List;48import java.util.Set;49import java.util.concurrent.TimeUnit;50/**51 * Util Class which offers some shortcut to webdriver methods52 */53@SuppressWarnings("PMD.GodClass")54public class FluentDriver extends FluentControlImpl implements FluentControl { // NOPMD GodClass55 private final Configuration configuration;56 private final ComponentsManager componentsManager;57 private final EventsRegistry events;58 private final ComponentsEventsRegistry componentsEventsRegistry;59 private final FluentInjector fluentInjector;60 private final CssControl cssControl; // NOPMD UnusedPrivateField61 private final Search search;62 private final WebDriver driver;63 private final MouseActions mouseActions;64 private final KeyboardActions keyboardActions;65 private final WindowAction windowAction;66 /**67 * Wrap the driver into a Fluent driver.68 *69 * @param driver underlying selenium driver70 * @param configuration configuration71 * @param adapter adapter fluent control interface72 */73 public FluentDriver(WebDriver driver, Configuration configuration, FluentControl adapter) {74 super(adapter);75 this.configuration = configuration;76 componentsManager = new ComponentsManager(adapter);77 this.driver = driver;78 search = new Search(driver, this, componentsManager, adapter);79 if (driver instanceof EventFiringWebDriver) {80 events = new EventsRegistry(this);81 componentsEventsRegistry = new ComponentsEventsRegistry(events, componentsManager);82 } else {83 events = null;84 componentsEventsRegistry = null;85 }86 mouseActions = new MouseActions(driver);87 keyboardActions = new KeyboardActions(driver);88 fluentInjector = new FluentInjector(adapter, events, componentsManager, new DefaultContainerInstantiator(this));89 cssControl = new CssControlImpl(adapter, adapter);90 windowAction = new WindowAction(adapter, componentsManager.getInstantiator(), driver);91 configureDriver(); // NOPMD ConstructorCallsOverridableMethod92 }93 public Configuration getConfiguration() {94 return configuration;95 }96 private ComponentsManager getComponentsManager() {97 return componentsManager;98 }99 private FluentInjector getFluentInjector() {100 return fluentInjector;101 }102 private CssControl getCssControl() {103 return cssControl;104 }105 private void configureDriver() {106 if (getDriver() != null && getDriver().manage() != null && getDriver().manage().timeouts() != null) {107 if (configuration.getPageLoadTimeout() != null) {108 getDriver().manage().timeouts().pageLoadTimeout(configuration.getPageLoadTimeout(), TimeUnit.MILLISECONDS);109 }110 if (configuration.getImplicitlyWait() != null) {111 getDriver().manage().timeouts().implicitlyWait(configuration.getImplicitlyWait(), TimeUnit.MILLISECONDS);112 }113 if (configuration.getScriptTimeout() != null) {114 getDriver().manage().timeouts().setScriptTimeout(configuration.getScriptTimeout(), TimeUnit.MILLISECONDS);115 }116 }117 }118 @Override119 public void takeHtmlDump() {120 takeHtmlDump(new Date().getTime() + ".html");121 }122 @Override123 public void takeHtmlDump(String fileName) {124 File destFile = null;125 try {126 if (configuration.getHtmlDumpPath() == null) {127 destFile = new File(fileName);128 } else {129 destFile = Paths.get(configuration.getHtmlDumpPath(), fileName).toFile();130 }131 String html;132 synchronized (FluentDriver.class) {133 html = $("html").first().html();134 }135 FileUtils.write(destFile, html, "UTF-8");136 } catch (Exception e) {137 if (destFile == null) {138 destFile = new File(fileName);139 }140 try (PrintWriter printWriter = new PrintWriter(destFile, "UTF-8")) {141 printWriter.write("Can't dump HTML");142 printWriter.println();143 e.printStackTrace(printWriter);144 } catch (IOException e1) {145 throw new RuntimeException("error when dumping HTML", e); //NOPMD PreserveStackTrace146 }147 }148 }149 @Override150 public boolean canTakeScreenShot() {151 return getDriver() instanceof TakesScreenshot;152 }153 @Override154 public void takeScreenshot() {155 takeScreenshot(new Date().getTime() + ".png");156 }157 @Override158 public void takeScreenshot(String fileName) {159 if (!canTakeScreenShot()) {160 throw new WebDriverException("Current browser doesn't allow taking screenshot.");161 }162 byte[] screenshot = prepareScreenshot();163 persistScreenshot(fileName, screenshot);164 }165 private void persistScreenshot(String fileName, byte[] screenshot) {166 try {167 File destFile;168 if (configuration.getScreenshotPath() == null) {169 destFile = new File(fileName);170 } else {171 destFile = Paths.get(configuration.getScreenshotPath(), fileName).toFile();172 }173 FileUtils.writeByteArrayToFile(destFile, screenshot);174 } catch (IOException e) {175 throw new RuntimeException("Error when taking the screenshot", e);176 }177 }178 private byte[] prepareScreenshot() {179 byte[] screenshot;180 try {181 screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);182 } catch (UnhandledAlertException uae) {183 ImageUtils imageUtils = new ImageUtils(getDriver());184 screenshot = imageUtils.handleAlertAndTakeScreenshot();185 }186 return screenshot;187 }188 @Override189 public WebDriver getDriver() {190 return driver;191 }192 private Search getSearch() {193 return search;194 }195 @Override196 public EventsRegistry events() {197 if (events == null) {198 throw new IllegalStateException("An EventFiringWebDriver instance is required to use events. "199 + "You should set 'eventsEnabled' configuration property to 'true' "200 + "or override newWebDriver() to build an EventFiringWebDriver.");201 }202 return events;203 }204 @Override205 public MouseActions mouse() {206 return mouseActions;207 }208 @Override209 public KeyboardActions keyboard() {210 return keyboardActions;211 }212 @Override213 public WindowAction window() {214 return windowAction;215 }216 @Override217 public FluentWait await() {218 FluentWait fluentWait = new FluentWait(this);219 Long atMost = configuration.getAwaitAtMost();220 if (atMost != null) {221 fluentWait.atMost(atMost);222 }223 Long pollingEvery = configuration.getAwaitPollingEvery();224 if (pollingEvery != null) {225 fluentWait.pollingEvery(pollingEvery);226 }227 return fluentWait;228 }229 @Override230 public Set<Cookie> getCookies() {231 return getDriver().manage().getCookies();232 }233 @Override234 public Cookie getCookie(String name) {235 return getDriver().manage().getCookieNamed(name);236 }237 private String buildUrl(String url) {238 String currentUrl = getDriver().getCurrentUrl();239 String baseUrl = UrlUtils.sanitizeBaseUrl(getBaseUrl(), currentUrl);240 return UrlUtils.concat(baseUrl, url);241 }242 @Override243 public String url() {244 String baseUrl = buildUrl(null);245 String currentUrl = getDriver().getCurrentUrl();246 if (currentUrl != null && baseUrl != null && currentUrl.startsWith(baseUrl)) {247 currentUrl = currentUrl.substring(baseUrl.length());248 }249 return currentUrl;250 }251 @Override252 public String pageSource() {253 return getDriver().getPageSource();254 }255 @Override256 public <P extends FluentPage> P goTo(P page) {257 if (page == null) {258 throw new IllegalArgumentException("Page is mandatory");259 }260 page.go();261 return page;262 }263 @Override264 public void goTo(String url) {265 if (url == null) {266 throw new IllegalArgumentException("Url is mandatory");267 }268 getDriver().get(buildUrl(url));269 }270 @Override271 public void goToInNewTab(String url) {272 if (url == null) {273 throw new IllegalArgumentException("Url is mandatory");274 }275 String newTab;276 synchronized (getClass()) {277 Set<String> initialTabs = getDriver().getWindowHandles();278 executeScript("window.open('" + buildUrl(url) + "', '_blank');");279 Set<String> tabs = getDriver().getWindowHandles();280 tabs.removeAll(initialTabs);281 newTab = tabs.iterator().next();282 }283 getDriver().switchTo().window(newTab);284 }285 @Override286 public Capabilities capabilities() {287 WebDriver currentDriver = getDriver();288 Capabilities capabilities = currentDriver instanceof HasCapabilities289 ? ((HasCapabilities) currentDriver).getCapabilities()290 : null;291 while (currentDriver instanceof WrapsDriver && capabilities == null) {292 currentDriver = ((WrapsDriver) currentDriver).getWrappedDriver();293 capabilities = currentDriver instanceof HasCapabilities ? ((HasCapabilities) currentDriver).getCapabilities() : null;294 }295 return capabilities;296 }297 @Override298 public FluentJavascript executeScript(String script, Object... args) {299 return new FluentJavascript((JavascriptExecutor) getDriver(), false, script, args);300 }301 @Override302 public FluentJavascript executeAsyncScript(String script, Object... args) {303 return new FluentJavascript((JavascriptExecutor) getDriver(), true, script, args);304 }305 @Override306 public FluentList<FluentWebElement> find(String selector, SearchFilter... filters) {307 return getSearch().find(selector, filters);308 }309 @Override310 public FluentList<FluentWebElement> find(By locator, SearchFilter... filters) {311 return getSearch().find(locator, filters);312 }313 @Override314 public FluentList<FluentWebElement> find(SearchFilter... filters) {315 return getSearch().find(filters);316 }317 @Override318 public FluentList<FluentWebElement> find(List<WebElement> rawElements) {319 return getSearch().find(rawElements);320 }321 @Override322 public FluentList<FluentWebElement> $(List<WebElement> rawElements) {323 return getSearch().$(rawElements);324 }325 @Override326 public FluentWebElement el(WebElement rawElement) {327 return getSearch().el(rawElement);328 }329 @Override330 public void switchTo(FluentList<? extends FluentWebElement> elements) {331 switchTo(elements.first());332 }333 @Override334 public void switchTo(FluentWebElement element) {335 if (null == element || !"iframe".equals(element.tagName())) {336 getDriver().switchTo().defaultContent();337 } else {338 WebElement target = element.getElement();339 while (target instanceof WrapsElement && target != ((WrapsElement) target).getWrappedElement()) {340 target = ((WrapsElement) target).getWrappedElement();341 }342 getDriver().switchTo().frame(target);343 }344 }345 @Override346 public void switchTo() {347 switchTo((FluentWebElement) null);348 }349 @Override350 public void switchToDefault() {351 switchTo((FluentWebElement) null);352 }353 @Override354 public Alert alert() {355 return new AlertImpl(getDriver());356 }357 /**358 * Quit the underlying web driver and release fluent driver resources.359 */360 public void quit() {361 if (getDriver() != null) {362 getDriver().quit();363 }364 releaseFluent();365 }366 /**367 * Release fluent driver resources.368 */369 public void releaseFluent() {370 fluentInjector.release();371 if (componentsEventsRegistry != null) {372 componentsEventsRegistry.close();373 }374 }375 @Override376 public <L extends List<T>, T> L newComponentList(Class<L> listClass, Class<T> componentClass) {377 return getComponentsManager().newComponentList(listClass, componentClass);378 }379 @Override380 public <T> ComponentList asComponentList(Class<T> componentClass, Iterable<WebElement> elements) {381 return getComponentsManager().asComponentList(componentClass, elements);382 }383 @Override384 public <L extends List<T>, T> L newComponentList(Class<L> listClass, Class<T> componentClass, T... componentsList) {385 return getComponentsManager().newComponentList(listClass, componentClass, componentsList);386 }387 @Override388 public <T extends FluentWebElement> FluentList<T> asFluentList(Class<T> componentClass, Iterable<WebElement> elements) {389 return getComponentsManager().asFluentList(componentClass, elements);390 }391 @Override392 public boolean isComponentClass(Class<?> componentClass) {393 return getComponentsManager().isComponentClass(componentClass);394 }395 @Override396 public <T> ComponentList<T> asComponentList(Class<T> componentClass, List<WebElement> elements) {397 return getComponentsManager().asComponentList(componentClass, elements);398 }399 @Override400 public <T extends FluentWebElement> FluentList<T> asFluentList(Class<T> componentClass, WebElement... elements) {401 return getComponentsManager().asFluentList(componentClass, elements);402 }403 @Override404 public <T extends FluentWebElement> FluentList<T> newFluentList(Class<T> componentClass) {405 return getComponentsManager().newFluentList(componentClass);406 }407 @Override408 public FluentWebElement newFluent(WebElement element) {409 return getComponentsManager().newFluent(element);410 }411 @Override412 public boolean isComponentListClass(Class<? extends List<?>> componentListClass) {413 return getComponentsManager().isComponentListClass(componentListClass);414 }415 @Override416 public FluentList<FluentWebElement> asFluentList(WebElement... elements) {417 return getComponentsManager().asFluentList(elements);418 }419 @Override420 public FluentList<FluentWebElement> asFluentList(Iterable<WebElement> elements) {421 return getComponentsManager().asFluentList(elements);422 }423 @Override424 public <L extends List<T>, T> L asComponentList(Class<L> listClass, Class<T> componentClass, WebElement... elements) {425 return getComponentsManager().asComponentList(listClass, componentClass, elements);426 }427 @Override428 public <L extends List<T>, T> L asComponentList(Class<L> listClass, Class<T> componentClass, Iterable<WebElement> elements) {429 return getComponentsManager().asComponentList(listClass, componentClass, elements);430 }431 @Override432 public FluentList<FluentWebElement> asFluentList(List<WebElement> elements) {433 return getComponentsManager().asFluentList(elements);434 }435 @Override436 public <T extends FluentWebElement> FluentList<T> asFluentList(Class<T> componentClass, List<WebElement> elements) {437 return getComponentsManager().asFluentList(componentClass, elements);438 }439 @Override440 public <T> ComponentList<T> asComponentList(Class<T> componentClass, WebElement... elements) {441 return getComponentsManager().asComponentList(componentClass, elements);442 }443 @Override444 public <T> T newComponent(Class<T> componentClass, WebElement element) {445 return getComponentsManager().newComponent(componentClass, element);446 }447 @Override448 public <T> ComponentList<T> newComponentList(Class<T> componentClass, T... componentsList) {449 return getComponentsManager().newComponentList(componentClass, componentsList);450 }451 @Override452 public <T> ComponentList<T> newComponentList(Class<T> componentClass, List<T> componentsList) {453 return getComponentsManager().newComponentList(componentClass, componentsList);454 }455 @Override456 public <L extends List<T>, T> L newComponentList(Class<L> listClass, Class<T> componentClass, List<T> componentsList) {457 return getComponentsManager().newComponentList(listClass, componentClass, componentsList);458 }459 @Override460 public FluentList<FluentWebElement> newFluentList() {461 return getComponentsManager().newFluentList();462 }463 @Override464 public FluentList<FluentWebElement> newFluentList(List<FluentWebElement> elements) {465 return getComponentsManager().newFluentList(elements);466 }467 @Override468 public <T> ComponentList<T> newComponentList(Class<T> componentClass) {469 return getComponentsManager().newComponentList(componentClass);470 }471 @Override472 public FluentList<FluentWebElement> newFluentList(FluentWebElement... elements) {473 return getComponentsManager().newFluentList(elements);474 }475 @Override476 public <T extends FluentWebElement> FluentList<T> newFluentList(Class<T> componentClass, List<T> elements) {477 return getComponentsManager().newFluentList(componentClass, elements);478 }479 @Override480 public <T extends FluentWebElement> FluentList<T> newFluentList(Class<T> componentClass, T... elements) {481 return getComponentsManager().newFluentList(componentClass, elements);482 }483 @Override484 public <L extends List<T>, T> L asComponentList(Class<L> listClass, Class<T> componentClass, List<WebElement> elements) {485 return getComponentsManager().asComponentList(listClass, componentClass, elements);486 }487 @Override488 public ContainerContext inject(Object container) {489 return getFluentInjector().inject(container);490 }491 @Override492 public <T> T newInstance(Class<T> cls) {493 return getFluentInjector().newInstance(cls);494 }495 @Override496 public ContainerContext injectComponent(Object componentContainer, Object parentContainer, SearchContext searchContext) {497 return getFluentInjector().injectComponent(componentContainer, parentContainer, searchContext);498 }499 @Override500 public CssSupport css() {501 return getCssControl().css();502 }503}...

Full Screen

Full Screen

Source:FluentTestContainer.java Github

copy

Full Screen

...5import org.fluentlenium.adapter.SharedMutator;6import org.fluentlenium.core.annotation.Page;7import org.fluentlenium.core.components.ComponentsManager;8import org.fluentlenium.core.inject.DefaultContainerInstantiator;9import org.fluentlenium.core.inject.FluentInjector;10import java.util.Arrays;11import static java.util.Objects.isNull;12import static java.util.Objects.nonNull;13/**14 * Container class for {@link FluentCucumberTest}.15 * <p>16 * It uses Sinlgeton pattern based on enum to makes sure that all Cucumber steps17 */18public enum FluentTestContainer {19 /**20 * Instance of FluentTestContainer.21 */22 FLUENT_TEST;23 private ThreadLocal<FluentAdapter> fluentAdapter;24 private ThreadLocal<FluentControlContainer> controlContainer;25 private ThreadLocal<SharedMutator> sharedMutator;26 private ThreadLocal<FluentInjector> injector;27 private static Class<?> configClass;28 FluentTestContainer() {29 fluentAdapter = new ThreadLocal<>();30 controlContainer = new ThreadLocal<>();31 sharedMutator = new ThreadLocal<>();32 injector = new ThreadLocal<>();33 }34 /**35 * Returns single instance of adapter across all Cucumber steps.36 *37 * @return instance of fluent adapter38 */39 public FluentAdapter instance() {40 if (isNull(fluentAdapter.get())) {41 controlContainer.set(new DefaultFluentControlContainer());42 sharedMutator.set(new FluentCucumberSharedMutator());43 if (nonNull(configClass)) {44 fluentAdapter.set(new FluentCucumberTest(controlContainer.get(), configClass, sharedMutator.get()));45 } else {46 fluentAdapter.set(new FluentCucumberTest(controlContainer.get(), sharedMutator.get()));47 }48 injector.set(new FluentInjector(fluentAdapter.get(), null,49 new ComponentsManager(fluentAdapter.get()),50 new DefaultContainerInstantiator(fluentAdapter.get())));51 }52 return fluentAdapter.get();53 }54 /**55 * Reset instance of FluentAdapter stored in container.56 */57 public void reset() {58 sharedMutator.remove();59 controlContainer.remove();60 injector.remove();61 configClass = null;62 fluentAdapter.remove();63 }64 /**65 * Provide control container across different classes.66 *67 * @return control container instance.68 */69 protected FluentControlContainer getControlContainer() {70 if (fluentAdapter.get() == null) {71 instance();72 }73 return controlContainer.get();74 }75 /**76 * Sets config class - needed to enable annotation configuration.77 *78 * @param clazz class annotated with @RunWith(FluentCucumber.class)79 */80 public static void setConfigClass(Class clazz) {81 configClass = clazz;82 }83 /**84 * Returns used inside container SharedMutator85 *86 * @return SharedMutator instance87 */88 protected SharedMutator getSharedMutator() {89 return sharedMutator.get();90 }91 /**92 * Injector used in FluentObjectFactory for creating instances93 *94 * @return fluent injector without loaded full FluentControl context95 */96 public FluentInjector injector() {97 return injector.get();98 }99 /**100 * Creating new instances of pages.101 *102 * @param obj container obj which contains pages to initialize103 */104 public void instantiatePages(Object obj) {105 Arrays.stream(obj.getClass().getDeclaredFields())106 .filter(field -> field.isAnnotationPresent(Page.class))107 .forEach(field -> {108 try {109 Object instance = injector.get().newInstance(field.getType());110 field.setAccessible(true);...

Full Screen

Full Screen

Source:FluentInjectorListComponentTest.java Github

copy

Full Screen

...16import java.util.Arrays;17import java.util.List;18import static org.mockito.Mockito.when;19@RunWith(MockitoJUnitRunner.class)20public class FluentInjectorListComponentTest {21 @Mock22 private WebDriver webDriver;23 private FluentAdapter fluentAdapter;24 private FluentInjector injector;25 @Before26 public void before() {27 fluentAdapter = new FluentAdapter();28 fluentAdapter.initFluent(webDriver);29 injector = new FluentInjector(fluentAdapter, null, new ComponentsManager(fluentAdapter),30 new DefaultContainerInstantiator(fluentAdapter));31 }32 public static class Component {33 private final WebElement foundElement;34 private FluentWebElement element;35 public Component(WebElement webElement) {36 foundElement = webElement;37 }38 }39 public static class Container {40 private List<Component> components;41 }42 @Test43 public void testListComponent() {...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful