Skip to content

Commit ae6ea41

Browse files
committed
fix: introduce MobileBy.className
The change made in Selenium 4 (SeleniumHQ/selenium@0aaa401#r58091435) broke Appium `class name` selector strategy. The workaround was implemented: `MobileBy#className`.
1 parent dddfd04 commit ae6ea41

File tree

7 files changed

+34
-10
lines changed

7 files changed

+34
-10
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,15 @@ public static By custom(final String selector) {
162162
return new ByCustom(selector);
163163
}
164164

165+
/**
166+
* For IOS it is the full name of the XCUI element and begins with XCUIElementType.
167+
* For Android it is the full name of the UIAutomator2 class (e.g.: android.widget.TextView)
168+
* @param selector the class name of the element
169+
* @return an instance of {@link ByClassName}
170+
*/
171+
public static By className(final String selector) {
172+
return new ByClassName(selector);
173+
}
165174

166175
public static class ByAndroidUIAutomator extends MobileBy implements Serializable {
167176

@@ -270,6 +279,17 @@ public ByAndroidViewTag(String tag) {
270279
return "By.AndroidViewTag: " + getLocatorString();
271280
}
272281
}
282+
283+
public static class ByClassName extends MobileBy implements Serializable {
284+
285+
protected ByClassName(String selector) {
286+
super("class name", selector);
287+
}
288+
289+
@Override public String toString() {
290+
return "By.className: " + getLocatorString();
291+
}
292+
}
273293
}
274294

275295

src/main/java/io/appium/java_client/pagefactory/bys/builder/Strategies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ enum Strategies {
4646
},
4747
BYCLASSNAME("className") {
4848
@Override By getBy(Annotation annotation) {
49-
return By.className(getValue(annotation, this));
49+
return MobileBy.className(getValue(annotation, this));
5050
}
5151
},
5252
BYID("id") {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class AndroidAbilityToUseSupplierTest extends BaseAndroidTest {
2121
driver.findElement(By.id("io.appium.android.apis:id/gallery"));
2222

2323
AndroidElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));
24-
List<MobileElement> images = gallery.findElements(By.className("android.widget.ImageView"));
24+
List<MobileElement> images = gallery.findElements(MobileBy.className("android.widget.ImageView"));
2525
Point location = gallery.getLocation();
2626
Point center = gallery.getCenter();
2727

@@ -48,12 +48,13 @@ public class AndroidAbilityToUseSupplierTest extends BaseAndroidTest {
4848
Activity activity = new Activity("io.appium.android.apis", ".view.Gallery1");
4949
driver.startActivity(activity);
5050
AndroidElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));
51-
List<MobileElement> images = gallery.findElements(By.className("android.widget.ImageView"));
51+
List<MobileElement> images = gallery.findElements(MobileBy.className("android.widget.ImageView"));
5252
int originalImageCount = images.size();
5353

5454
horizontalSwipe.get().perform();
5555

56-
assertNotEquals(originalImageCount, gallery.findElements(By.className("android.widget.ImageView")).size());
56+
assertNotEquals(originalImageCount,
57+
gallery.findElements(MobileBy.className("android.widget.ImageView")).size());
5758
}
5859

5960
@Test public void verticalSwipingWithSupplier() throws Exception {

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
import static org.junit.Assert.assertEquals;
1010
import static org.junit.Assert.assertNotEquals;
1111

12+
import io.appium.java_client.MobileBy;
1213
import io.appium.java_client.MobileElement;
1314
import io.appium.java_client.MultiTouchAction;
1415
import io.appium.java_client.TouchAction;
16+
1517
import org.junit.Before;
1618
import org.junit.Test;
1719
import org.openqa.selenium.By;
@@ -169,7 +171,7 @@ public void setUp() {
169171
driver.startActivity(activity);
170172

171173
AndroidElement gallery = driver.findElement(By.id("io.appium.android.apis:id/gallery"));
172-
List<MobileElement> images = gallery.findElements(By.className("android.widget.ImageView"));
174+
List<MobileElement> images = gallery.findElements(MobileBy.className("android.widget.ImageView"));
173175
int originalImageCount = images.size();
174176
Point location = gallery.getLocation();
175177
Point center = gallery.getCenter();
@@ -180,7 +182,8 @@ public void setUp() {
180182
.moveTo(element(gallery,10,center.y - location.y))
181183
.release();
182184
swipe.perform();
183-
assertNotEquals(originalImageCount, gallery.findElements(By.className("android.widget.ImageView")).size());
185+
assertNotEquals(originalImageCount,
186+
gallery.findElements(MobileBy.className("android.widget.ImageView")).size());
184187
}
185188

186189
@Test public void multiTouchTest() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected boolean assertThatSearchListenerWorks(EmptyWebDriver driver, TestListe
3939
prefix + "The searching for something using By.id: someId2 has beed finished. "
4040
+ "The root element was null"));
4141

42-
driver.findElement(By.id("someId")).findElement(By.className("someClazz"));
42+
driver.findElement(By.id("someId")).findElement(MobileBy.className("someClazz"));
4343

4444
assertThat(listener.messages,
4545
contains(prefix + "Attempt to find something using By.id: someId. The root element is null",
@@ -57,7 +57,7 @@ protected boolean assertThatSearchListenerWorks(EmptyWebDriver driver, TestListe
5757
prefix + "The searching for something using By.className: someClazz has beed finished. "
5858
+ "The root element was io.appium.java_client.events.StubWebElement"));
5959

60-
driver.findElements(By.id("someId2")).get(0).findElements(By.className("someClazz2"));
60+
driver.findElements(By.id("someId2")).get(0).findElements(MobileBy.className("someClazz2"));
6161
assertThat(listener.messages,
6262
contains(prefix + "Attempt to find something using By.id: someId. The root element is null",
6363
prefix + "The searching for something using By.id: someId has beed finished. "

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void setValueTest() {
2929
WebDriverWait wait = new WebDriverWait(driver, 20);
3030

3131
IOSElement slider = wait.until(
32-
driver1 -> driver1.findElement(By.className("XCUIElementTypeSlider")));
32+
driver1 -> driver1.findElement(MobileBy.className("XCUIElementTypeSlider")));
3333
slider.setValue("0%");
3434
assertEquals("0%", slider.getAttribute("value"));
3535
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void touchWithPressureTest() {
7575

7676
IOSTouchAction iosTouchAction = new IOSTouchAction(driver);
7777
iosTouchAction.doubleTap(element(firstField));
78-
IOSElement editingMenu = driver.findElement(By.className("XCUIElementTypeTextField"));
78+
IOSElement editingMenu = driver.findElement(MobileBy.className("XCUIElementTypeTextField"));
7979
assertNotNull(editingMenu);
8080
}
8181
}

0 commit comments

Comments
 (0)