appium / appium-mac2-driver

Next-gen Appium macOS driver, backed by Apple XCTest
Apache License 2.0
115 stars 24 forks source link

Can't find any labels using XCUIElementTypeStaticText #196

Open arielelkin opened 1 year ago

arielelkin commented 1 year ago

I'm trying to assert that the text in the label says "Hello, world!"

image
import pytest

from appium import webdriver
from appium.options.mac import Mac2Options
from appium.webdriver.common.appiumby import AppiumBy

@pytest.fixture()
def driver():
    options = Mac2Options()
    options.bundle_id = 'com.example.myapp'
    drv = webdriver.Remote('http://127.0.0.1:4723', options=options)
    yield drv
    drv.quit()

def test_edit_text(driver):
    print(driver.page_source)
    my_label = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeStaticText')
    assert(my_label.text == "Hello, world!")

This fails with the following error:

FAILED tester.py::test_edit_text - AssertionError: assert 'AppiumTestApp' == 'Hello, world!'

But the textfield is definitely in the page source!

<?xml version="1.0" encoding="UTF-8"?>
<XCUIElementTypeApplication elementType="2" identifier="" label="" title="AppiumTestApp" enabled="false" selected="false" x="0" y="0" width="0" height="0">
  <XCUIElementTypeWindow elementType="4" identifier="AppiumTestApp.ContentView-1-AppWindow-1" label="" title="AppiumTestApp" enabled="false" selected="false" x="390" y="313" width="900" height="450">
    <XCUIElementTypeGroup elementType="3" identifier="" label="" title="" enabled="false" selected="false" x="390" y="313" width="900" height="450">
      <XCUIElementTypeImage elementType="43" identifier="" label="Globe" title="" enabled="true" selected="false" x="831" y="520" width="18" height="17"/>
      <XCUIElementTypeStaticText elementType="48" identifier="" value="Hello, world!" label="" title="" enabled="true" selected="false" x="802" y="538" width="76" height="17"/>

Here's the very small demo app I'm trying to test: https://github.com/arielelkin/macostestapp

jlipps commented 1 year ago

It's possible that get text is retrieving the label attribute. You could try using the get attribute command and getting the value attribute instead? Just a thought. I haven't used this driver before.

arielelkin commented 1 year ago

I've tried following your suggestion in a couple of ways.

1

    my_label = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeStaticText')
    assert(my_label.value == "Hello, world!")

Produces this error message

FAILED tester.py::test_edit_text - AttributeError: 'WebElement' object has no attribute 'value'

2

    my_label = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeStaticText')
    assert(my_label.get_attribute('text') == "Hello, world!")

Produces this error message

FAILED tester.py::test_edit_text - selenium.common.exceptions.InvalidArgumentException: Message: The att...
jlipps commented 1 year ago

did you try

my_label.get_attribute('value`)

?

arielelkin commented 1 year ago
    my_label = driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeStaticText')
    assert(my_label.get_attribute('value') == "Hello, world!")

Produces this error:

FAILED tester.py::test_edit_text - AssertionError: assert 'AppiumTestApp' == 'Hello, world!'

Which is weird, it looks like driver.find_element(by=AppiumBy.CLASS_NAME, value='XCUIElementTypeStaticText') retrieves the entire app?

jlipps commented 1 year ago

That doesn't seem right unless your app has accessibility text configured incorrectly. @mykola-mokhnach I'm not sure where to go from here given I don't use this driver. Any ideas on your end?

mykola-mokhnach commented 1 year ago

I don't see any issues there. We display exactly what XCTest returns to us. If some values are not present then either a wrong element is located or XCTest fetches unexpected values for it

arielelkin commented 1 year ago

@jlipps @mykola-mokhnach

The app I'm testing is just Xcode's macOS App template, but it's as bare-bones as can be (except for the TextField I've added but which really shouldn't be interfering)

Are you able to reproduce the issue? Here's the small demo app: https://github.com/arielelkin/macostestapp

arielelkin commented 1 year ago

@jlipps @mykola-mokhnach following up on this 😃

I was wondering if you were able to reproduce the issue? Or have any suggestions as to why the XCUIElementTypeStaticText identifier isn't working..