Skip to content

Commit

Permalink
Merge pull request #10 from Vertispan/gwt-driver-7
Browse files Browse the repository at this point in the history
gwt-driver-7 Add faster child/descendant Widget locator
  • Loading branch information
jhickman authored Feb 16, 2022
2 parents 00c6114 + 786312b commit 1c6006c
Show file tree
Hide file tree
Showing 14 changed files with 951 additions and 90 deletions.
13 changes: 13 additions & 0 deletions LICENSE_VERTISPAN
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright 2022 Vertispan 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.
52 changes: 44 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -212,19 +212,55 @@
<plugin>
<groupId>com.mycila</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>3.0</version>
<version>4.2.rc2</version>
<configuration>
<mapping>
<gss>JAVADOC_STYLE</gss>
<java>SLASHSTAR_STYLE</java>
</mapping>
<includes>
<include>**/*.java</include>
<include>**/*.gss</include>
<include>**/*.gwt.xml</include>
<include>**/*.html</include>
</includes>
<header>LICENSE</header>

<licenseSets>
<licenseSet>
<header>LICENSE_VERTISPAN</header>
<includes>
<include>**/*.java</include>
<include>**/*.gss</include>
<include>**/*.gwt.xml</include>
<include>**/*.ui.xml</include>
<include>**/*.html</include>
</includes>
</licenseSet>
<licenseSet>
<header>LICENSE</header>
<!-- grandfathered -->
<includes>
<include>**/Button.java</include>
<include>**/ByNearestWidget.java</include>
<include>**/ByWidget.java</include>
<include>**/CheatingByChained.java</include>
<include>**/Child.java</include>
<include>**/ClientMethods.java</include>
<include>**/ClientMethodsFactory.java</include>
<include>**/Dialog.java</include>
<include>**/ExportedMethods.java</include>
<include>**/FasterByChained.java</include>
<include>**/GwtDriver.gwt.xml</include>
<include>**/GwtLabel.java</include>
<include>**/GwtRootPanel.java</include>
<include>**/GwtWidget.java</include>
<include>**/GwtWidgetFinder.java</include>
<include>**/Input.java</include>
<include>**/ModuleUtilities.java</include>
<include>**/SeExporterGenerator.java</include>
<include>**/SeleniumExporter.java</include>
<include>**/SimpleWidgetTest.java</include>
<include>**/SimpleWidgets.gwt.xml</include>
<include>**/SimpleWidgetsEP.java</include>
<include>**/WidgetContainer.java</include>
<include>**/index.html</include>
</includes>
</licenseSet>
</licenseSets>
</configuration>
<executions>
<execution>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright 2022 Vertispan 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 com.vertispan.webdriver.gwt.gwtdriver.by;

import com.google.gwt.user.client.ui.Widget;

import com.vertispan.webdriver.gwt.gwtdriver.invoke.ClientMethodsFactory;
import com.vertispan.webdriver.gwt.gwtdriver.invoke.ExportedMethods;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.Require;

import java.util.List;


