3

I noticed some weird behavior in a UIAutomation script I had written a while back that I hadn't ran in a while. My assertions were failing; after doing some digging, I saw that when iterating a UIAElement's .elements(), subelements do not appear to be equal to themselves.

This has worked for me as expected in the past, but appears to be broken in at least XCode 4.3.2

To repro:

  1. create a single-view app
  2. throw some elements in the view, set Accessibility Labels on the elements so they get picked up by UIAutomation
  3. Run the following script in UIAutomation:

    var elements = UIATarget.localTarget().frontMostApp().mainWindow().elements();
    for (var i = 0; i < elements.length; i++) {
      var el1 = elements[i];
      var el2 = elements[i];
      var equals = (el1 == el2);
      UIALogger.logMessage(el1.label() + " is equal to " + el2.label() + " ? " + equals);
    }
    
  4. See that el1 and el2 do not appear to reference the same object.

I'm not sure if this is the expected behavior, though this seems very off to me. If anybody has any insight, I'd appreciate it.

cozykozy
  • 217
  • 1
  • 9
  • This seems quite relevant: http://stackoverflow.com/questions/201183/how-do-you-determine-equality-for-two-javascript-objects – Chris Livdahl Jan 04 '13 at 00:57

3 Answers3

0

Not sure if this helps, but can you try === operator to compare the objects?

Rahul Jawale
  • 71
  • 1
  • 6
  • Sadly, this does not work.. Not surprisingly, `==` should coerce if the objects aren't of the same type, which is actually a weaker check than `===`. – cozykozy Nov 13 '12 at 23:13
0

This is actually a known bug in the UIAutomation software. The workaround that I've come up with is to test based on all the available properties of an element. It's a real pain in the butt.

 var el1 = ELEMENT_1;

 var el1x = el1.rect().origin.x;
 var el1y = el1.rect().origin.y;
 var el1w = el1.rect().size.width;
 var el1h = el1.rect().size.height;
 var el1n = el1.name();

 var el2 = ELEMENT_2;

 var el2x = el2.rect().origin.x;
 var el2y = el2.rect().origin.y;
 var el2w = el2.rect().size.width;
 var el2h = el2.rect().size.height;
 var el2n = el2.name();

 if(el1x == el2x && el1y == el2y &&
      el1w == el2w && el1h == el2h &&
      el1n == el2n)
 {
      // Elements are equal
      return true;
 }
Ian
  • 11,280
  • 3
  • 36
  • 58
Braains
  • 606
  • 1
  • 5
  • 22
0

take a look at tuneup.js (http://www.tuneupjs.org/). This library makes iOS UIAutomation a lot more pleasant. One thing it does is extend the functionality of UIAElements, using tuneup the above line in your code would be

var equals = (el1.equals(el2));

ekcrisp
  • 1,967
  • 1
  • 18
  • 26
  • Since I [contributed](https://github.com/alexvollmer/tuneup_js/blame/master/uiautomation-ext.js#L67) this particular function to tuneup.js, I should point out that it's also available in [Illuminator](https://github.com/paypal/Illuminator)'s Extensions.js. – Ian Apr 06 '15 at 21:10