Using Appium for iOS to Get Text of Element

I am a developer using Appium to drive automated testing in an iOS app. We have been tapping into the accessibilityLabels to test, however the accessibilityLabels and actual text of the label is not always the same.

We would like to access the actual text attribute of the element to verify that it is correct. It would be great if we could tap into the size and location of the iOS element as well. Any help with this would be greatly appreciated.

Thanks!

I’ll start the answer and maybe someone will complete it!

As far as I know in ios you have element.text and element.name and the first should be the value of the element and the second the accessibility label. p.e.

first_tag(:UIAStaticText).name
first_tag(:UIAStaticText).text

as far as the element position to tap it, you should be abe to do so with something similar to:

element = first_tag(:UIAStaticText)

positionY = element.getLocation().getY() + (element.getSize().getHeight() / 2);
positionX = element.getLocation().getX() + (element.getSize().getWidth() / 2);

And then tap it

TouchAction elementTap = new TouchAction(driver).tap(positionX,positionY);
elementTap.perform();

Note: surely I have typos on above code. I tried to write it in java, but I’m using ruby. Also you should not call getLocation and getSize so often, call it once and save the array results and the those variables, in order to decrease the calls to appium.

Can you explain more about the first_tag function? Do you mean first_ele? Because looking in the Appium documentation I see a first_ele function, but not a first_tag function.

http://www.rubydoc.info/gems/appium_lib/0.6.7/Appium/Common

Also, how do you recommend we test that this works? We are writing tests in a ruby file, but it looks like we only have access to the Selenium Web Driver Element functions. How do we access the Appium library functions from this file?

my mistake, as you said I mean first_ele, but that its a random example to gather one element, you can use button, text, find_element, etc…

You are testing with arc only? Or you already using cucumber or rspec?

I’m initializing my driver this way:

require 'rspec/expectations'
require 'appium_lib'
require 'cucumber/ast'

class AppiumWorld
end

caps = Appium.load_appium_txt file: File.expand_path('./', __FILE__), verbose: true
Appium::Driver.new(caps)
Appium.promote_appium_methods AppiumWorld

World do
    AppiumWorld.new
end

Before do
    $driver.start_driver
end

After do
    $driver.driver_quit
end

Here are the results of testing the two functions you mentioned.

$driver.first_ele(:UIAStaticText).name

is the accessibilityIdentifier of the element.

$driver.first_ele(:UIAStaticText).text

is the accessibilityLabel of the element.

I am trying to directly access the text in the label of the element, not the accessibilityLabel. Is there a way to do this?

Have you analyze the element in inspector?

Sorry to Crash the thread here, but I believe with Ruby to glean text out of an element the command is
Text(‘element’)

This page has more info and may help. Hope this is helpful!
http://appium.io/slate/en/tutorial/android.html?ruby#page-command

Hi @shermaneric,

sorry to say you are not right. Whats on the link you post is:

To find by text we can use the text command.
» text(‘API Demos’)
Selenium::WebDriver::Element:0x7f5348cbb3447ff2 id=“1”
The text command will look for any text on screen.

This looks for the text ‘API Demos’ and return the element where it founds it. In this case, if there is more than one, this command will return the first it finds.

To get the text from an element, as far as I know its:

element.text

in the above case

» text(‘API Demos’)
Selenium::WebDriver::Element:0x7f5348cbb3447ff2 id=“1”
» text(‘API Demos’).text
“Check out the API Demos”

Ah right. Sorry for the red herring. Hope you find what you are looking for!

This seems to work for me:

element.value

Secondarily is there a reason why you are using the accessibilityLabels rather than accessibilityIdentifiers? I think that accessibilityLabels are specifically intended to be used by iOS for assistive functions, whereas accessibilityIdentifiers are for UIAutomation. Since assistive functions will be looking to read out the labels on each UI Element it would make sense for the system to conceal the text being visually presented on an element if a different accessibilityLabel has been set.

1 Like

good hint… There is also

element.label

But I’m not sure on witch one translate in what on instruments. Just on accessibility elements we have

accessibilityLabel
accessibilityHint
accessibilityValue

Hi Telmo

Is it possible to validate whether the text on the element or label is exist or not?

If so how?

Thanks
Anand Jois

How to handle current screen element which have same element id in previous screen in IOS?