-
Notifications
You must be signed in to change notification settings - Fork 0
/
modularScripts.html
381 lines (313 loc) · 14.7 KB
/
modularScripts.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link href='https://fonts.googleapis.com/css?family=Chivo:900' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/pygment_trac.css" media="screen">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/styles/default.min.css">
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.3/highlight.min.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script>hljs.initHighlightingOnLoad();</script>
<title>Testing With Node by NickTulett</title>
</head>
<body>
<div id="container">
<div class="inner">
<header>
<h1>Modular test scripts</h1>
<h2></h2>
</header>
<!--
<section id="downloads" class="clearfix">
<a href="https://github.com/NickTulett/TestingWithNode/zipball/master" id="download-zip" class="button">
<span>Download .zip</span>
</a>
<a href="https://github.com/NickTulett/TestingWithNode/tarball/master" id="download-tar-gz" class="button">
<span>Download .tar.gz</span>
</a>
<a href="https://github.com/NickTulett/TestingWithNode" id="view-on-github" class="button">
<span>View on GitHub</span>
</a>
</section>
-->
<hr>
<section id="main_content">
<h2>
<a name="using-nodejs-a-an-automated-test-engine" class="anchor" href="#using-nodejs-a-an-automated-test-engine">
<span class="octicon octicon-link"></span>
</a>Breaking it down</h2>
<p>After you've written a few tests scripts, you don't really want all of them in one text file. I decided to break mine down into 4 layers:</p>
<ol>
<li>Routines generally useful for testing.</li>
<li>Interacting directly with webdriver-sync</li>
<li>Specific interactions with the AUT.</li>
<li>High level scripts for individual features/bugs.</li>
</ol>
<p>node.js already has a way of writing scripts in a modular way. You simply write the script to return a modules.export object, whose members become available to any other script that "requires" that file, e.g:</p>
<pre><code class="javascript">
//in Testing.js
module.exports = {
assert: function testing_assert(cond, errmsg) {
if (!cond) {
log.error(errmsg);
}
return cond;
},
files: {
wipeFolder: function files_wipeFolder(path, matching) {
matching = matching || /./;
fs.readdirSync(path).forEach(function (fileName) {
if (fileName.match(matching)) {
fs.unlinkSync(path + "/" + fileName);
}
});
}
},
log: {
//various log methods and properties
}
}
//then in a higher level script file
var testing = require("./Testing.js");
testing.files.wipeFolder("/home/tester/downloads/", /\.csv$/);
testing.assert($(".welcomeMessage").getText() === "Hello",
"Wrong welcome message");
</code></pre>
<h3>Testing layer</h3>
<p>As shown above, I've got routines for dealing with asserts, the local filesystem, logging (including marking the starting and ending of individual test cases) and starting and stopping timers.</p>
<h3>webdriver-sync layer</h3>
<p>We've seen a lot of this layer already in the previous page when discussing the selector syntax and even earlier when talking about instantiating browsers.</p>
<h3>AUT layer</h3>
<p>This is an attempt to abstract the business logic of the website away from its implementation. This is where I identified the locators discussed in the previous page. It also handles user admin, including logging in and out. There are also routines specific to the website's functionality, such as extracting values out of charts, filtering data as well as low-level server interactions such as tailing logs and dumping the database. The latter are implemented using the execSync npm module:</p>
<pre><code class="javascript">
var sh = require("execSync");
module.exports {
//other business layer object members
,host: "testserver.acme.com"
,ssh: function PTL_ssh(commands, user, stdout) {
console.log("STARTING SSH SESSION");
if (typeof user == "string") {
user = user || "tester";
} else {
stdout = user;
user = "tester";
}
if (typeof commands == "string") {
commands = [commands];
}
//either execute something quietly
if (!stdout) {
return sh.run("ssh " + user + "@" + this.host + " \""
+ commands.join("; ") + ";exit\"");
}
//or return the stdout from the command
return sh.exec("ssh " + user + "@" + this.host + " \""
+ commands.join("; ") + ";exit\"").stdout;
}
,taillog: function PTL_tailllog(n) {
n = n || 10;
console.log("FETCHING LAST " + n + " LINES OF THE LOG");
return sh.exec("ssh tester@" + this.host + " \" tail -n "
+ n + " /var/log/tomcat7/application.log\"").stdout;
}
}
</code></pre>
<h3>High-level script layer</h3>
<p>By the time we get to this layer, it shouldn't look much like a webdriver script (or javascript for that matter) but be a human-readable sequence of interactions and checks, e.g.</p>
<pre><code class="javascript">
//PTL is the AUT and "POR-748" is the related JIRA issue
"POR-748": function() {
log.startCase("Create a new organization");
PTL.signIn();
$("'Admin'").click();
PTL.waitFor("ADMIN PAGE", "'Organization'").hover();
Delay(1e3);
$("'New'", 0).click();
PTL.waitFor("NEW ORGANIZATION PAGE", "#name").set("Acme");
$("#newUserEmailToAddress").set("[email protected]");
$("#agreedLicense").click();
$("#create").click();
log.startCase("Checking log for recent errors");
var taillog = PTL.taillog(2);
console.log(taillog);
assert(!taillog.match(/ERROR/),
"Errors logged when creating a new organisation");
}
</code></pre>
<h2>Big Testing Object</h2>
<p>The tests are triggered as a node command line, so there is a file that contains a TESTS object which refers to each individual test script in the other files. It looks something like this:</p>
<pre><code class="javascript">
//in ptl.js
var TESTS = {
"Smoke": function (expectedBuildNumber, expectedArchiveNumber) {
expectedBuildNumber = expectedBuildNumber || "";
expectedArchiveNumber = expectedArchiveNumber || "";
if (expectedBuildNumber && expectedArchiveNumber) {
PTL.deploy(hgBranch, expectedArchiveNumber);
}
if (!this.RequestAuthentication()) {
return;
}
log.startCase("Log in");
Delay(5e3);
assert(PTL.url.match(/notAuthorized/),
"Wrong pre-permission landing page url " + PTL.url);
//...
}
,"Other test": function () {
//do stuff
}
//..and other tests
}
//then we read in the .js files that contain our other tests and add them to the TESTS object
var testModules = ["admin", "SingleSite", "MultiSite", "Security",
"Actuals", "Config", "Charts", "InternetExplorer"];
testModules.forEach(function (testModule) {
var req = require("./" + testModule + ".js");
for (var test in req) {
TESTS[test] = req[test];
}
});
//then some other stuff to check which development branch we're on
//and use the correct host, which I won't show here
</code></pre>
<p>The command to run the test will be something like:</p>
<pre><code class="bash">
node ptl.js Chrome "POR-748"
</code></pre>
<p>So ptl.js is called with command line arguments and has to launch the named browser and run the specified test.</p>
<pre><code class="javascript">
//in ptl.js
var driverType = process.argv[2];
var jenkinsJob = process.argv[3];//the reason for the name will be apparent later
//here is where it gets tricky
PTL = require("./Portal.js")(driverType);
//PTL is now an object that contains the AUT layer (in this case the AUT is "Portal")
//Creating PTL also instantiates the browser because
//in Portal.js - this is the AUT level
//the module exports for Portal.js instantiates the browser, then returns the object
var WDS = require("./WD.js");//the webdriver-sync layer
module.exports = function (browserType) {
var WD = WDS(browserType);
return {
locators: {
//...
}
//...etc.
}
//and finally the browser is instantiated in WD.js which in turn returns a function
//which takes the driverType parameter passed along from the original command line
//and then returns the object with the webdriver-sync layer members
//in WD.js
var wd = require("webdriver-sync");
var testing = require("./Testing.js");
var FirefoxProfile = require(testing.HOME
+ "/node_modules/webdriver-sync/src/classes/FirefoxProfile.js");
module.exports = function (driverType) {
console.log(driverType);
var driver, caps;
var profile = null;
if (driverType == "Remote") {
caps = new wd.DesiredCapabilities["internetExplorer"]();
driver = new wd["RemoteWebDriver"]("http://192.168.10.104:4444/wd/hub", caps);
} else {
if (driverType == "IOS") {
caps = new wd.DesiredCapabilities["ipad"]();
driver = new wd["RemoteWebDriver"]("http://192.168.10.61:5555/wd/hub", caps);
} else {
if (driverType.match(/Firefox/i)) {
//stop prompts for download locations
profile = new FirefoxProfile();
profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "text/csv");
driver = new wd["FirefoxDriver"](profile);
} else {
if (driverType.match(/Chrome/i)) {
//to stop warning banner in Chrome 35+
var chromeOptions = new wd.ChromeOptions();
chromeOptions.addArguments("test-type");
caps = new wd.DesiredCapabilities["chrome"]();
caps = new wd.DesiredCapabilities["chrome"]();
caps.setCapability("chromeOptions", chromeOptions);
driver = new wd["ChromeDriver"](caps);
} else {
//internetExplorer probably
driver = new wd[driverType + "Driver"]();
}
}
}
}
return {
driver: driver,
findElement: function WD_findElement(selector, index) {
//...we've seen this before
}
//...etc
}
}
</code></pre>
<p>The <strong>test-type</strong> argument we passed to Chrome above stops a security message appearing every time you run your tests and ruining any screenshots you might be taking. Peter Beverloo maintains a <a href="http://peter.sh/experiments/chromium-command-line-switches/#test-type">full list of Chrome command line options</a> for the curious.</p>
<p>Similarly, the preference to automatically download csv files is added to the FirefoxProfile. There around <a href="http://kb.mozillazine.org/About:config_entries">800 preferences</a> available - you may not need any of them.</p>
<h3>Running multiple tests</h3>
<p>We don't want to run the command line for every single test, but we can easily call multiple tests in a single script file:</p>
<pre><code class="javascript">
//in one of our test script files
module.exports = {
//some individual tests named "POR-nnnn"
,"Single Site Tests": function () {
[
"POR-976", "POR-1056", "POR-966",
"POR-664", "POR-654", "POR-768", "POR-433","POR-657",
"POR-813", "POR-837", "POR-694", "POR-1081", "POR-1078"
].forEach(function (singleSiteTest) {
console.log("RUNNING SINGLE SITE TEST: " + singleSiteTest);
this[singleSiteTest]();
}, this);
}
}
</code></pre>
<p>Then the command line would be:</p>
<pre><code class="bash">
node ptl.js Chrome "Single Site Tests"
</code></pre>
<h2>So where is that $() method you promised?</h2>
<p>That appears at the AUT level. Portal.js actually starts like this:</p>
<pre><code class="javascript">
var WDS = require("./WD.js");
var path = require("path");
var testing = require("./Testing.js");
var log = testing.log;
var timer = testing.timer;
var fs = require("fs");
var sh = require("execSync");
module.exports = function (browserType) {
var WD = WDS(browserType);
var browser = WD.driver;
var browserName = browser.getCapabilities().getBrowserName();
//here we are:
var $ = WD.findElement;
//that was it
var go = WD.go;
var getJSON = WD.getJSON;
var getHeaders = WD.getHeaders;
var browserLogs = WD.browserLogs;
var waterfall = WD.waterfall;
return {
//specific locators
locators: {
//and so on
</code></pre>
<a href="uploadsDownloads.html">Next: Uploads and Downloads</a>
</section>
<footer>
<a href="/TestingWithNode/">Testing With Node</a> is maintained by <a href="https://github.com/NickTulett">NickTulett</a>
<br>This page was generated by <a href="http://pages.github.com">GitHub Pages</a>. Tactile theme by <a href="https://twitter.com/jasonlong">Jason Long</a>.
</footer>
</div>
</div>
</body>
</html>