Understanding basics of iOS uiautomation selector strategy for Appium - ios

I am writing UI automation tests for an iOS native app using Appium and gradually realizing how most of the element locating strategies don't reliably work for iOS. Apart from XPath which randomly works, other options that I have are:
Accessibility ID (did not work for me)
name (not every element will have value for 'name' attribute)
class (makes sense when you are working with a list of elements)
iOS UiAutomation predicates (steep learning curve for beginners)
I have been trying to understand how to use iOS UiAutomation locator strategy and find elements using it but it's not working on Appium Inspector. I have referred to these documentations (Appium iOS Predicate reference, Apple UIAutomation reference) but I feel they cater to an advanced Appium user audience who have some knowledge on iOS development, not for beginners.
Currently the element hierarchy that I am trying to find element in is something like this:
My current automation setup is:
XCode 6.3.2
Appium 1.4.8
iOS 8.3
Appium Java Client 3.1.0
What will be the locator I can use to locate the highlighted element using UiAutomation predicate strategy? I have been trying a few options on the Appium Inspector like:
applications()[0].windows()[0].navigationBars()[0].textFields().withPredicate("value == 'Search eBay'")
.textFields().withPredicate("value == 'Search eBay'")
These did not work. What am I doing wrong here? Are there any other documentations which clearly explain iOS UiAutomation locators from ground-up? It will really help if someone can explain these basics.

I have never worked with Appium before but I have worked with UIAutomation in javascript.
You can probably find the element using:
....textFields().firstWithName("Search eBay")
Note that UIAuatomation uses UIAccessibility protocol. The value for UITextField is its accessibilityValue and that one will be equal to the searched text, not the placeholder. Once you type something to the field, you will be able to use value.
Of course, in your case grabbing the first text field would work too, as there is only one in the navigation bar.

Just use this .navigationsBars()["EBUH_whateverstring"].textfields()["Search eBay"].textfields()["Search eBay"].
Better way is to ask dev to add accessibility id in case the code is in Obj-C or accessibility identifier if the app code is in Swift. Otherwise if the passed on element value is dynamic then the test will fail in asserting or doing action upon this element.
Another failsafe method is using array values.
.navigationsBars()[0].textfields()[0].textfields()[0] --> Check the array values of your element is [0] or any other. U can use this appium app to get the array value from where it shows xpath value for the element. Or you can use XCode Instruments if you have access to the code to find the exact value as UIAutomation interprets it.