/**
* GWT specific {@code By} implementation that finds descendant widgets.
* <p>
* As this {@code By} calls the GWT module itself, it will only locate descendants if the {@link
* com.google.gwt.user.client.ui.HasWidgets} hierarchy is setup correctly. For example, if a Widget
* takes the Element from another Widget, but does not implement HasWidgets, this {@code By} will
* not locate the other Widget.
*/
public class ByDescendantWidget extends GwtBy {
private final String type;

/**
* Finds the descendant Widgets of any type - anything that extends Widget will be found.
*/
public ByDescendantWidget() {
this((WebDriver) null);
}

/**
* Finds the descendant Widgets of any type - anything that extends Widget will be found.
*
* @param driver The driver to use to communicate with the browser.
*/
public ByDescendantWidget(WebDriver driver) {
this(driver, Widget.class);
}

/**
* Finds the descendant Widgets of the given type.
* <p>
* This will find any subtype of that widget, allowing you to pass in {@link
* com.google.gwt.user.client.ui.ValueBoxBase} and find any {@link com.google.gwt.user.client.ui.TextBox},
* {@link com.google.gwt.user.client.ui.TextArea}, {@link com.google.gwt.user.client.ui.IntegerBox},
* etc, as these are all subclasses of {@code ValueBoxBase}. Note that interfaces cannot be used,
* only base classes, and those classes *must* extend Widget.
*
* @param widgetType The type of widget to find
*/
public ByDescendantWidget(Class<? extends Widget> widgetType) {
this(widgetType.getName());
}

/**
* Finds the descendant Widgets of the given type.
* <p>
* This will find any subtype of that widget, allowing you to pass in {@link
* com.google.gwt.user.client.ui.ValueBoxBase} and find any {@link com.google.gwt.user.client.ui.TextBox},
* {@link com.google.gwt.user.client.ui.TextArea}, {@link com.google.gwt.user.client.ui.IntegerBox},
* etc, as these are all subclasses of {@code ValueBoxBase}. Note that interfaces cannot be used,
* only base classes, and those classes *must* extend Widget.
*
* @param driver The driver to use to communicate with the browser.
* @param widgetType The type of widget to find
*/
public ByDescendantWidget(WebDriver driver, Class<? extends Widget> widgetType) {
this(driver, widgetType.getName());
}

/**
* Finds the descendant Widgets of the given type.
* <p>
* This will find any subtype of that widget, allowing you to pass in {@link
* com.google.gwt.user.client.ui.ValueBoxBase} and find any {@link com.google.gwt.user.client.ui.TextBox},
* {@link com.google.gwt.user.client.ui.TextArea}, {@link com.google.gwt.user.client.ui.IntegerBox},
* etc, as these are all subclasses of {@code ValueBoxBase}. Note that interfaces cannot be used,
* only base classes, and those classes *must* extend Widget.
*
* @param widgetClassName The type of widget to find
*/
public ByDescendantWidget(String widgetClassName) {
this(null, widgetClassName);
}

/**
* Finds the descendant Widgets of the given type.
* <p>
* This will find any subtype of that widget, allowing you to pass in {@link
* com.google.gwt.user.client.ui.ValueBoxBase} and find any {@link com.google.gwt.user.client.ui.TextBox},
* {@link com.google.gwt.user.client.ui.TextArea}, {@link com.google.gwt.user.client.ui.IntegerBox},
* etc, as these are all subclasses of {@code ValueBoxBase}. Note that interfaces cannot be used,
* only base classes, and those classes *must* extend Widget.
*
* @param driver The driver to use to communicate with the browser.
* @param widgetClassName The type of widget to find
*/
public ByDescendantWidget(WebDriver driver, String widgetClassName) {
super(driver);
this.type = widgetClassName;
}


@Override
public List<WebElement> findElements(SearchContext context) {
Require.nonNull("Search Context", context);
final WebElement contextElem = toWebElement(context);

ExportedMethods m = ClientMethodsFactory.create(ExportedMethods.class, getDriver(context));
// could return empty list
return m.findDescendantWidgetElementsOfType(contextElem, type);
}

@Override
public WebElement findElement(SearchContext context) {
Require.nonNull("Search Context", context);
final WebElement contextElem = toWebElement(context);

ExportedMethods m = ClientMethodsFactory.create(ExportedMethods.class, getDriver(context));
WebElement first = m.findFirstDescendantWidgetElementsOfType(contextElem, type);
if (first == null) {
throw new NoSuchElementException("Cannot find widget of type " + type);
}
return first;
}

@Override
public String toString() {
return "ByDescendantWidget{" +
"type='" + type + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.internal.Require;

import java.util.Collections;
import java.util.List;
Expand All @@ -44,10 +45,16 @@
* descendent element that is a widget, use another By to find those elements along with a {@code
* ByWidget} to confirm it is a widget.
*/
public class ByNearestWidget extends By {
private final WebDriver driver;
public class ByNearestWidget extends GwtBy {
private final String widgetClassName;

/**
* Finds the nearest containing widget of any type - anything that extends Widget will be found.
*/
public ByNearestWidget() {
this((WebDriver) null, Widget.class);
}

/**
* Finds the nearest containing widget of any type - anything that extends Widget will be found.
*
Expand All @@ -57,6 +64,18 @@ public ByNearestWidget(WebDriver driver) {
this(driver, Widget.class);
}

/**
* Finds the nearest containing widget of the given type. This will find any subtype of that
* widget, allowing you to pass in {@link ValueBoxBase} and find any {@link TextBox}, {@link
* TextArea}, {@link IntegerBox}, etc, as these are all subclasses of {@code ValueBoxBase}. Note
* that interfaces cannot be used, only base classes, and those classes *must* extend Widget.
*
* @param type the type of widget to find
*/
public ByNearestWidget(Class<? extends Widget> type) {
this(type.getName());
}

/**
* Finds the nearest containing widget of the given type. This will find any subtype of that
* widget, allowing you to pass in {@link ValueBoxBase} and find any {@link TextBox}, {@link
Expand All @@ -70,6 +89,18 @@ public ByNearestWidget(WebDriver driver, Class<? extends Widget> type) {
this(driver, type.getName());
}

/**
* Finds the nearest containing widget of the given type. This will find any subtype of that
* widget, allowing you to pass in {@link ValueBoxBase} and find any {@link TextBox}, {@link
* TextArea}, {@link IntegerBox}, etc, as these are all subclasses of {@code ValueBoxBase}. Note
* that interfaces cannot be used, only base classes, and those classes *must* extend Widget.
*
* @param widgetClassName the type of widget to find
*/
public ByNearestWidget(String widgetClassName) {
this(null, widgetClassName);
}

/**
* Finds the nearest containing widget of the given type. This will find any subtype of that
* widget, allowing you to pass in {@link ValueBoxBase} and find any {@link TextBox}, {@link
Expand All @@ -80,12 +111,13 @@ public ByNearestWidget(WebDriver driver, Class<? extends Widget> type) {
* @param widgetClassName the type of widget to find
*/
public ByNearestWidget(WebDriver driver, String widgetClassName) {
this.driver = driver;
super(driver);
this.widgetClassName = widgetClassName;
}

@Override
public List<WebElement> findElements(SearchContext context) {
Require.nonNull("Search Context", context);
WebElement elt = tryFindElement(context);
if (elt != null) {
return Collections.singletonList(elt);
Expand All @@ -95,6 +127,7 @@ public List<WebElement> findElements(SearchContext context) {

@Override
public WebElement findElement(SearchContext context) {
Require.nonNull("Search Context", context);
WebElement potentialElement = tryFindElement(context);
if (potentialElement == null) {
throw new NoSuchElementException("Cannot find a " + widgetClassName + " in " + context);
Expand All @@ -111,7 +144,7 @@ public WebElement findElement(SearchContext context) {
*/
private WebElement tryFindElement(SearchContext context) {
WebElement elt = context.findElement(By.xpath("."));
ExportedMethods m = ClientMethodsFactory.create(ExportedMethods.class, driver);
ExportedMethods m = ClientMethodsFactory.create(ExportedMethods.class, getDriver(context));
WebElement potentialElement = m.getContainingWidgetEltOfType(elt, widgetClassName);
return potentialElement;
}
Expand All @@ -121,5 +154,4 @@ public String toString() {
return "ByNearestWidget"
+ (Widget.class.getName().equals(widgetClassName) ? "" : " " + widgetClassName);
}

}
Loading

0 comments on commit 1c6006c

Please sign in to comment.