1. react native 使用 appium
1.1. Appium
参考 Find Elements Appium 通过 AccessibilityId 找到元素
List<MobileElement> elementsOne = (MobileElement) driver.findElementsByAccessibilityId("SomeAccessibilityID");
Accessibility ID
:
- iOS 对应
accessibilityIdentifier
- Android 对应
ContentDescription
1.2. React Native
使用 RN View 的 testID
或者 accessibilityLabel
1.2.1. iOS
accessibilityLabel
对应accessibilityLabel
testID
对应accessibilityIdentifier
RCTViewManager.m
:
RCT_REMAP_VIEW_PROPERTY(accessibilityLabel, reactAccessibilityElement.accessibilityLabel, NSString)
RCT_REMAP_VIEW_PROPERTY(testID, reactAccessibilityElement.accessibilityIdentifier, NSString)
1.2.2. Android
accessibilityLabel
对应ContentDescription
testID
对应Tag
BaseViewManager.java
:
private static final String PROP_ACCESSIBILITY_LABEL = "accessibilityLabel";
public static final String PROP_TEST_ID = "testID";
@ReactProp(name = PROP_ACCESSIBILITY_LABEL)
public void setAccessibilityLabel(T view, String accessibilityLabel) {
view.setContentDescription(accessibilityLabel);
}
@ReactProp(name = PROP_TEST_ID)
public void setTestId(T view, String testId) {
view.setTag(R.id.react_test_id, testId);
// temporarily set the tag and keyed tags to avoid end to end test regressions
view.setTag(testId);
}
1.3. 神策:
1.3.1. iOS
$element_content
对应accessibilityLabel
[properties setValue:[uiView.accessibilityLabel stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] forKey:@"$element_content"];
1.3.2. Android
$element_content
对应getText
1.4. 结论
Platform | System | ||
---|---|---|---|
React Native | JS | testID | accessibilityLabel |
React Native | iOS | accessibilityIdentifier | accessibilityLabel |
React Native | Android | Tag | ContentDescription |
Appium | iOS | AccessibilityId | - |
Appium | Android | - | AccessibilityId |
所以 Appium 在 React Native 中使用 AccessibilityId,必须同时设置 React Native 的 testID 和 accessibilityLabel 为同一值
1.5. References
- Is there any UI testing framework for React Native apps?
- Finding elements by Accessibility ID
- appium App 测试
- Selenium Web 测试
- Cavy react native 测试,react 方式获取 ref
- https://github.com/facebook/react-native/pull/9942 appium 支持 react native 的讨论
- Appium + React Native Quickstart