Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java: All searching interfaces are made generic #863

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 3 additions & 3 deletions java/client/src/org/openqa/selenium/SearchContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface SearchContext {
* @return A list of all {@link WebElement}s, or an empty list if nothing matches
* @see org.openqa.selenium.By
*/
List<WebElement> findElements(By by);
<T extends WebElement> List<T> findElements(By by);


/**
Expand All @@ -37,5 +37,5 @@ public interface SearchContext {
* @return The first matching element on the current context
* @throws NoSuchElementException If no matching elements are found
*/
WebElement findElement(By by);
}
<T extends WebElement> T findElement(By by);
}
20 changes: 18 additions & 2 deletions java/client/src/org/openqa/selenium/WebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,19 @@ public interface WebDriver extends SearchContext {
* found collection, or will return an empty list if the timeout is reached.
*
* @param by The locating mechanism to use
*
* @return A list of all {@link WebElement}s, or an empty list if nothing matches
*
* @throws ClassCastException The WebDriver interface is generic.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these comments about ClassCastException unnecessary? ClassCastException is unchecked.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alexec this is just information. If end users will re-implement the proposed API then there won't be any problem. An end user will face a problem when they use common Selenium implementations, because it not so flexible like API.

* It allows an end user to develop a WebDriver implementation that can find&return
* a list of instances of the desired {@link WebElement} subclass. But it is strongly recommended
* to use WebElement as the type of a list parameter when there are used
* common WebDriver implementations of the Selenium project.
*
* @see org.openqa.selenium.By
* @see org.openqa.selenium.WebDriver.Timeouts
*/
List<WebElement> findElements(By by);
<T extends WebElement> List<T> findElements(By by) throws ClassCastException;


/**
Expand All @@ -109,11 +117,19 @@ public interface WebDriver extends SearchContext {
*
* @param by The locating mechanism
* @return The first matching element on the current page
*
* @throws NoSuchElementException If no matching elements are found
*
* @throws ClassCastException The WebDriver interface is generic.
* It allows an end user to develop a WebDriver implementation that can find&return an
* instance of the desired {@link WebElement} subclass. But it is strongly recommended
* to use WebElement as the type of the returned element when there are used
* common WebDriver implementations of the Selenium project.
*
* @see org.openqa.selenium.By
* @see org.openqa.selenium.WebDriver.Timeouts
*/
WebElement findElement(By by);
<T extends WebElement> T findElement(By by) throws NoSuchElementException, ClassCastException;

// Misc

Expand Down
19 changes: 17 additions & 2 deletions java/client/src/org/openqa/selenium/WebElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,17 @@ public interface WebElement extends SearchContext, TakesScreenshot {
*
* @param by The locating mechanism to use
* @return A list of all {@link WebElement}s, or an empty list if nothing matches.
*
* @throws ClassCastException The WebElement interface is generic.
* It allows an end user to develop a WebElement implementation that can find&return
* a list of instances of the desired WebElement subclass. But it is strongly recommended to use WebElement
* as the type of a list parameter when there are used common {@link WebDriver} implementations of
* the Selenium project.
*
* @see org.openqa.selenium.By
* @see org.openqa.selenium.WebDriver.Timeouts
*/
List<WebElement> findElements(By by);
<T extends WebElement> List<T> findElements(By by);

/**
* Find the first {@link WebElement} using the given method. See the note in
Expand All @@ -164,11 +171,19 @@ public interface WebElement extends SearchContext, TakesScreenshot {
*
* @param by The locating mechanism
* @return The first matching element on the current context.
*
* @throws NoSuchElementException If no matching elements are found
*
* @throws ClassCastException The WebElement interface is generic.
* It allows an end user to develop a WebElement implementation that can find&return an instance of the desired
* WebElement subclass. But it is strongly recommended to use WebElement
* as the type of the returned element when there are used common {@link WebDriver} implementations of
* the Selenium project.
*
* @see org.openqa.selenium.By
* @see org.openqa.selenium.WebDriver.Timeouts
*/
WebElement findElement(By by);
<T extends WebElement> T findElement(By by);

/**
* Is this element displayed or not? This method avoids the problem of having to parse an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
* which is a headless (GUI-less) browser simulator.
* <p>The main supported browsers are Chrome, Firefox and Internet Explorer.
*/
@SuppressWarnings({"unchecked"})
public class HtmlUnitDriver implements WebDriver, JavascriptExecutor,
FindsById, FindsByLinkText, FindsByXPath, FindsByName, FindsByCssSelector,
FindsByTagName, FindsByClassName, HasCapabilities, HasInputDevices {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
import com.google.common.base.Strings;
import com.google.common.base.Throwables;


@SuppressWarnings({"unchecked"})
public class HtmlUnitWebElement implements WrapsDriver,
FindsById, FindsByLinkText, FindsByXPath, FindsByTagName,
FindsByCssSelector, Locatable, WebElement {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

public interface FindsByClassName {
WebElement findElementByClassName(String using);
<T extends WebElement> T findElementByClassName(String using);

List<WebElement> findElementsByClassName(String using);
<T extends WebElement> List<T> findElementsByClassName(String using);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

public interface FindsByCssSelector {
WebElement findElementByCssSelector(String using);
<T extends WebElement> T findElementByCssSelector(String using);

List<WebElement> findElementsByCssSelector(String using);
<T extends WebElement> List<T> findElementsByCssSelector(String using);
}
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/internal/FindsById.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

public interface FindsById {
WebElement findElementById(String using);
<T extends WebElement> T findElementById(String using);

List<WebElement> findElementsById(String using);
<T extends WebElement> List<T> findElementsById(String using);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
import java.util.List;

public interface FindsByLinkText {
WebElement findElementByLinkText(String using);
<T extends WebElement> T findElementByLinkText(String using);

List<WebElement> findElementsByLinkText(String using);
<T extends WebElement> List<T> findElementsByLinkText(String using);

WebElement findElementByPartialLinkText(String using);
<T extends WebElement> T findElementByPartialLinkText(String using);

List<WebElement> findElementsByPartialLinkText(String using);
<T extends WebElement> List<T> findElementsByPartialLinkText(String using);
}
4 changes: 2 additions & 2 deletions java/client/src/org/openqa/selenium/internal/FindsByName.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

public interface FindsByName {
WebElement findElementByName(String using);
<T extends WebElement> T findElementByName(String using);

List<WebElement> findElementsByName(String using);
<T extends WebElement> List<T> findElementsByName(String using);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

public interface FindsByTagName {
WebElement findElementByTagName(String using);
<T extends WebElement> T findElementByTagName(String using);

List<WebElement> findElementsByTagName(String using);
<T extends WebElement> List<T> findElementsByTagName(String using);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.util.List;

public interface FindsByXPath {
WebElement findElementByXPath(String using);
<T extends WebElement> T findElementByXPath(String using);

List<WebElement> findElementsByXPath(String using);
<T extends WebElement> List<T> findElementsByXPath(String using);
}
16 changes: 8 additions & 8 deletions java/client/src/org/openqa/selenium/remote/RemoteWebDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

@Augmentable
@Augmentable @SuppressWarnings({"unchecked"})
public class RemoteWebDriver implements WebDriver, JavascriptExecutor,
FindsById, FindsByClassName, FindsByLinkText, FindsByName,
FindsByCssSelector, FindsByTagName, FindsByXPath,
Expand Down Expand Up @@ -232,7 +232,7 @@ protected void startSession(Capabilities desiredCapabilities) {
startSession(desiredCapabilities, null);
}

@SuppressWarnings({"unchecked"})

protected void startSession(Capabilities desiredCapabilities,
Capabilities requiredCapabilities) {

Expand Down Expand Up @@ -369,7 +369,7 @@ protected void setFoundBy(SearchContext context, WebElement element, String by,
}
}

@SuppressWarnings("unchecked")

protected List<WebElement> findElements(String by, String using) {
if (using == null) {
throw new IllegalArgumentException("Cannot find elements when the selector is null.");
Expand Down Expand Up @@ -520,7 +520,7 @@ public void quit() {
}
}

@SuppressWarnings({"unchecked"})

public Set<String> getWindowHandles() {
Response response = execute(DriverCommand.GET_WINDOW_HANDLES);
Object value = response.getValue();
Expand Down Expand Up @@ -741,7 +741,7 @@ public void deleteAllCookies() {
execute(DriverCommand.DELETE_ALL_COOKIES);
}

@SuppressWarnings({"unchecked"})

public Set<Cookie> getCookies() {
Object returned = execute(DriverCommand.GET_ALL_COOKIES).getValue();

Expand Down Expand Up @@ -795,7 +795,7 @@ public Window window() {

protected class RemoteInputMethodManager implements WebDriver.ImeHandler {

@SuppressWarnings("unchecked")

public List<String> getAvailableEngines() {
Response response = execute(DriverCommand.IME_GET_AVAILABLE_ENGINES);
return (List<String>) response.getValue();
Expand Down Expand Up @@ -869,7 +869,7 @@ public void setPosition(Point targetPosition) {
}
}

@SuppressWarnings({"unchecked"})

public Dimension getSize() {
Response response = getW3CStandardComplianceLevel() == 0
? execute(DriverCommand.GET_WINDOW_SIZE, ImmutableMap.of("windowHandle", "current"))
Expand All @@ -883,7 +883,7 @@ public Dimension getSize() {
return new Dimension(width, height);
}

@SuppressWarnings({"unchecked"})

Map<String, Object> rawPoint;
public Point getPosition() {
if (getW3CStandardComplianceLevel() == 0) {
Expand Down
12 changes: 6 additions & 6 deletions java/client/src/org/openqa/selenium/remote/RemoteWebElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;

@SuppressWarnings({"unchecked"})
public class RemoteWebElement implements WebElement, FindsByLinkText, FindsById, FindsByName,
FindsByTagName, FindsByClassName, FindsByCssSelector,
FindsByXPath, WrapsDriver, Locatable, HasIdentity,
Expand Down Expand Up @@ -206,7 +206,7 @@ protected WebElement findElement(String using, String value) {
return element;
}

@SuppressWarnings("unchecked")

protected List<WebElement> findElements(String using, String value) {
Response response = execute(DriverCommand.FIND_CHILD_ELEMENTS,
ImmutableMap.of("id", id, "using", using, "value", value));
Expand Down Expand Up @@ -371,7 +371,7 @@ public boolean isDisplayed() {
}
}

@SuppressWarnings({"unchecked"})

public Point getLocation() {
Response response = parent.getW3CStandardComplianceLevel() == 0
? execute(DriverCommand.GET_ELEMENT_LOCATION, ImmutableMap.of("id", id))
Expand All @@ -382,7 +382,7 @@ public Point getLocation() {
return new Point(x, y);
}

@SuppressWarnings({"unchecked"})

public Dimension getSize() {
Response response = parent.getW3CStandardComplianceLevel() == 0
? execute(DriverCommand.GET_ELEMENT_SIZE, ImmutableMap.of("id", id))
Expand All @@ -405,12 +405,12 @@ public Point inViewPort() {
Response response = execute(DriverCommand.GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW,
ImmutableMap.of("id", getId()));

@SuppressWarnings("unchecked")

Map<String, Number> mapped = (Map<String, Number>) response.getValue();
return new Point(mapped.get("x").intValue(), mapped.get("y").intValue());

} else {
@SuppressWarnings("unchecked")

Map<String, Number> mapped = (Map<String, Number>) parent.executeScript(
"return arguments[0].getBoundingClientRect()", RemoteWebElement.this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
* A wrapper around an arbitrary {@link WebDriver} instance which supports registering of a
* {@link WebDriverEventListener}, e&#46;g&#46; for logging purposes.
*/
@SuppressWarnings({"unchecked"})
public class EventFiringWebDriver implements WebDriver, JavascriptExecutor, TakesScreenshot,
WrapsDriver, HasInputDevices, HasTouchScreen {

Expand Down
72 changes: 72 additions & 0 deletions java/client/test/org/openqa/selenium/SearchContextTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.openqa.selenium;

import org.junit.Test;

import java.util.List;

// this class just tests we can compile the code
public class SearchContextTest {

private final SubClassingSearchContext subClassingSearchContext = new SubClassingSearchContext();
private final SameClassSearchContext sameClassSearchContext = new SameClassSearchContext() {
@Override
public List<WebElement> findElements(By by) {
return null;
}

@Override
public WebElement findElement(By by) {
return null;
}
};

private interface TestWebElement extends WebElement {

}

private static class SubClassingSearchContext implements SearchContext {

@Override
public List<TestWebElement> findElements(By by) {
return null;
}

@Override
public TestWebElement findElement(By by) {
return null;
}
}

private interface SameClassSearchContext extends SearchContext {

}

@Test
public void makeSureFindElementsIsBackwardsCompatible() throws Exception {

@SuppressWarnings("unused")
List<? extends WebElement> elements = sameClassSearchContext.findElements(null);
}

@Test
public void makeSureFindElementIsBackwardsCompatible() throws Exception {

@SuppressWarnings("unused")
WebElement element = sameClassSearchContext.findElement(null);
}

@Test
public void makeSureFindElementsCanUseSuperClass() throws Exception {
@SuppressWarnings("unused")
List<TestWebElement> elements = subClassingSearchContext.findElements(null);
}

@Test
public void makeSureFindElementCanUseSuperClass() throws Exception {

@SuppressWarnings("unused")
WebElement element = subClassingSearchContext.findElement(null);


}
}
Loading