Why are UIAElement's children not equal to themselves?

痴心易碎 提交于 2019-12-06 06:42:13

问题


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.


回答1:


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




回答2:


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));




回答3:


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;
 }


来源:https://stackoverflow.com/questions/12100928/why-are-uiaelements-children-not-equal-to-themselves

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!