Skip to content

Instantly share code, notes, and snippets.

@semutmerah
Last active September 17, 2018 17:39
Show Gist options
  • Save semutmerah/230d8d4afe668fe545219278b7adbe63 to your computer and use it in GitHub Desktop.
Save semutmerah/230d8d4afe668fe545219278b7adbe63 to your computer and use it in GitHub Desktop.
How to adding testID in react native
import React from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
export default class App extends React.Component {
render() {
return (
<View style={styles.container}>
<Text {...testID('important-text')}>Hello World!</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
function testID(id) {
return Platform.OS === 'android' ? { accessible: true, accessibilityLabel: id } : { testID: id }
}
@semutmerah
Copy link
Author

When I am working with a project that using react native, I realize it does not support resource-id, which is needed for E2E testing using tools like Appium. There's some attempt to fix this issue on this PR discussion, but if you follow it until the end of discussions, it seems like the PR won't be merged at all.
There's also some workaround for this, based from that discussion, which is by defining a custom function to set testID and then put it in the View Component that you need to set.

This gist basically giving some people (including myself) the basic understanding of how to implement it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment