Skip to content

Commit 65d2c3a

Browse files
SrinivasanTargetTikhomirovSergey
authored andcommitted
Selenium 3 updates (#489)
* Selenium 3 Updates with updates of new element event listeners * Rotation support * Upgraded Selenium to 3.0 Fixed check style issues
1 parent 5b76d32 commit 65d2c3a

File tree

8 files changed

+122
-38
lines changed

8 files changed

+122
-38
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ compileJava {
5050
}
5151

5252
dependencies {
53-
compile('org.seleniumhq.selenium:selenium-java:2.53.1'){
53+
compile('org.seleniumhq.selenium:selenium-java:3.0.0'){
5454
exclude module: 'cglib'
5555
exclude group: 'com.google.code.gson'
5656
}
@@ -60,6 +60,7 @@ dependencies {
6060
compile 'cglib:cglib:3.2.4'
6161
compile 'commons-validator:commons-validator:1.5.1'
6262
compile 'org.apache.commons:commons-lang3:3.4'
63+
compile 'commons-io:commons-io:2.5'
6364
compile 'com.google.code.gson:gson:2.6.2'
6465
compile 'org.springframework:spring-context:4.3.2.RELEASE'
6566
compile 'org.aspectj:aspectjweaver:1.8.9'

src/main/java/io/appium/java_client/AppiumDriver.java

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import org.openqa.selenium.By;
3232
import org.openqa.selenium.Capabilities;
33+
import org.openqa.selenium.DeviceRotation;
3334
import org.openqa.selenium.Dimension;
3435
import org.openqa.selenium.Point;
3536
import org.openqa.selenium.ScreenOrientation;
@@ -57,17 +58,18 @@
5758
import java.util.Map;
5859
import java.util.Set;
5960

61+
6062
/**
61-
* @param <T> the required type of class which implement {@link org.openqa.selenium.WebElement}.
62-
* Instances of the defined type will be returned via findElement* and findElements*
63-
* Warning (!!!). Allowed types:
64-
* {@link org.openqa.selenium.WebElement}
65-
* {@link io.appium.java_client.TouchableElement}
66-
* {@link org.openqa.selenium.remote.RemoteWebElement}
67-
* {@link io.appium.java_client.MobileElement} and its subclasses that designed
68-
* specifically
69-
* for each target mobile OS (still Android and iOS)
70-
*/
63+
* @param <T> the required type of class which implement {@link org.openqa.selenium.WebElement}.
64+
* Instances of the defined type will be returned via findElement* and findElements*
65+
* Warning (!!!). Allowed types:
66+
* {@link org.openqa.selenium.WebElement}
67+
* {@link io.appium.java_client.TouchableElement}
68+
* {@link org.openqa.selenium.remote.RemoteWebElement}
69+
* {@link io.appium.java_client.MobileElement} and its subclasses that designed
70+
* specifically
71+
* for each target mobile OS (still Android and iOS)
72+
*/
7173
@SuppressWarnings("unchecked")
7274
public abstract class AppiumDriver<T extends WebElement>
7375
extends DefaultGenericMobileDriver<T> {
@@ -79,13 +81,13 @@ public abstract class AppiumDriver<T extends WebElement>
7981
private ExecuteMethod executeMethod;
8082

8183
/**
82-
* @param executor is an instance of {@link org.openqa.selenium.remote.HttpCommandExecutor}
83-
* or class that extends it. Default commands or another vendor-specific
84-
* commands may be specified there.
85-
* @param capabilities take a look
86-
* at {@link org.openqa.selenium.Capabilities}
84+
* @param executor is an instance of {@link org.openqa.selenium.remote.HttpCommandExecutor}
85+
* or class that extends it. Default commands or another vendor-specific
86+
* commands may be specified there.
87+
* @param capabilities take a look
88+
* at {@link org.openqa.selenium.Capabilities}
8789
* @param converterClazz is an instance of a class that extends
88-
* {@link org.openqa.selenium.remote.internal.JsonToWebElementConverter}. It converts
90+
* {@link org.openqa.selenium.remote.internal.JsonToWebElementConverter}. It converts
8991
* JSON response to an instance of
9092
* {@link org.openqa.selenium.WebElement}
9193
*/
@@ -98,10 +100,10 @@ protected AppiumDriver(HttpCommandExecutor executor, Capabilities capabilities,
98100
this.remoteAddress = executor.getAddressOfRemoteServer();
99101
try {
100102
Constructor<? extends JsonToWebElementConverter> constructor =
101-
converterClazz.getConstructor(RemoteWebDriver.class);
103+
converterClazz.getConstructor(RemoteWebDriver.class);
102104
this.setElementConverter(constructor.newInstance(this));
103105
} catch (NoSuchMethodException | IllegalAccessException | InstantiationException
104-
| InvocationTargetException e) {
106+
| InvocationTargetException e) {
105107
throw new RuntimeException(e);
106108
}
107109
}
@@ -114,7 +116,7 @@ public AppiumDriver(URL remoteAddress, Capabilities desiredCapabilities,
114116

115117
public AppiumDriver(URL remoteAddress, HttpClient.Factory httpClientFactory,
116118
Capabilities desiredCapabilities,
117-
Class<? extends JsonToWebElementConverter> converterClazz) {
119+
Class<? extends JsonToWebElementConverter> converterClazz) {
118120
this(new AppiumCommandExecutor(MobileCommand.commandRepository, remoteAddress,
119121
httpClientFactory), desiredCapabilities, converterClazz);
120122
}
@@ -127,7 +129,7 @@ public AppiumDriver(AppiumDriverLocalService service, Capabilities desiredCapabi
127129

128130
public AppiumDriver(AppiumDriverLocalService service, HttpClient.Factory httpClientFactory,
129131
Capabilities desiredCapabilities,
130-
Class<? extends JsonToWebElementConverter> converterClazz) {
132+
Class<? extends JsonToWebElementConverter> converterClazz) {
131133
this(new AppiumCommandExecutor(MobileCommand.commandRepository, service, httpClientFactory),
132134
desiredCapabilities, converterClazz);
133135
}
@@ -139,14 +141,14 @@ public AppiumDriver(AppiumServiceBuilder builder, Capabilities desiredCapabiliti
139141

140142
public AppiumDriver(AppiumServiceBuilder builder, HttpClient.Factory httpClientFactory,
141143
Capabilities desiredCapabilities,
142-
Class<? extends JsonToWebElementConverter> converterClazz) {
144+
Class<? extends JsonToWebElementConverter> converterClazz) {
143145
this(builder.build(), httpClientFactory, desiredCapabilities, converterClazz);
144146
}
145147

146148
public AppiumDriver(HttpClient.Factory httpClientFactory, Capabilities desiredCapabilities,
147149
Class<? extends JsonToWebElementConverter> converterClazz) {
148-
this(AppiumDriverLocalService.buildDefaultService(), httpClientFactory,
149-
desiredCapabilities, converterClazz);
150+
this(AppiumDriverLocalService.buildDefaultService(), httpClientFactory, desiredCapabilities,
151+
converterClazz);
150152
}
151153

152154
public AppiumDriver(Capabilities desiredCapabilities,
@@ -156,8 +158,8 @@ public AppiumDriver(Capabilities desiredCapabilities,
156158

157159
/**
158160
* @param originalCapabilities the given {@link Capabilities}.
159-
* @param newPlatform a {@link MobileCapabilityType#PLATFORM_NAME} value which has
160-
* to be set up
161+
* @param newPlatform a {@link MobileCapabilityType#PLATFORM_NAME} value which has
162+
* to be set up
161163
* @return {@link Capabilities} with changed mobile platform value
162164
*/
163165
protected static Capabilities substituteMobilePlatform(Capabilities originalCapabilities,
@@ -409,6 +411,21 @@ public void zoom(int x, int y) {
409411
return contextName;
410412
}
411413

414+
@Override public DeviceRotation rotation() {
415+
Response response = execute(DriverCommand.GET_SCREEN_ROTATION);
416+
DeviceRotation deviceRotation =
417+
new DeviceRotation((Map<String, Number>) response.getValue());
418+
if (deviceRotation.getX() < 0 || deviceRotation.getY() < 0 || deviceRotation.getZ() < 0) {
419+
throw new WebDriverException("Unexpected orientation returned: " + deviceRotation);
420+
}
421+
return deviceRotation;
422+
}
423+
424+
@Override public void rotate(DeviceRotation rotation) {
425+
execute(DriverCommand.SET_SCREEN_ROTATION, rotation.parameters());
426+
}
427+
428+
412429
@Override public void rotate(ScreenOrientation orientation) {
413430
execute(DriverCommand.SET_SCREEN_ORIENTATION,
414431
ImmutableMap.of("orientation", orientation.value().toUpperCase()));
@@ -450,7 +467,6 @@ public URL getRemoteAddress() {
450467

451468
/**
452469
* @return a map with values that hold session details.
453-
*
454470
*/
455471
public Map<String, Object> getSessionDetails() {
456472
Response response = execute(GET_SESSION);

src/main/java/io/appium/java_client/events/DefaultListener.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,18 @@
4242
import java.util.Collection;
4343
import java.util.List;
4444

45-
class DefaultListener implements Listener, AppiumWebDriverEventListener, ListensToException,
46-
SearchingEventListener, NavigationEventListener,
47-
JavaScriptEventListener, ElementEventListener, AlertEventListener,
45+
class DefaultListener
46+
implements Listener, AppiumWebDriverEventListener, ListensToException, SearchingEventListener,
47+
NavigationEventListener, JavaScriptEventListener, ElementEventListener, AlertEventListener,
4848
WindowEventListener, ContextEventListener, RotationEventListener {
4949

5050
private final List<Listener> listeners = new ArrayList<>();
5151

52-
private Object dispatcher = Proxy.newProxyInstance(Listener.class.getClassLoader(),
53-
new Class[] {AlertEventListener.class,
54-
ContextEventListener.class, ElementEventListener.class, JavaScriptEventListener.class,
55-
ListensToException.class, NavigationEventListener.class, RotationEventListener.class,
56-
SearchingEventListener.class, WindowEventListener.class,
57-
WebDriverEventListener.class},
52+
private Object dispatcher = Proxy.newProxyInstance(Listener.class.getClassLoader(),
53+
new Class[] {AlertEventListener.class, ContextEventListener.class,
54+
ElementEventListener.class, JavaScriptEventListener.class, ListensToException.class,
55+
NavigationEventListener.class, RotationEventListener.class,
56+
SearchingEventListener.class, WindowEventListener.class, WebDriverEventListener.class},
5857
new ListenerInvocationHandler(listeners));
5958

6059
@Override public void beforeNavigateTo(String url, WebDriver driver) {
@@ -109,10 +108,20 @@ class DefaultListener implements Listener, AppiumWebDriverEventListener, Listens
109108
((ElementEventListener) dispatcher).beforeChangeValueOf(element, driver);
110109
}
111110

111+
@Override public void beforeChangeValueOf(WebElement element, WebDriver driver,
112+
CharSequence[] keysToSend) {
113+
((ElementEventListener) dispatcher).beforeChangeValueOf(element, driver, keysToSend);
114+
}
115+
112116
@Override public void afterChangeValueOf(WebElement element, WebDriver driver) {
113117
((ElementEventListener) dispatcher).afterChangeValueOf(element, driver);
114118
}
115119

120+
@Override public void afterChangeValueOf(WebElement element, WebDriver driver,
121+
CharSequence[] keysToSend) {
122+
((ElementEventListener) dispatcher).afterChangeValueOf(element, driver, keysToSend);
123+
}
124+
116125
@Override public void beforeScript(String script, WebDriver driver) {
117126
((JavaScriptEventListener) dispatcher).beforeScript(script, driver);
118127
}
@@ -206,4 +215,4 @@ public void afterWindowIsMoved(WebDriver driver, WebDriver.Window window, Point
206215
@Override public void afterRotation(WebDriver driver, ScreenOrientation orientation) {
207216
((RotationEventListener) dispatcher).afterRotation(driver, orientation);
208217
}
209-
}
218+
}

src/main/java/io/appium/java_client/events/api/general/ElementEventListener.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ public interface ElementEventListener extends Listener {
4747
*/
4848
void beforeChangeValueOf(WebElement element, WebDriver driver);
4949

50+
/**
51+
* Called before {@link org.openqa.selenium.WebElement#clear WebElement.clear()},
52+
* {@link org.openqa.selenium.WebElement#sendKeys WebElement.sendKeys(...)}.
53+
*
54+
* @param driver WebDriver
55+
* @param element the WebElement being used for the action
56+
*/
57+
void beforeChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend);
58+
5059
/**
5160
* Called after {@link org.openqa.selenium.WebElement#clear WebElement.clear()},
5261
* {@link org.openqa.selenium.WebElement#sendKeys WebElement.sendKeys(...)} .
@@ -56,4 +65,14 @@ public interface ElementEventListener extends Listener {
5665
* @param element the WebElement being used for the action
5766
*/
5867
void afterChangeValueOf(WebElement element, WebDriver driver);
59-
}
68+
69+
/**
70+
* Called after {@link org.openqa.selenium.WebElement#clear WebElement.clear()},
71+
* {@link org.openqa.selenium.WebElement#sendKeys WebElement.sendKeys(...)} .
72+
* Not called, if an exception is thrown.
73+
*
74+
* @param driver WebDriver
75+
* @param element the WebElement being used for the action
76+
*/
77+
void afterChangeValueOf(WebElement element, WebDriver driver, CharSequence[] keysToSend);
78+
}

src/test/java/io/appium/java_client/events/EmptyWebDriver.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.openqa.selenium.Capabilities;
1414
import org.openqa.selenium.ContextAware;
1515
import org.openqa.selenium.Cookie;
16+
import org.openqa.selenium.DeviceRotation;
1617
import org.openqa.selenium.HasCapabilities;
1718
import org.openqa.selenium.JavascriptExecutor;
1819
import org.openqa.selenium.NoSuchElementException;
@@ -61,10 +62,18 @@ private static List<StubWebElement> createStubList() {
6162
//The rotation does nothing there
6263
}
6364

65+
@Override public void rotate(DeviceRotation rotation) {
66+
//The rotation does nothing there
67+
}
68+
6469
@Override public ScreenOrientation getOrientation() {
6570
return null;
6671
}
6772

73+
@Override public DeviceRotation rotation() {
74+
return null;
75+
}
76+
6877
@Override public void get(String url) {
6978
//There is no navigation. It is added only for event firing
7079
}

src/test/java/io/appium/java_client/events/listeners/AppiumListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ public class AppiumListener extends TestListener implements AppiumWebDriverEvent
6363
messages.add("WebDriverEventListener: Thee element was clicked");
6464
}
6565

66+
@Override public void beforeChangeValueOf(WebElement element, WebDriver driver,
67+
CharSequence[] keysToSend) {
68+
messages.add("WebDriverEventListener: Attempt to click on the element");
69+
}
70+
6671
@Override public void beforeChangeValueOf(WebElement element, WebDriver driver) {
6772
messages.add("WebDriverEventListener: Attempt to change value of the element");
6873
}
@@ -71,6 +76,11 @@ public class AppiumListener extends TestListener implements AppiumWebDriverEvent
7176
messages.add("WebDriverEventListener: The value of the element was changed");
7277
}
7378

79+
@Override public void afterChangeValueOf(WebElement element, WebDriver driver,
80+
CharSequence[] keysToSend) {
81+
messages.add("WebDriverEventListener: Thee element was clicked");
82+
}
83+
7484
@Override public void beforeScript(String script, WebDriver driver) {
7585
messages.add("WebDriverEventListener: Attempt to perform java script: " + script);
7686
}

src/test/java/io/appium/java_client/events/listeners/ElementListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ public class ElementListener extends TestListener implements ElementEventListene
1818
messages.add("Attempt to change value of the element");
1919
}
2020

21+
@Override public void beforeChangeValueOf(WebElement element, WebDriver driver,
22+
CharSequence[] keysToSend) {
23+
messages.add("Attempt to change value of the element");
24+
}
25+
2126
@Override public void afterChangeValueOf(WebElement element, WebDriver driver) {
2227
messages.add("The value of the element was changed");
2328
}
2429

30+
@Override public void afterChangeValueOf(WebElement element, WebDriver driver,
31+
CharSequence[] keysToSend) {
32+
messages.add("The value of the element was changed");
33+
}
34+
2535
@Override protected void add() {
2636
SingleListeners.listeners.put(ElementListener.class, this);
2737
}

src/test/java/io/appium/java_client/events/listeners/ElementListener2.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,20 @@ public class ElementListener2 extends TestListener implements ElementEventListen
1818
messages.add("Externally defined listener: Attempt to change value of the element");
1919
}
2020

21+
@Override public void beforeChangeValueOf(WebElement element, WebDriver driver,
22+
CharSequence[] keysToSend) {
23+
messages.add("Externally defined listener: Attempt to change value of the element");
24+
}
25+
2126
@Override public void afterChangeValueOf(WebElement element, WebDriver driver) {
2227
messages.add("Externally defined listener: The value of the element was changed");
2328
}
2429

30+
@Override public void afterChangeValueOf(WebElement element, WebDriver driver,
31+
CharSequence[] keysToSend) {
32+
messages.add("Externally defined listener: The value of the element was changed");
33+
}
34+
2535
@Override protected void add() {
2636
SingleListeners.listeners.put(ElementListener2.class, this);
2737
}

0 commit comments

Comments
 (0)