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

Add class chain locator type for iOS XCUITest-based driver #599

Merged
merged 2 commits into from
Mar 22, 2017
Merged
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
32 changes: 32 additions & 0 deletions src/main/java/io/appium/java_client/FindsByIosClassChain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client;

import org.openqa.selenium.WebElement;

import java.util.List;

public interface FindsByIosClassChain<T extends WebElement> extends FindsByFluentSelector<T> {

default T findElementByIosClassChain(String using) {
return findElement(MobileSelector.IOS_CLASS_CHAIN.toString(), using);
}

default List<T> findElementsByIosClassChain(String using) {
return findElements(MobileSelector.IOS_CLASS_CHAIN.toString(), using);
}
}
67 changes: 67 additions & 0 deletions src/main/java/io/appium/java_client/MobileBy.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@ public static By AccessibilityId(final String accessibilityId) {
return new ByAccessibilityId(accessibilityId);
}

/**
* This locator strategy is available in XCUITest Driver mode
* @param iOSClassChainString is a valid class chain locator string.
* See <a href="https://github.com/facebook/WebDriverAgent/wiki/Queries">
* the documentation</a> for more details
* @return an instance of {@link io.appium.java_client.MobileBy.ByIosClassChain}
*/
public static By iOSClassChain(final String iOSClassChainString) {
return new ByIosClassChain(iOSClassChainString);
}

/**
* This locator strategy is available in XCUITest Driver mode
* @param iOSNsPredicateString is an an iOS NsPredicate String
Expand Down Expand Up @@ -290,6 +301,62 @@ public List<WebElement> findElements(SearchContext context) throws WebDriverExce
}
}

public static class ByIosClassChain extends MobileBy implements Serializable {

protected ByIosClassChain(String locatorString) {
super(MobileSelector.IOS_CLASS_CHAIN, locatorString);
}

/**
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@SuppressWarnings("unchecked")
@Override public List<WebElement> findElements(SearchContext context) {
Class<?> contextClass = context.getClass();

if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {
return FindsByIosClassChain.class.cast(context)
.findElementsByIosClassChain(getLocatorString());
}

if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
return super.findElements(context);
}

throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,
FindsByFluentSelector.class);
}

/**
* @throws WebDriverException when current session doesn't support the given selector or when
* value of the selector is not consistent.
* @throws IllegalArgumentException when it is impossible to find something on the given
* {@link SearchContext} instance
*/
@Override public WebElement findElement(SearchContext context) {
Class<?> contextClass = context.getClass();

if (FindsByIosClassChain.class.isAssignableFrom(contextClass)) {
return FindsByIosClassChain.class.cast(context)
.findElementByIosClassChain(getLocatorString());
}

if (FindsByFluentSelector.class.isAssignableFrom(context.getClass())) {
return super.findElement(context);
}

throw formIllegalArgumentException(contextClass, FindsByIosClassChain.class,
FindsByFluentSelector.class);
}

@Override public String toString() {
return "By.IosClassChain: " + getLocatorString();
}
}

public static class ByIosNsPredicate extends MobileBy implements Serializable {

protected ByIosNsPredicate(String locatorString) {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/io/appium/java_client/MobileSelector.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public enum MobileSelector {
ANDROID_UI_AUTOMATOR("-android uiautomator"),
IOS_UI_AUTOMATION("-ios uiautomation"),
IOS_PREDICATE_STRING("-ios predicate string"),
IOS_CLASS_CHAIN("-ios class chain"),
WINDOWS_UI_AUTOMATION("-windows uiautomation");

private final String selector;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSDriver.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static io.appium.java_client.MobileCommand.prepareArguments;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.FindsByIosClassChain;
import io.appium.java_client.FindsByIosNSPredicate;
import io.appium.java_client.FindsByIosUIAutomation;
import io.appium.java_client.HidesKeyboardWithKeyName;
Expand Down Expand Up @@ -50,7 +51,8 @@
public class IOSDriver<T extends WebElement>
extends AppiumDriver<T>
implements HidesKeyboardWithKeyName, ShakesDevice,
FindsByIosUIAutomation<T>, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate<T> {
FindsByIosUIAutomation<T>, LocksIOSDevice, PerformsTouchID, FindsByIosNSPredicate<T>,
FindsByIosClassChain<T> {

private static final String IOS_PLATFORM = MobilePlatform.IOS;

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/io/appium/java_client/ios/IOSElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@

package io.appium.java_client.ios;

import io.appium.java_client.FindsByIosClassChain;
import io.appium.java_client.FindsByIosNSPredicate;
import io.appium.java_client.FindsByIosUIAutomation;
import io.appium.java_client.MobileElement;

public class IOSElement extends MobileElement
implements FindsByIosUIAutomation<MobileElement>, FindsByIosNSPredicate<MobileElement> {
implements FindsByIosUIAutomation<MobileElement>, FindsByIosNSPredicate<MobileElement>,
FindsByIosClassChain<MobileElement> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ enum Strategies {
.windowsAutomation(getValue(annotation, this));
}
},
BY_CLASS_CHAIN("iOSClassChain") {
@Override By getBy(Annotation annotation) {
return MobileBy
.iOSClassChain(getValue(annotation, this));
}
},
BY_NS_PREDICATE("iOSNsPredicate") {
@Override By getBy(Annotation annotation) {
return MobileBy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@
@Repeatable(iOSXCUITFindBySet.class)
public @interface iOSXCUITFindBy {

/**
* The Class Chain locator is similar to xpath, but it's faster and can only
* search direct children elements. See the
* <a href="https://github.com/facebook/WebDriverAgent/wiki/Queries">
* documentation</a> for more details.
*/
String iOSClassChain() default "";

/**
* The NSPredicate class is used to define logical conditions used to constrain
* a search either for a fetch or for in-memory filtering.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@

import static io.appium.java_client.pagefactory.LocatorGroupStrategy.ALL_POSSIBLE;
import static io.appium.java_client.pagefactory.LocatorGroupStrategy.CHAIN;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -41,6 +45,7 @@
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.List;
import java.util.function.Supplier;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
Expand Down Expand Up @@ -85,7 +90,14 @@ public class XCUITModeTest extends AppXCUITTest {
@iOSXCUITFindBy(iOSNsPredicate = "name BEGINSWITH 'location'")
private MobileElement locationAlert;

@iOSXCUITFindBy(iOSClassChain = "XCUIElementTypeWindow/*/XCUIElementTypeTextField[2]")
private MobileElement secondTextField;

@iOSXCUITFindBy(iOSClassChain = "XCUIElementTypeWindow/*/XCUIElementTypeButton[-1]")
private MobileElement lastButton;

@iOSXCUITFindBy(iOSClassChain = "XCUIElementTypeWindow/*/XCUIElementTypeButton")
private List<MobileElement> allButtons;

/**
* The setting up.
Expand Down Expand Up @@ -133,6 +145,18 @@ public class XCUITModeTest extends AppXCUITTest {
assertTrue(locationAlert.isDisplayed());
}

@Test public void findElementByClassChain() {
assertThat(secondTextField.getAttribute("name"), equalTo("IntegerB"));
}

@Test public void findElementByClassChainWithNegativeIndex() {
assertThat(lastButton.getAttribute("name"), equalTo("Test Gesture"));
}

@Test public void findMultipleElementsByClassChain() {
assertThat(allButtons.size(), is(greaterThan(1)));
}

@Test public void findElementByXUISelectorTest() {
assertNotNull(gesture.getText());
}
Expand Down