4

I could create and replay the following script very well in the mac I Use.

  var target = UIATarget.localTarget();

  UIATarget.localTarget().delay(15);

  target.frontMostApp().mainWindow().tableViews()[0].textFields()[0].tap();

When I run the above script in another mac, It shows error in the 3rd line. after changing the above script's third line as following, it is replaying fine.

  target.frontMostApp().mainWindow().tableViews()[1].textFields()[0].tap();

Just I have changed tableview's index from 0 to 1. How can I achieve this in multiple mac systems? Both mac are having same xcode version(xcode 5) and simulator version(6.1) and mac version.Why Instruments are taking scripts API differently in different macs?

HangarRash
  • 7,314
  • 5
  • 5
  • 32

1 Answers1

1

For more consistent results one of the ways can be to access AX element by name (or other identifier/expected content/number of contained cells etc.). For example here Trouble Getting Elements by Name from UIAElementArray in UIAutomation SO question and corresponding discussion about how name for element can be set in different ways.

For example:

var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
tableViews['TableView'].textFields()[0].tap();

or

var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
tableViews['TableView'].textFields.withName("TextFieldName")[0].tap();

If using names are not practicable can be analysed content of the table and according to that right table can be selected. For example if table has some cell with name "Cell name":

var mainWindow = UIATarget.localTarget().frontMostApp().mainWindow();
var tableViews = mainWindow.tableViews();
if (tableViews[0].cells().firstWithName("Cell name")) {
    tableViews[0].textFields()[0].tap();
} else if (tableViews[1].cells().firstWithName("Cell name")) {
    tableViews[1].textFields()[0].tap();
}

More details about identifying cells in tableView for example in SO question UIACollectionView cells vs visibleCells This looks not very nice but it can be reasonable workaround and should work quite reliable. If number of cells are know and different in these tables than number of cells can be compared to find required table.

Community
  • 1
  • 1
Oleksiy Ivanov
  • 2,454
  • 15
  • 21
  • I had an issue when changing order/position of the UI elements will break UI Automation tests and because of that I tried to change my test to using names instead of sub-elements indexes. I do not know why your results is different for different Macs, there may be still a way to fix this without using names. I think Xcode will auto generate names for elements (at least some of the elements will have them, probably depending on element type/content). I used 'target.frontMostApp().mainWindow().logElementTree();' to print current elements names. – Oleksiy Ivanov Dec 16 '13 at 12:49
  • Edited answer to describe how elements can be identified by analysing their hierarchy (child's of the element). – Oleksiy Ivanov Dec 17 '13 at 17:58
  • It is not clear what reason for this, try 'target.frontMostApp().mainWindow().logElementTree();' for both machines and compare results. If builds are identical but issue exists than it may be not related to Xcode. – Oleksiy Ivanov Dec 20 '13 at 15:40
  • AFAIK, the script will run proper in all the macs, until and unless if the build that you are running changes. – coder284 Jan 10 '14 at 05:47