这是我第一次在 xcode 中使用自动化工具,该脚本适用于所有按钮点击,但建立服务器连接的按钮除外。不知道原因
这是我到目前为止尝试过的脚本
var target = UIATarget.localTarget();
target.pushTimeout(4);
target.popTimeout();
var window=target.frontMostApp().mainWindow()
var appScroll=window.scrollViews()[0];
appScroll.logElementTree();
UIATarget.localTarget().delay(2);
appScroll.buttons()[1].tap();
上面的脚本会显示 UIActivityIndicator,而不是在成功后移动到下一个 Controller
我知道我一定错过了一个非常简单的点。所以请帮帮我
最佳答案
UIAutomation 试图让开发人员的事情变得“简单”,但这样做可能会让事情变得非常困惑。听起来您正在获取对 window
的引用,等待按钮出现,然后在该按钮上执行 .tap()
。
我发现您已经考虑过修改 target.pushTimeout()
,这与您的问题有关。超时系统可以让你做一些在任何理智的系统中都是不可能的事情:在元素存在之前获取对它的引用。我怀疑 UIAutomation 在幕后会反复尝试获取您想要的引用——只要超时
允许。
因此,在您发布的示例中,此“功能”可能实际上会伤害您。
var window=target.frontMostApp().mainWindow()
var appScroll=window.scrollViews()[0];
UIATarget.localTarget().delay(2);
appScroll.buttons()[1].tap();
如果 View 在 2 秒延迟期间发生变化怎么办?您对 target.frontMostApp().mainWindow.scrollViews()[0]
的引用可能无效,或者它可能未指向您认为指向的对象。
我们在 Illuminator framework 中解决了这个问题通过完全忘记超时系统,并仅手动重新评估给定的引用,直到它实际返回某些内容。我们称之为waitForChildExistence
,但功能基本如下:
var myTimeout = 3; // how long we want to wait
// this function selects an element
// relative to a parent element (target) that we will pass in
var selectorFn = function (myTarget) {
var ret = myTarget.frontMostApp().mainWindow.scrollViews()[0];
// assert that ret exists, is visible, etc
return ret;
}
// re-evaluate our selector until we get something
var element = null;
var later = get_current_time() + myTimeout;
while (element === null && get_current_time() < later) {
try {
element = selectorFn(target);
} catch (e) {
// must not have worked
}
}
// check whether element is still null
// do something with element
对于存在临时进度对话框的情况,此代码将简单地等待它消失,然后才能成功返回所需的元素。
关于javascript - 自动化脚本不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32316208/