@DataProvider(name = "locatorDP") public static Iterator<Object[]> testData() { ArrayList<Object[]> data = new ArrayList<Object[]>(); data.add(new Object[]{"id=eleId", ById.class}); data.add(new Object[]{"name=eleName", ByName.class}); data.add(new Object[]{"css=#eleId.className", ByCssSelector.class}); data.add(new Object[]{"tagName=div", ByTagName.class}); data.add(new Object[]{"link=Link Text", ByLinkText.class}); data.add(new Object[]{"partialLink=Link Text", ByPartialLinkText.class}); data.add(new Object[]{"['css=#qa','name=eleName']", ByAny.class}); // self descriptive data.add(new Object[]{"{'locator' : 'id=eleId'; 'desc' : 'locate element by id'}", ById.class}); data.add(new Object[]{ "{'locator' : 'name=eleName'; 'desc' : 'locate element by name'}", ByName.class}); data.add(new Object[]{ "{'locator' : 'css=#eleId.className'; 'desc' : 'locate element by css'}", ByCssSelector.class}); data.add(new Object[]{ "{'locator' : ['css=#qa','name=eleName']; 'desc' : 'locate element by css'}", ByAny.class}); data.add(new Object[]{"xpath=//*[text()='Albany-Rensselaer, NY (ALB)']", ByXPath.class}); return data.iterator(); }
@Override public WebElement locateElement() { // the class was initialized with an index so use it if (index >= 0) { List<WebElement> elements = locateElements(); if (index < elements.size()) { return locateElements().get(index); } else { throw new NoSuchElementException(String.format("Unable to locate element using %s and %s", by, index)); } } WebElement element; if (isWithinIFrame()) { ((BaseElement) iframeElement).switchFocusToIFrame(); } else { switchFocusFromIFrame(); } if (hoverElement != null && hoverElement.isDisplayed()) hoverElement.hover(); if (parentElement != null) { By parentBy = by; if (by instanceof ByXPath) { parentBy = getByForParentElement(by); } element = parentElement.locateElement().findElement(parentBy); } else { element = driver.findElement(by); } if (autoScrollIntoView) { scrollElement(element); } return element; }
public List<WebElement> locateElements() { List<WebElement> elements; if (isWithinIFrame()) { ((BaseElement) iframeElement).switchFocusToIFrame(); } else { switchFocusFromIFrame(); } if (hoverElement != null && hoverElement.isDisplayed()) hoverElement.hover(); if (parentElement != null) { By parentBy; if (by instanceof ByXPath) { parentBy = getByForParentElement(by); } else { parentBy = by; } elements = getListWebElements(() -> parentElement.locateElement().findElements(parentBy)); } else { elements = getListWebElements(() -> driver.findElements(by)); } if (autoScrollIntoView && !elements.isEmpty()) { scrollElement(elements.get(0)); } return elements; }
public static By getByForParentElement(By by) { if (by instanceof ByXPath) { String xpath = by.toString().replace("By.xpath: ", "").replaceFirst("^.?//", ".//"); return By.xpath(xpath); } return by; }
@Test public void shouldRegisterWhenFlattenedLocatorFileIsLoaded() throws URISyntaxException { File locatorFile = new File(LocatorRegistryTest.class.getResource("/conf/flattened/locator.yml").toURI()); LocatorRegistry locatorRegistry = new LocatorRegistry(new Properties(), locatorFile); assertTrue(locatorRegistry.getLocator("my-button") instanceof ByCssSelector); assertTrue(locatorRegistry.getLocator("my-input") instanceof ByXPath); assertTrue(locatorRegistry.getLocator("my-frame") instanceof ById); assertEquals(new ByCssSelector("#mypage .mybutton"), locatorRegistry.getLocator("my-button")); assertEquals(new ByXPath("//input[@class='myinput']"), locatorRegistry.getLocator("my-input")); assertEquals(new ById("myframe"), locatorRegistry.getLocator("my-frame")); }
@Test public void shouldRegisterDefaultProfileWhenLocatorPropertyIsNotSet() throws URISyntaxException { File locatorFile = new File(LocatorRegistryTest.class.getResource("/conf/hierarchical/locator.yml").toURI()); LocatorRegistry locatorRegistry = new LocatorRegistry(new Properties(), locatorFile); assertTrue(locatorRegistry.getLocator("my-button") instanceof ByCssSelector); assertTrue(locatorRegistry.getLocator("my-input") instanceof ByXPath); assertTrue(locatorRegistry.getLocator("my-frame") instanceof ById); assertEquals(new ByCssSelector("#mypage .mybutton"), locatorRegistry.getLocator("my-button")); assertEquals(new ByXPath("//input[@class='myinput']"), locatorRegistry.getLocator("my-input")); assertEquals(new ById("myframe"), locatorRegistry.getLocator("my-frame")); }
@Test public void shouldRegisterUserSpecifiedProfileWhenLocatorPropertyIsSet() throws URISyntaxException { Properties properties = new Properties(); properties.setProperty("locator", "override"); File locatorFile = new File(LocatorRegistryTest.class.getResource("/conf/hierarchical/locator.yml").toURI()); LocatorRegistry locatorRegistry = new LocatorRegistry(properties, locatorFile); assertTrue(locatorRegistry.getLocator("my-button") instanceof ByCssSelector); assertTrue(locatorRegistry.getLocator("my-input") instanceof ByXPath); assertTrue(locatorRegistry.getLocator("my-frame") instanceof ById); assertEquals(new ByCssSelector("#overridepage .overridebutton"), locatorRegistry.getLocator("my-button")); assertEquals(new ByXPath("//input[@class='myinput']"), locatorRegistry.getLocator("my-input")); assertEquals(new ById("myframe"), locatorRegistry.getLocator("my-frame")); }