#import 在 iOS 中如何工作?用户界面自动化?
我正在制作一个小型测试框架,它使用 JavaScript 模块模式在 iOS 上进行 UI 自动化测试。但是,我似乎得到了基于 #import 和扩展模块。
我有一个名为 Tester-Module.js
的基本测试模块:
(function() {
var Tester = this.Tester = {};
Tester.setUp = function() {
UIALogger.logMessage('Regular SetUp()');
}
}).call(this);
如果我在测试用例中导入此模块,它就可以正常工作。这是测试文件 tester.js
(tester.js
是我在 Instruments 中导入的文件):
#import "./Tester-Module.js"
// Prints 'Regular SetUp()'
Tester.setUp();
但是,如果我尝试扩展 Tester-Module.js
模块位于另一个模块文件中,我无法引用 Tester 对象。 Tester-Extension.js
扩展了 Tester-Module.js
中定义的 Tester 模块:
#import "./Tester-Module.js"
// Outputs:
// Exception raised while running script:
// ReferenceError: Can't find variable: Tester\n
Tester.setUp = function() {
UIALogger.logMessage('Overwritten SetUp()');
}
以及更新的测试用例文件 tester.js
:
#import "./Tester-Extension.js"
// Exception is thrown before this
Tester.setUp();
我希望相关问题是:
为什么我不能在
Tester-Extension.js
中引用 Tester 对象,但可以在tester.js
中引用?#import 宏在做什么?
I'm making a small test framework that uses the JavaScript module pattern for UI Automation testing on iOS. However, I seem to be getting odd results based on #import and extending modules.
I have the base test module called Tester-Module.js
:
(function() {
var Tester = this.Tester = {};
Tester.setUp = function() {
UIALogger.logMessage('Regular SetUp()');
}
}).call(this);
If I import this module in my test case, it works fine. Here's the test file tester.js
(tester.js
is the file I import in Instruments):
#import "./Tester-Module.js"
// Prints 'Regular SetUp()'
Tester.setUp();
However, if I try to extend the Tester-Module.js
module in another module file, I cannot reference the Tester object. Tester-Extension.js
extends the Tester module defined in Tester-Module.js
:
#import "./Tester-Module.js"
// Outputs:
// Exception raised while running script:
// ReferenceError: Can't find variable: Tester\n
Tester.setUp = function() {
UIALogger.logMessage('Overwritten SetUp()');
}
And the updated test case file tester.js
:
#import "./Tester-Extension.js"
// Exception is thrown before this
Tester.setUp();
My hopefully related questions are:
Why can I not reference the Tester object inside
Tester-Extension.js
, but can intester.js
?What is the #import macro doing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更多搜索和测试后,UI 自动化框架似乎不支持在每个模块文件中使用
#import
(类似于 Node.js 中的 require)。解决方法是包含一个导入每个模块的头文件,然后将其导入到测试用例中。使用上面的示例,头文件将如下所示:
测试文件将简单地导入头文件,如下所示:
Mother May UI BDD 框架有一个更广泛的头文件 并将头文件导入到 测试文件。披露:我编写了框架并最初提出这个问题是为了使框架更加模块化。
After some more searching and testing, it looks like using
#import
in each module file — similar to require in Node.js — is not supported with the UI Automation framework.The work around is to include a header file that imports every module and then just import that in the test case. Using the example above, the header file would look like:
And the test file would simply import the header file like so:
The Mother May UI BDD framework has a more extensive example of a header file and importing the header file into a test file. Disclosure: I wrote the framework and originally asked this question in order to make the framework more modular.