Search code examples
appiumreact-native

Wait for Android app to display a React Native button and then touch it


I'm developing a React Native app for Android and I've been learning and trying to do my first test in Ruby. Right now I would like my test to wait for the app's main screen to load and then touch a React 'TouchableOpacity' button. The React Native code is the following:

<View style={styles.row}>
    <View style={styles.buttonContainer}>
        <TouchableOpacity style={styles.buttonIcon} onPress={this.goToFaqsList.bind(this)} accessible={true} accessibility_id={'FAQs'}>
        <Icon style={styles.icon} size={32} name="help" color="white"> </Icon>
        </TouchableOpacity>
        <Text style={styles.buttonText}>FAQ's</Text>
    </View>
<View style={styles.buttonContainer}>

The test:

require 'rubygems'
require 'appium_lib'

capabilities = {
  platformName:  'Android',
  deviceName:    'Android',
  app:           '~/workspace/maps2/android/app/build/outputs/apk/app-debug.apk',
}

server_url = "http://0.0.0.0:4723/wd/hub"

# Start the driver
Appium::Driver.new(caps: capabilities).start_driver

Appium.promote_appium_methods Object

wait = Selenium::WebDriver::Wait.new :timeout => 10
wait.until { elementByAccessibilityId("FAQs").displayed? }

source
mobileElement = elementByAccessibilityId("FAQs")
puts(mobileElement)

driver_quit

The " elementByAccessibilityId " was an approach I saw in another topic but I'm getting an error:

Failure/Error: wait.until { elementByAccessibilityId("FAQs").displayed? }

NoMethodError:
undefined method `elementByAccessibilityId' for main:Object

Can someone tell my mistake and point me in the right direction? Thanks.


Solution

  • I got it working: in the React Native code I changed the attribute accessibility_id={'FAQs'} to accessibilityLabel={'FAQ'}, and in the ruby test file the correct method is find_element(:accessibility_id, 'FAQs'). And it's working! Now I can .click() the button and continue testing.

    https://github.com/appium/ruby_lib/blob/master/docs/android_docs.md to see what other functions we have available to use.