appium / appium-flutter-driver

Appium Flutter Driver is a test automation tool for Flutter apps on multiple platforms/OSes. Appium Flutter Driver is part of the Appium mobile test automation tool maintained by community
MIT License
440 stars 179 forks source link

Flutter Finder Doesn't Raise Exception if Element Doesn't Exists #697

Closed sonumuto closed 2 weeks ago

sonumuto commented 4 weeks ago

Hi, We are trying to automate a Flutter/Native Android application using Python. We have noticed that if element does not exist in the current page, it does not raise an exception which differs from the expected behavior of the find element. It seems like application is stuck in find element.


import unittest
import time
from typing import Dict, Any

from appium import webdriver
from appium.options.common import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
from appium_flutter_finder import FlutterFinder, FlutterElement

cap: Dict[str, Any] = {
    'platformName': 'Android',
    'automationName': 'flutter',
    'uuid': '**********',
    'platformVersion': '13',
    'app': '/Users/test/Desktop/app-organization.apk',
}
appium_server_url = 'http://localhost:4723'

driver = webdriver.Remote(appium_server_url, options=AppiumOptions().load_capabilities(cap))

###############Flutter##################################
#Change the context to FLUTTER
driver.switch_to.context("NATIVE_APP")
driver.switch_to.context("FLUTTER")
#Init Fluttter finder class
finder = FlutterFinder()

key_finder = finder.by_text('start')
button_element = FlutterElement(driver, key_finder)
print(button_element.text)
button_element.click()

key_finder = finder.by_text("Skip")
goto_next_route_element = FlutterElement(driver, key_finder)
goto_next_route_element.click()
###############Flutter##################################
###############Native##################################
driver.switch_to.context("NATIVE_APP")
#Wait async operation
time.sleep(2)

driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/base_button").click()
driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/et").send_keys('zzzzzzzzz')
driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/btn_continue").click()

driver.switch_to.context("FLUTTER")

textfield_key_finder = finder.by_value_key("xNameTextFieldKey")
textfield_element = FlutterElement(driver, textfield_key_finder)
textfield_element.send_keys("Example Text")
time.sleep(3)

driver.implicitly_wait(20)
driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/et_x").send_keys('x@y.com')
driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/et_y").send_keys('53634643')
driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/btn_z").click()

driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/btn_a").click()
driver.implicitly_wait(20)
try:
    driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Show me").click()
except:
    driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/btn_w").click()

try:
    driver.find_element(by=AppiumBy.ID, value="com.organization.app:id/btn_w").click()
except:
    pass

driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="Settings").click()

driver.find_element(by=AppiumBy.ACCESSIBILITY_ID, value="settingsTextKey").click()

###############Native##################################
#Switch the context to FLUTTER

###############FLUTTER##################################
driver.switch_to.context("FLUTTER")

textfield_key_finder = finder.by_value_key("TextFieldKey")
textfield_element = FlutterElement(driver, textfield_key_finder)
textfield_element.send_keys("Example Text")
time.sleep(3)

textfield_element.clear()

pw_key_finder = finder.by_value_key("extFieldKey")
pw_element = FlutterElement(driver, pw_key_finder)
pw_element.send_keys("58475684")

time.sleep(10)
driver.quit()
sonumuto commented 3 weeks ago

Any ideas on possible root causes? @KazuCocoa

KazuCocoa commented 3 weeks ago

I guess it is flutter_driver internal behavior. https://github.com/appium/appium-flutter-driver/issues/693 could be a workaround.

KazuCocoa commented 2 weeks ago

https://github.com/appium/appium-flutter-driver?tab=readme-ov-file#troubleshooting added the waitFor method as part of troubleshooting. The docs can be improved later of course.

sonumuto commented 1 week ago

Thanks Kazu!