简体   繁体   English

#import如何在iOS的UI自动化中工作?

[英]How does #import work in iOS' UI Automation?

I'm making a small test framework that uses the JavaScript module pattern for UI Automation testing on iOS. 我正在制作一个小型测试框架,它使用JavaScript模块模式在iOS上进行UI自动化测试。 However, I seem to be getting odd results based on #import and extending modules. 但是,基于#import和扩展模块,我似乎得到了奇怪的结果。

I have the base test module called Tester-Module.js : 我有一个名为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): 这是测试文件tester.jstester.js是我在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-Module.js模块,我就无法引用Tester对象。 Tester-Extension.js extends the Tester module defined in Tester-Module.js : 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()');
}

And the updated test case file tester.js : 更新的测试用例文件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 in tester.js ? 为什么我不能在Tester-Extension.js引用Tester对象,但可以在tester.jstester.js

  • What is the #import macro doing? #import宏在做什么?

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. 经过一些更多的搜索和测试后,看起来在每个模块文件中使用#import - 类似于Node.js中的require - UI自动化框架不支持。

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: 使用上面的示例,头文件看起来像:

// Tester-Header.js
#import "./Tester-Module.js"
#import "./Tester-Extension.js"

And the test file would simply import the header file like so: 而测试文件只会导入头文件,如下所示:

#import "./Tester-Header.js"

// Prints "Overwritten SetUp()"
Tester.setUp();

The Mother May UI BDD framework has a more extensive example of a header file and importing the header file into a test file . Mother May UI BDD框架有一个更广泛的头文件示例,并将头文件导入测试文件 Disclosure: I wrote the framework and originally asked this question in order to make the framework more modular. 披露:我编写了框架并最初问了这个问题,以使框架更加模块化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM