Skip to content

Commit e6a2790

Browse files
committed
reimplemented ScrollTo() and ScrollToExact()
1 parent 8f2d86e commit e6a2790

File tree

7 files changed

+127
-11
lines changed

7 files changed

+127
-11
lines changed

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,20 @@ public void lockScreen(int seconds) {
491491
execute(LOCK, ImmutableMap.of("seconds", seconds));
492492
}
493493

494+
/**
495+
* Scroll to an element which contains the given text.
496+
* Implemented differently on iOS and Android, see docs for individual methods.
497+
* @param text
498+
*/
499+
public abstract void scrollTo(String text);
500+
501+
/**
502+
* Scroll to an element with the given text.
503+
* Implemented differently on iOS and Android, see docs for individual methods.
504+
* @param text
505+
*/
506+
public abstract void scrollToExact(String text);
507+
494508
@Override
495509
public WebDriver context(String name) {
496510
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: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,12 @@
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;
2522
import io.appium.java_client.remote.MobileCapabilityType;
26-
2723
import org.apache.commons.codec.binary.Base64;
2824
import org.junit.After;
2925
import org.junit.Before;
3026
import org.junit.Test;
27+
import org.openqa.selenium.WebElement;
3128
import org.openqa.selenium.remote.DesiredCapabilities;
3229

3330
import java.io.File;
@@ -144,18 +141,32 @@ public void ignoreUnimportantViews() {
144141
}
145142

146143
@Test
147-
public void startActivityInThisAppTest(){
144+
public void startActivityInThisAppTest() {
148145
driver.startActivity("io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity", null, null);
149146
String activity = driver.currentActivity();
150147
assertTrue(activity.contains("Node"));
151148
}
152149

153150
@Test
154-
public void startActivityInAnotherAppTest(){
151+
public void startActivityInAnotherAppTest() {
155152
driver.startActivity("com.android.contacts", ".ContactsListActivity", null, null);
156153
String activity = driver.currentActivity();
157154
assertTrue(activity.contains("Contact"));
158155
}
159156

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

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@
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;
24-
2523
import org.junit.After;
2624
import org.junit.Before;
2725
import org.junit.Test;
26+
import org.openqa.selenium.Point;
2827
import org.openqa.selenium.remote.DesiredCapabilities;
2928

3029
import java.io.File;
3130
import java.net.URL;
3231

3332
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotEquals;
3434

3535
/**
3636
* Test Mobile Driver features
@@ -78,4 +78,22 @@ public void hideKeyboardWithParametersTest() {
7878
element.click();
7979
driver.hideKeyboard(HideKeyboardStrategy.PRESS_KEY, "Done");
8080
}
81+
82+
@Test
83+
public void scrollToTest() {
84+
MobileElement searchBar = (MobileElement) driver.findElementByName("Search Bars");
85+
Point before = searchBar.getLocation();
86+
driver.scrollTo("Search Ba");
87+
Point after = searchBar.getLocation();
88+
assertNotEquals(before, after);
89+
}
90+
91+
@Test
92+
public void scrollToExactTest() {
93+
MobileElement searchBar = (MobileElement) driver.findElementByName("Search Bars");
94+
Point before = searchBar.getLocation();
95+
driver.scrollToExact("Search Bars");
96+
Point after = searchBar.getLocation();
97+
assertNotEquals(before, after);
98+
}
8199
}

0 commit comments

Comments
 (0)