/** * Compiles the selector for the given context (the context will determinate what selectors are natively * supported and what selectors should be handled by SeleniumQuery programatically) and matches elements based on it. */ @Override public List<WebElement> findElements(SearchContext context, String selector) { TagComponentList xPathLocator = XPathComponentCompilerService.compileSelectorList(selector); return xPathLocator.findWebElements(context); }
@Override public ConditionSimpleComponent pseudoClassToXPath(PseudoClassSelector pseudoClassSelector) { String notSelector = pseudoClassSelector.getPseudoClassContent(); String insideHasXPath = XPathComponentCompilerService.compileSelectorList(notSelector).toXPath(); insideHasXPath = insideHasXPath.substring(1, insideHasXPath.length()-1); return new ConditionSimpleComponent("[" + insideHasXPath + "]"); }
public static TagComponentList compileSelectorList(String selector) { W3cCssSelectorListWithMap parsedSelectorList = W3cCssSelectorWithMapParser.parseSelector(selector); List<TagComponent> tagComponents = new ArrayList<>(parsedSelectorList.size()); parsedSelectorList.forEach(w3cCssSelectorWithMap -> { tagComponents.add(compileIntoTagComponent(w3cCssSelectorWithMap)); }); return new TagComponentList(tagComponents); }
@Override public ConditionSimpleComponent pseudoClassToXPath(PseudoClassSelector pseudoClassSelector) { String notSelector = pseudoClassSelector.getPseudoClassContent(); String insideNotXPath = XPathComponentCompilerService.compileSelectorList(notSelector).toXPathCondition(); return new ConditionSimpleComponent("[not(" + insideNotXPath + ")]"); }
@Override public ConditionSimpleComponent pseudoClassToXPath(PseudoClassSelector pseudoClassSelector) { String notSelector = pseudoClassSelector.getPseudoClassContent(); String insideNotXPath = XPathComponentCompilerService.compileSelectorList(notSelector).toXPathCondition(); return new ConditionSimpleComponent("[not(" + insideNotXPath + ")]"); }
/** * Compiles the selector for the given context (the context will determinate what selectors are natively * supported and what selectors should be handled by SeleniumQuery programatically) and matches elements based on it. */ @Override public List<WebElement> findElements(SearchContext context, String selector) { TagComponentList xPathLocator = XPathComponentCompilerService.compileSelectorList(selector); return xPathLocator.findWebElements(context); }
@Override public ConditionSimpleComponent pseudoClassToXPath(PseudoClassSelector pseudoClassSelector) { String notSelector = pseudoClassSelector.getPseudoClassContent(); String insideHasXPath = XPathComponentCompilerService.compileSelectorList(notSelector).toXPath(); insideHasXPath = insideHasXPath.substring(1, insideHasXPath.length()-1); return new ConditionSimpleComponent("[" + insideHasXPath + "]"); }
public static TagComponentList compileSelectorList(String selector) { W3cCssSelectorListWithMap parsedSelectorList = W3cCssSelectorWithMapParser.parseSelector(selector); List<TagComponent> tagComponents = new ArrayList<>(parsedSelectorList.size()); parsedSelectorList.forEach(w3cCssSelectorWithMap -> { tagComponents.add(compileIntoTagComponent(w3cCssSelectorWithMap)); }); return new TagComponentList(tagComponents); }
@Override public boolean isPseudoClass(WebDriver driver, WebElement element, PseudoClassSelector pseudoClassSelector) { String hasSelector = pseudoClassSelector.getPseudoClassContent(); TagComponentList compiledSelector = XPathComponentCompilerService.compileSelectorList(hasSelector); List<WebElement> elements = compiledSelector.findWebElements(driver); return !elements.isEmpty(); }
@Test public void attribute_escaping__maybe_should_change() { String selector = "[attr=\"a\\\"bc\"]"; // [attr="a\"bc"] TagComponentList compileSelectorList = XPathComponentCompilerService.compileSelectorList(selector); String xPath = compileSelectorList.toXPath(); assertThat(xPath, is("(.//*[@*[translate(name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'attr'] = 'a\\\"bc'])")); // (.//*[@attr = 'a\"bc']) }
@Override public boolean isPseudoClass(WebDriver driver, WebElement element, PseudoClassSelector pseudoClassSelector) { String hasSelector = pseudoClassSelector.getPseudoClassContent(); TagComponentList compiledSelector = XPathComponentCompilerService.compileSelectorList(hasSelector); List<WebElement> elements = compiledSelector.findWebElements(driver); return !elements.isEmpty(); }
@Test public void toXPath__id() { TagComponentList compileSelectorList = XPathComponentCompilerService.compileSelectorList("#ball"); assertThat(compileSelectorList.toXPath(), is("(.//*[@id = 'ball'])")); }
@Test public void findWebElements_should_call_findElementsByXPath() { // given TagComponentList tagComponentList = XPathComponentCompilerService.compileSelectorList("span"); SearchContext searchContext = createSearchContextThatReturnsWebElementsForXPath("(.//*[self::span])", dummyWebElements); // when List<WebElement> webElements = tagComponentList.findWebElements(searchContext); // then assertThat(webElements, is(dummyWebElements)); }
@Test public void toXPath__and_conditional() { TagComponentList compileSelectorList = XPathComponentCompilerService.compileSelectorList(".a.b"); assertThat(compileSelectorList.toXPath(), is("(.//*[contains(concat(' ', normalize-space(@class), ' '), ' a ') and contains(concat(' ', normalize-space(@class), ' '), ' b ')])")); }
@Test public void findWebElements_if_selector_is_just_an_id_it_should_call_findElementById() { // given TagComponentList tagComponentList = XPathComponentCompilerService.compileSelectorList("#idz"); SearchContext searchContext = createSearchContextThatReturnsWebElementForId("idz", firstDummyWebElement); // when List<WebElement> webElements = tagComponentList.findWebElements(searchContext); // then assertThat(webElements, contains(firstDummyWebElement)); }