If you are trying to find elements in Appium you will have to write code to do so. Assuming you are using Java, which is what I am using for code, the way you locate these elements is through the driver, tables, and rows.
What do I mean by this? Each element has an XPath associated with it, so one way of doing this is saying
driver.findElementByXPath("xpath_string_here");
This can be very useful when trying to run assertions, for example. using the above code, let us say we want to assert that its name is valid. we can say:
AssertEquals(driver.findElementByXPath("xpath_string_here").getAttribute("name"), 'Practice Example");
When I mention tables and rows, I mean doing something like this:
MobileElement table = (MobileElement) driver.findElementByXPath("string here");
List<WebElement> rows = driver.findElementByClassName("Class name here");
What does this code do? it creates a variable of type MobileElement which will go through the xPath you want, and then the rows value will find elements of that class name present inside of that table view. So in the above image, I would stop at the XPath for the UIAWindow, and then tell my rows to find the elements using class name of "UIAButton" for example.
At this point it is a matter of a simple loop if you want to run some actions on them such as .click(); using their indexes using the .get(int i) method. So for example: rows.get(i).click();
Does this help you with your question?

Related

How to write xpath if two text field having same class name in appium 1.7.for ios

Can anyone help me to write xpath in appium 1.7 for iOS..
If two class having same name
driver.findElementByClassName("TextField").sendKeys("abc");
driver.findElementByClassName("TextField").sendKeys("1234");
In any case use className is not reliable search strategy as its not a unique.
I usually suggest following rules:
have unique AccessibilityId for most of elements used in automation tests (talk with developers if they agree to fix this)
If not, build unique Xpath with relation to other elements that have AccessibilityId or more unique className
As a temporary solution you can do this:
List<WebElement> textfields = driver.findElementsByClassName("TextField");
textFields.get(0).sendKeys("abc");
textFields.get(1).sendKeys("1234");
since you didn't provide your page source, better print it out with driver.getPageSource() and think of good XPath that you put in:
List<WebElement> textfields = driver.findElementsByXpath(<your xpath>);
This below code working fine for me
driver.findElementByName("No account? Sign up").click();
driver.findElementByClassName("TextField").sendKeys("abc"); driver.findElementByClassName("SecureTextField").sendKeys("12345"); driver.findElementByClassName("SecureTextField").sendKeys(Keys.ENTER);
driver.findElementByXPath("(//XCUIElementTypeSecureTextField[2]").sendKeys("12345");
driver.findElementByXPath("(//XCUIElementTypeTextField)[2]").sendKeys(Keys.ENTER);
driver.findElementByXPath("(//XCUIElementTypeTextField)[2]").sendKeys("9876543210");

Ranorex Spy - The recognition elements on the page (prioritizing attributes)

I have question related to the Ranorex Spy.
Is it possible to recognize (by default) elements of the page by attribute other than id, e.g. data-id
I know that I can modify this later manually for each element (but it is time consuming)
Currently:
\input[#id='..."]
Expected (automatically, by Ranorex Spy):
\input[#data-id='..."]
I personnally did not bother using this (because we use many frameworks and whats desired in one framework is not necessarily whats desired in another one) but I think you can achieve priorisation of XPath rules using the RanoreXPath Weight Rules.
Following is a Ranorex article describing how to do this: http://www.ranorex.com/support/user-guide-20/ranorexpath-weight-rule-library.html
Good luck!

Can't touch second element in array

I'm trying to touch a UITextField using Calabash. When I use query("UITextField")[x], where x is the number in the array of text fields that are on screen, I can correctly query for just one text field. However, when I use touch("UITextField")[x] it will always touch the first text field. This happens when using the console and when using cucumber to run the tests.
Here's relevant info about my setup:
xcode-select --print-path
/Applications/Xcode.app/Contents/Developer
xcodebuild -version
Xcode 7.2
Build version 7C68
calabash-ios version
0.16.4
Try
touch("UITextField index:x")
where x == your index.
Also, please update to calabash 0.17.0 :)
Explanation
The Calabash environment is composed of a client and a server. The server runs on the device/simulator as part of the app and receives commands/queries from the client. In this case, the client is the ruby interface through which you are interacting with the app.
The client is responsible for sending enough info to the server to select objects on which to perform the gestures. Once the gestures / queries have been performed, results are sent back to the client as json / hashes, which is what you see in the console. The distinction is important: the server performs the queries, the client does not.
By the time you are seeing the results of a query / gesture in the irb console, it has already been performed: the json that is returned simply represents the state of the views, not the actual views themselves.
So when you are running
touch("UITextField")[index]
This is actually equivalent to
touch_results = touch("UITextField")
touch_results[index]
In the first line, touch_results is receiving the json representation of the elements affected by performing touch on the results of querying for "UITextField", meaning that the touch event has already completed by the time you try to access the results with touch_results[index].
Another way to think about it is this: Given res = touch(query), the query is the part used to specify the actual views, and res is just json that represents the state of those views / results of a query or gesture.
In conclusion
When you need more specificity on a query, the specifiers need to go inside the query. E.g.,
touch("all UIScrollView UITextField marked:'some text' index:2")
(this will search for all UIScrollViews , find any UITextFields inside of them which contain text matching 'some text', and return the 3rd of such results).
For a full explanation of query language syntax, see the docs.

In Appium 1.0+, now that finding elements by tag is deprecated, how do I find an element by tag name when operating on the DOM of the webview?

Once I switch my context to the DOM of the webview, I want to be able to search those elements by tag, but I get the error that searching by tag is deprecated and to search by class instead. This won't work to find DOM elements by tag. Is there still a way to do it? Thanks!
As per Appium documentation for migrating to 1.0:
We've removed the following locator strategies:
-name
-tag name
... tag name has been replaced by class name. So to find an element by its
UI type, use the class name locator strategy for your client.
Why searching by tag name?
Although Selenium still supports this type of query, Appium decided not to do anymore. Actually when interacting with the device, searching by tag name is very inefficient.
Why would you want to do that? Think about it, if your page has a bit of content, you will end up having many p, div, span tags. Your search will return many elements and then you will have to go thorugh the list and locate the one you are interested in. If your page is very little, then you will probably end up with one tag of the type you are looking for, however why not applying a class and solve the problem?
Classes are not for CSS style
Remember that HTML attribute class was not introduced by W3C for applying CSS style. It is used to provide an element with more informationa bout its purpose in the DOM. When you apply a class to an element, you should do that basing on the role that element holds! Thus locating an element by class is sure better.
So forget searching by tag name. You should change your strategy and apply class names to your tags in your hybrid app. If you do not want to do so, then do not switch to the new version of Appium but this will keep you far from future innovations!
Migrating from a tagname based element location to a class name
orientd one is good practice. That's why you should change too.
maybe this can help
element.getAttribute("class")

How to select elements for Frank using UIQuery/UISpec?

I've been using Frank automation tool to write tests for iOS using cucumber. If I understand correctly, I can point to the different UI elements using UIQuery selectors. I've had a hard time finding documentation about these selectors. How exactly do they work?
Particularly, right now I'm trying to select a UITextField which the Symbiote Frank inspector identifies as UITextField with label "UserName". I've explicitly set that identifier with Xcode using the Accessibility Label property. I've seen that by default, Frank uses the placeholder property to refer to the UITextFields but I don't want to use that.
This is a partial response to the particular problem I had regarding how to refer to items by the accessibility label property. For a UITextField with the accessibility label set to UserName the selector "view marked: 'UserName'" seems to work.
The more general question about how selectors work is still open.
Consider using the Igor Query Language: https://github.com/dhemery/igor/wiki
I modeled Igor after CSS selectors, and added some syntax to handle some situations that CSS doesn't handle (e.g. "cousins" and selecting views based on the characteristics of their descendants).
The Igor syntax for your query would be:
UITextField[accessibilityLabel=='UserName']
The grammar for Igor is described completely on the wiki. The wiki also has some examples, though probably not enough.

Resources