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 in tester.js
?
What is the #import macro doing?
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:
// 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. Disclosure: I wrote the framework and originally asked this question in order to make the framework more modular.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With