|
| 1 | +package aquality.selenium.core.elements; |
| 2 | + |
| 3 | +import aquality.selenium.core.elements.interfaces.IElement; |
| 4 | +import aquality.selenium.core.elements.interfaces.IElementFactory; |
| 5 | +import aquality.selenium.core.elements.interfaces.IElementFinder; |
| 6 | +import aquality.selenium.core.elements.interfaces.IElementSupplier; |
| 7 | +import aquality.selenium.core.localization.ILocalizationManager; |
| 8 | +import aquality.selenium.core.logging.Logger; |
| 9 | +import aquality.selenium.core.waitings.IConditionalWait; |
| 10 | +import com.google.inject.Inject; |
| 11 | +import org.openqa.selenium.By; |
| 12 | +import org.openqa.selenium.By.ByXPath; |
| 13 | +import org.openqa.selenium.InvalidArgumentException; |
| 14 | +import org.openqa.selenium.WebElement; |
| 15 | +import org.openqa.selenium.support.pagefactory.ByChained; |
| 16 | + |
| 17 | +import java.lang.reflect.Constructor; |
| 18 | +import java.lang.reflect.InvocationTargetException; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.HashMap; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.TimeoutException; |
| 24 | + |
| 25 | +public class ElementFactory implements IElementFactory { |
| 26 | + |
| 27 | + private static final int XPATH_SUBSTRING_BEGIN_INDEX = 10; |
| 28 | + private static final long ZERO_TIMEOUT = 0L; |
| 29 | + |
| 30 | + private final IConditionalWait conditionalWait; |
| 31 | + private final IElementFinder elementFinder; |
| 32 | + private final ILocalizationManager localizationManager; |
| 33 | + |
| 34 | + @Inject |
| 35 | + public ElementFactory(IConditionalWait conditionalWait, IElementFinder elementFinder, ILocalizationManager localizationManager) { |
| 36 | + this.conditionalWait = conditionalWait; |
| 37 | + this.elementFinder = elementFinder; |
| 38 | + this.localizationManager = localizationManager; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public <T extends IElement> T getCustomElement(IElementSupplier<T> elementSupplier, By locator, String name, ElementState state) { |
| 43 | + return elementSupplier.get(locator, name, state); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public <T extends IElement> T getCustomElement(Class<T> clazz, By locator, String name, ElementState state) { |
| 48 | + IElementSupplier<T> elementSupplier = getDefaultElementSupplier(clazz); |
| 49 | + return getCustomElement(elementSupplier, locator, name, state); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public <T extends IElement> T findChildElement(IElement parentElement, By childLoc, String name, Class<T> clazz, ElementState state) { |
| 54 | + IElementSupplier<T> elementSupplier = getDefaultElementSupplier(clazz); |
| 55 | + return findChildElement(parentElement, childLoc, name, elementSupplier, state); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public <T extends IElement> T findChildElement(IElement parentElement, By childLoc, String name, IElementSupplier<T> supplier, ElementState state) { |
| 60 | + String childName = name == null ? "Child element of ".concat(parentElement.getName()) : name; |
| 61 | + By fullLocator = new ByChained(parentElement.getLocator(), childLoc); |
| 62 | + return supplier.get(fullLocator, childName, state); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public <T extends IElement> List<T> findElements(By locator, String name, IElementSupplier<T> supplier, |
| 67 | + ElementsCount count, ElementState state) { |
| 68 | + try { |
| 69 | + waitForElementsCount(locator, count, state); |
| 70 | + } catch (TimeoutException e) { |
| 71 | + throw new org.openqa.selenium.TimeoutException(e.getMessage()); |
| 72 | + } |
| 73 | + List<WebElement> webElements = elementFinder.findElements(locator, state, ZERO_TIMEOUT); |
| 74 | + String namePrefix = name == null ? "element" : name; |
| 75 | + List<T> list = new ArrayList<>(); |
| 76 | + for (int index = 1; index <= webElements.size(); index++) { |
| 77 | + WebElement webElement = webElements.get(index - 1); |
| 78 | + String currentName = String.format("%1$s %2$s", namePrefix, index); |
| 79 | + T element = supplier.get(generateXpathLocator(locator, webElement, index), currentName, state); |
| 80 | + list.add(element); |
| 81 | + } |
| 82 | + return list; |
| 83 | + } |
| 84 | + |
| 85 | + protected void waitForElementsCount(By locator, ElementsCount count, ElementState state) throws TimeoutException { |
| 86 | + switch (count) { |
| 87 | + case ZERO: |
| 88 | + conditionalWait.waitForTrue(() -> elementFinder.findElements(locator, state, ZERO_TIMEOUT).isEmpty(), |
| 89 | + localizationManager.getLocalizedMessage("loc.elements.found.but.should.not", |
| 90 | + locator.toString(), state.toString())); |
| 91 | + break; |
| 92 | + case MORE_THEN_ZERO: |
| 93 | + conditionalWait.waitForTrue(() -> !elementFinder.findElements(locator, state, ZERO_TIMEOUT).isEmpty(), |
| 94 | + localizationManager.getLocalizedMessage("loc.no.elements.found.by.locator", |
| 95 | + locator.toString(), state.toString())); |
| 96 | + break; |
| 97 | + case ANY: |
| 98 | + conditionalWait.waitFor(() -> elementFinder.findElements(locator, state, ZERO_TIMEOUT) != null); |
| 99 | + break; |
| 100 | + default: |
| 101 | + throw new IllegalArgumentException("No such expected value: ".concat(count.toString())); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public <T extends IElement> List<T> findElements(By locator, String name, Class<T> clazz, |
| 107 | + ElementsCount count, ElementState state) { |
| 108 | + IElementSupplier<T> elementSupplier = getDefaultElementSupplier(clazz); |
| 109 | + return findElements(locator, name, elementSupplier, count, state); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Generates xpath locator for target element. |
| 114 | + * |
| 115 | + * @param multipleElementsLocator locator used to find elements. Currently only {@link ByXPath} locators are supported. |
| 116 | + * @param webElement target element. |
| 117 | + * @param elementIndex index of target element. |
| 118 | + * @return target element's locator |
| 119 | + */ |
| 120 | + protected By generateXpathLocator(By multipleElementsLocator, WebElement webElement, int elementIndex) { |
| 121 | + Class supportedLocatorType = ByXPath.class; |
| 122 | + if (multipleElementsLocator.getClass().equals(supportedLocatorType)) { |
| 123 | + return By.xpath( |
| 124 | + String.format("(%1$s)[%2$s]", multipleElementsLocator.toString().substring(XPATH_SUBSTRING_BEGIN_INDEX), elementIndex)); |
| 125 | + } |
| 126 | + throw new InvalidArgumentException(String.format( |
| 127 | + "Cannot define unique baseLocator for element %1$s. Multiple elements' baseLocator %2$s is not %3$s, and is not supported yet", |
| 128 | + webElement.toString(), multipleElementsLocator, supportedLocatorType)); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Gets map between elements interfaces and their implementations. |
| 133 | + * Can be extended for custom elements with custom interfaces. |
| 134 | + * |
| 135 | + * @return Map where key is interface and value is its implementation. |
| 136 | + */ |
| 137 | + protected Map<Class<? extends IElement>, Class<? extends IElement>> getElementTypesMap() { |
| 138 | + return new HashMap<>(); |
| 139 | + } |
| 140 | + |
| 141 | + protected <T extends IElement> Class<T> resolveElementClass(Class<T> clazz) { |
| 142 | + if (clazz.isInterface() && !getElementTypesMap().containsKey(clazz)) { |
| 143 | + throw new IllegalArgumentException( |
| 144 | + String.format("Interface %1$s is not found in getElementTypesMap()", clazz)); |
| 145 | + } |
| 146 | + |
| 147 | + return clazz.isInterface() ? (Class<T>) getElementTypesMap().get(clazz) : clazz; |
| 148 | + } |
| 149 | + |
| 150 | + protected <T extends IElement> IElementSupplier<T> getDefaultElementSupplier(Class<T> clazz) { |
| 151 | + return (locator, name, state) -> { |
| 152 | + try { |
| 153 | + Constructor<T> ctor = resolveElementClass(clazz) |
| 154 | + .getDeclaredConstructor(By.class, String.class, ElementState.class); |
| 155 | + ctor.setAccessible(true); |
| 156 | + T instance = ctor.newInstance(locator, name, state); |
| 157 | + ctor.setAccessible(false); |
| 158 | + return instance; |
| 159 | + } catch (NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { |
| 160 | + Logger.getInstance().debug(e.getMessage()); |
| 161 | + throw new IllegalArgumentException("Something went wrong during element casting"); |
| 162 | + } |
| 163 | + }; |
| 164 | + } |
| 165 | +} |
0 commit comments