Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@ public interface AdditionalBrowserInteractions {

List<WebElement> filterByRegexp(List<WebElement> webElements, String regexp);

List<WebElement> filterByNearby(List<WebElement> webElements, WebElement target);

WebElement parentByCss(SearchContext element, String css);
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ public List<WebElement> filterByRegexp(List<WebElement> webElements, String rege
webElements, regexp);
}

@Override
@SuppressWarnings("unchecked")
public List<WebElement> filterByNearby(List<WebElement> webElements, WebElement target) {
injectScript();
return (List<WebElement>) javascriptExecutor.executeScript(returnTwoArgFunc("filterByNearby"),
webElements, target);
}

@Override
public WebElement parentByCss(SearchContext element, String css) {
injectScript();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@
import org.testingisdocumenting.webtau.browser.page.path.filter.ByNumberPageElementsFilter;
import org.testingisdocumenting.webtau.browser.page.path.filter.ByRegexpPageElementsFilter;
import org.testingisdocumenting.webtau.browser.page.path.filter.ByTextPageElementsFilter;
import org.testingisdocumenting.webtau.browser.page.path.finder.ByCssPageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.finder.ParentByCssPageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.finder.ParentPageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.filter.NearbyPageElementFilter;
import org.testingisdocumenting.webtau.browser.page.path.finder.*;
import org.testingisdocumenting.webtau.data.ValuePath;
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
import org.testingisdocumenting.webtau.data.render.PrettyPrinter;
Expand Down Expand Up @@ -265,6 +264,10 @@ public PageElement parent(String css) {
return withFinder(new ParentByCssPageElementFinder(additionalBrowserInteractions, css));
}

public PageElement nearby(PageElement target) {
return withFilter(new NearbyPageElementFilter(additionalBrowserInteractions, target));
}

public PageElement get(String text) {
return withFilter(new ByTextPageElementsFilter(additionalBrowserInteractions, text));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@
package org.testingisdocumenting.webtau.browser.page.path;

import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.util.List;

public interface PageElementFinder {
List<WebElement> find(SearchContext parent);
List<WebElement> find(PageElementPathSearchContext parent);

/**
* @param isFirst isFirst is this the first entry in the path
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@

package org.testingisdocumenting.webtau.browser.page.path;

import org.testingisdocumenting.webtau.browser.page.path.finder.ByCssPageElementFinder;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testingisdocumenting.webtau.browser.page.path.finder.ByCssPageElementFinder;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.*;

public class PageElementPath {
private List<PageElementPathEntry> entries;
Expand Down Expand Up @@ -64,16 +63,16 @@ public static PageElementPath css(String cssSelector) {
}

public List<WebElement> find(WebDriver driver) {
SearchContext root = driver;
var context = new PageElementPathSearchContext(driver, null);

List<WebElement> webElements = Collections.emptyList();
for (PageElementPathEntry entry : entries) {
webElements = entry.find(root);
webElements = entry.find(context);
if (webElements.isEmpty()) {
return webElements;
}

root = webElements.get(0);
context = PageElementPathSearchContext.fromElement(webElements.get(0));
}

return webElements;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package org.testingisdocumenting.webtau.browser.page.path;

import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
Expand All @@ -44,8 +43,8 @@ PageElementPathEntry copy() {
return copy;
}

List<WebElement> find(SearchContext parent) {
List<WebElement> elements = finder.find(parent);
List<WebElement> find(PageElementPathSearchContext searchContext) {
List<WebElement> elements = finder.find(searchContext);
if (elements.isEmpty()) {
return elements;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 webtau maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.webtau.browser.page.path;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

public record PageElementPathSearchContext(SearchContext searchContext, WebElement parent) {
static PageElementPathSearchContext fromElement(WebElement element) {
return new PageElementPathSearchContext(element, element);
}

public boolean hasParent() {
return parent != null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright 2023 webtau maintainers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.webtau.browser.page.path.filter;

import org.openqa.selenium.WebElement;
import org.testingisdocumenting.webtau.browser.AdditionalBrowserInteractions;
import org.testingisdocumenting.webtau.browser.page.NullWebElement;
import org.testingisdocumenting.webtau.browser.page.PageElement;
import org.testingisdocumenting.webtau.browser.page.path.PageElementsFilter;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;

import java.util.Collections;
import java.util.List;

import static org.testingisdocumenting.webtau.WebTauCore.*;

public class NearbyPageElementFilter implements PageElementsFilter {
private final AdditionalBrowserInteractions additionalBrowserInteractions;
private final PageElement target;

public NearbyPageElementFilter(AdditionalBrowserInteractions additionalBrowserInteractions, PageElement target) {
this.additionalBrowserInteractions = additionalBrowserInteractions;
this.target = target;
}

@Override
public List<WebElement> filter(List<WebElement> original) {
WebElement targetElement = target.findElement();
if (targetElement instanceof NullWebElement) {
return Collections.emptyList();
}

return additionalBrowserInteractions.filterByNearby(original, targetElement);
}

@Override
public TokenizedMessage description() {
return tokenizedMessage().classifier("nearby").add(target.describe());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
package org.testingisdocumenting.webtau.browser.page.path.finder;

import org.testingisdocumenting.webtau.browser.page.path.PageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.PageElementPathSearchContext;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;

import java.util.List;
Expand All @@ -35,8 +35,8 @@ public ByCssPageElementFinder(String css) {
}

@Override
public List<WebElement> find(SearchContext parent) {
return parent.findElements(By.cssSelector(css));
public List<WebElement> find(PageElementPathSearchContext parent) {
return parent.searchContext().findElements(By.cssSelector(css));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2021 webtau maintainers
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.webtau.browser.page.path.finder;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.locators.RelativeLocator;
import org.testingisdocumenting.webtau.browser.page.PageElement;

import java.util.function.Function;

public class ByRelativeAbovePageElementFinder extends ByRelativePageElementFinder {
public ByRelativeAbovePageElementFinder(PageElement target) {
super(target, "above");
}

protected Function<WebElement, RelativeLocator.RelativeBy> direction(RelativeLocator.RelativeBy chainStart) {
return chainStart::above;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2021 webtau maintainers
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.testingisdocumenting.webtau.browser.page.path.finder;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.locators.RelativeLocator;
import org.testingisdocumenting.webtau.browser.page.PageElement;
import org.testingisdocumenting.webtau.browser.page.path.PageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.PageElementPathSearchContext;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;

import java.util.List;
import java.util.function.Function;

import static org.testingisdocumenting.webtau.WebTauCore.*;

abstract public class ByRelativePageElementFinder implements PageElementFinder {
private final PageElement target;
private final String classifier;

public ByRelativePageElementFinder(PageElement target, String classifier) {
this.target = target;
this.classifier = classifier;
}

abstract protected Function<WebElement, RelativeLocator.RelativeBy> direction(RelativeLocator.RelativeBy chainStart);

@Override
public List<WebElement> find(PageElementPathSearchContext parent) {
RelativeLocator.RelativeBy chainStart = RelativeLocator.with(new ByAdapter(parent.parent()));
RelativeLocator.RelativeBy relativeBy = direction(chainStart).apply(target.findElement());
return parent.searchContext().findElements(relativeBy);
}

@Override
public TokenizedMessage description(boolean isFirst) {
return tokenizedMessage().selectorType(classifier).add(target.describe());
}

static private class ByAdapter extends By {
private final WebElement parent;

public ByAdapter(WebElement parent) {
this.parent = parent;
}

@Override
public List<WebElement> findElements(SearchContext context) {
return List.of(parent);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.testingisdocumenting.webtau.browser.page.path.finder;

import org.openqa.selenium.By;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.testingisdocumenting.webtau.browser.page.path.PageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.PageElementPathSearchContext;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;

import java.util.List;
Expand All @@ -34,8 +34,8 @@ public ByXPathPageElementFinder(String xPath) {
}

@Override
public List<WebElement> find(SearchContext parent) {
return parent.findElements(By.xpath(xPath));
public List<WebElement> find(PageElementPathSearchContext parent) {
return parent.searchContext().findElements(By.xpath(xPath));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

package org.testingisdocumenting.webtau.browser.page.path.finder;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebElement;
import org.testingisdocumenting.webtau.browser.AdditionalBrowserInteractions;
import org.testingisdocumenting.webtau.browser.page.path.PageElementFinder;
import org.testingisdocumenting.webtau.browser.page.path.PageElementPathSearchContext;
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;

import java.util.Collections;
Expand All @@ -37,8 +37,8 @@ public ParentByCssPageElementFinder(AdditionalBrowserInteractions additionalBrow
}

@Override
public List<WebElement> find(SearchContext parent) {
WebElement webElement = additionalBrowserInteractions.parentByCss(parent, css);
public List<WebElement> find(PageElementPathSearchContext parent) {
WebElement webElement = additionalBrowserInteractions.parentByCss(parent.searchContext(), css);
return webElement == null ? Collections.emptyList() : Collections.singletonList(webElement);
}

Expand Down
Loading