Skip to content

Commit cd5bc3e

Browse files
committed
Reimplemented ScrollTo() and ScrollToExact()
2 parents d9c5ab4 + e6a2790 commit cd5bc3e

File tree

7 files changed

+133
-9
lines changed

7 files changed

+133
-9
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,21 @@ public void lockScreen(int seconds) {
525525
execute(LOCK, ImmutableMap.of("seconds", seconds));
526526
}
527527

528+
/**
529+
* Scroll to an element which contains the given text.
530+
* Implemented differently on iOS and Android, see docs for individual methods.
531+
* @param text
532+
*/
533+
public abstract void scrollTo(String text);
534+
535+
/**
536+
* Scroll to an element with the given text.
537+
* Implemented differently on iOS and Android, see docs for individual methods.
538+
* @param text
539+
*/
540+
public abstract void scrollToExact(String text);
541+
542+
528543
@Override
529544
public WebDriver context(String name) {
530545
if (!_isNotNullOrEmpty(name)) {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,15 @@
1717

1818
package io.appium.java_client;
1919

20-
import java.util.List;
21-
20+
import com.google.common.collect.ImmutableMap;
2221
import org.openqa.selenium.By;
2322
import org.openqa.selenium.Dimension;
2423
import org.openqa.selenium.Point;
2524
import org.openqa.selenium.WebElement;
2625
import org.openqa.selenium.remote.FileDetector;
2726
import org.openqa.selenium.remote.RemoteWebElement;
2827

29-
import com.google.common.collect.ImmutableMap;
28+
import java.util.List;
3029

3130
public class MobileElement extends RemoteWebElement implements FindsByAccessibilityId, FindsByAndroidUIAutomator,
3231
FindsByIosUIAutomation {

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,34 @@ public AndroidDriver(URL remoteAddress, Capabilities desiredCapabilities) {
3939
ANDROID_PLATFORM));
4040
}
4141

42+
/**
43+
* Scroll forward to the element which has a description or name which contains the input text.
44+
* The scrolling is performed on the first scrollView present on the UI
45+
* @param text
46+
*/
47+
@Override
48+
public void scrollTo(String text) {
49+
String uiScrollables = UiScrollable("new UiSelector().descriptionContains(\"" + text + "\")") +
50+
UiScrollable("new UiSelector().textContains(\"" + text + "\")");
51+
findElementByAndroidUIAutomator(uiScrollables);
52+
}
53+
54+
/**
55+
* Scroll forward to the element which has a description or name which exactly matches the input text.
56+
* The scrolling is performed on the first scrollView present on the UI
57+
* @param text
58+
*/
59+
@Override
60+
public void scrollToExact(String text) {
61+
String uiScrollables = UiScrollable("new UiSelector().description(\"" + text + "\")") +
62+
UiScrollable("new UiSelector().text(\"" + text + "\")");
63+
findElementByAndroidUIAutomator(uiScrollables);
64+
}
65+
66+
private String UiScrollable(String uiSelector) {
67+
return "new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(" + uiSelector + ".instance(0));";
68+
}
69+
4270
/**
4371
* @param key
4472
* code for the key pressed on the Android device

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,52 @@ public IOSDriver(URL remoteAddress, Capabilities desiredCapabilities) {
2323
IOS_PLATFORM));
2424
}
2525

26+
/**
27+
* Scroll to the element whose 'text' attribute contains the input text.
28+
* This scrolling happens within the first UIATableView on the UI. Use the additional 'context' param to specify a different scrollView.
29+
* @param text input text contained in text attribute
30+
*/
31+
@Override
32+
public void scrollTo(String text) {
33+
scrollTo(text, (MobileElement) findElementByClassName("UIATableView"));
34+
}
35+
36+
/**
37+
* Scroll to the element whose 'text' attribute is equal to the input text.
38+
* This scrolling happens within the first UIATableView on the UI. Use the additional 'context' param to specify a different scrollView.
39+
* @param text input text to match
40+
*/
41+
@Override
42+
public void scrollToExact(String text) {
43+
scrollToExact(text, (MobileElement) findElementByClassName("UIATableView"));
44+
}
45+
46+
/**
47+
* Scroll to the element whose 'text' attribute contains the input text.
48+
* @param text input text contained in text attribute
49+
* @param context container element to scroll within
50+
*/
51+
public void scrollTo(String text, MobileElement context) {
52+
context.findElementByIosUIAutomation(".scrollToElementWithPredicate(\"name CONTAINS '" + text + "'\")");
53+
}
54+
55+
/**
56+
* Scroll to the element whose 'text' attribute is equal to the input text.
57+
* @param text input text to match
58+
* @param context container element to scroll within
59+
*/
60+
public void scrollToExact(String text, MobileElement context) {
61+
context.findElementByIosUIAutomation(".scrollToElementWithName(\"" + text + "\")");
62+
}
63+
64+
/**
65+
* Scroll to the given element.
66+
* This scrolling happens within the first UIATableView on the UI. Use the ScrollToExactWithinContext() method to specify a different scrollView.
67+
*/
68+
public void scrollTo(WebElement el) {
69+
scrollToExact(el.getText());
70+
}
71+
2672
/**
2773
* @see IOSDeviceActionShortcuts#hideKeyboard(String, String)
2874
*/
11.8 MB
Binary file not shown.

src/test/java/io/appium/java_client/android/AndroidDriverTest.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@
1919

2020
import io.appium.java_client.AppiumSetting;
2121
import io.appium.java_client.NetworkConnectionSetting;
22-
import io.appium.java_client.android.AndroidDriver;
23-
import io.appium.java_client.android.AndroidKeyCode;
24-
import io.appium.java_client.android.AndroidKeyMetastate;
22+
2523
import io.appium.java_client.remote.MobileCapabilityType;
2624

2725
import org.apache.commons.codec.binary.Base64;
2826
import org.junit.After;
2927
import org.junit.Before;
3028
import org.junit.Test;
29+
import org.openqa.selenium.WebElement;
3130
import org.openqa.selenium.remote.DesiredCapabilities;
3231

3332
import java.io.File;
@@ -144,18 +143,32 @@ public void ignoreUnimportantViews() {
144143
}
145144

146145
@Test
147-
public void startActivityInThisAppTest(){
146+
public void startActivityInThisAppTest() {
148147
driver.startActivity("io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity", null, null);
149148
String activity = driver.currentActivity();
150149
assertTrue(activity.contains("Node"));
151150
}
152151

153152
@Test
154-
public void startActivityInAnotherAppTest(){
153+
public void startActivityInAnotherAppTest() {
155154
driver.startActivity("com.android.contacts", ".ContactsListActivity", null, null);
156155
String activity = driver.currentActivity();
157156
assertTrue(activity.contains("Contact"));
158157
}
159158

160159
//TODO hideKeyboard() test
160+
161+
@Test
162+
public void scrollToTest() {
163+
driver.scrollTo("View");
164+
WebElement views = driver.findElementByAccessibilityId("Views");
165+
assertNotNull(views);
166+
}
167+
168+
@Test
169+
public void scrollToExactTest() {
170+
driver.scrollTo("Views");
171+
WebElement views = driver.findElementByAccessibilityId("Views");
172+
assertNotNull(views);
173+
}
161174
}

src/test/java/io/appium/java_client/ios/IOSDriverTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,25 @@
1818
package io.appium.java_client.ios;
1919

2020
import io.appium.java_client.MobileElement;
21-
import io.appium.java_client.ios.IOSDriver;
2221
import io.appium.java_client.remote.HideKeyboardStrategy;
2322
import io.appium.java_client.remote.MobileCapabilityType;
2423

2524
import org.junit.After;
2625
import org.junit.Before;
2726
import org.junit.Test;
27+
28+
import org.openqa.selenium.Point;
29+
2830
import org.openqa.selenium.remote.DesiredCapabilities;
2931

3032
import java.io.File;
3133
import java.net.URL;
3234

3335
import static org.junit.Assert.assertEquals;
3436

37+
import static org.junit.Assert.assertNotEquals;
38+
39+
3540
/**
3641
* Test Mobile Driver features
3742
*/
@@ -78,4 +83,22 @@ public void hideKeyboardWithParametersTest() {
7883
element.click();
7984
driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");
8085
}
86+
87+
@Test
88+
public void scrollToTest() {
89+
MobileElement searchBar = (MobileElement) driver.findElementByName("Search Bars");
90+
Point before = searchBar.getLocation();
91+
driver.scrollTo("Search Ba");
92+
Point after = searchBar.getLocation();
93+
assertNotEquals(before, after);
94+
}
95+
96+
@Test
97+
public void scrollToExactTest() {
98+
MobileElement searchBar = (MobileElement) driver.findElementByName("Search Bars");
99+
Point before = searchBar.getLocation();
100+
driver.scrollToExact("Search Bars");
101+
Point after = searchBar.getLocation();
102+
assertNotEquals(before, after);
103+
}
81104
}

0 commit comments

Comments
 (0)