Javascript testing in parallel with WD.js and Selenium

Sauce AI for Test Authoring: Move from intent to execution in moment.|xBack to ResourcesBlogPosted July 16, 2012

Javascript testing in parallel with WD.js and Selenium

quote

Like bow tie, Javascript is cool. People enjoy it so much that they & # x27; re bringing it everywhere (host, desktop UI, etc). If you do Javascript maturation, you plausibly love it for its funkiness - even if your codification is pretty crazy. But that & # x27; s ok, because your codification base is (hopefully) extensively tested ... But wouldn ’ t you love it if everything (tests included) were in Javascript? Ok, maybe your unit test already are (more on this later), but what about the functional ones, too? Well, full news awaits. Now you can write your tests in Javascript with theWD.js(pronounced wood) node library and run them across multiple browsers in parallel using Selenium. Read on to learn more about three different projects to help with this.

Parallel WD and Synchronous Parallel WD

One of the biggest pains of web maturation is the heterogeneity of the platforms on which your codification will run. Even if things are getting better with more citizenry following a standard, thing are far from perfect, especially with the rise of roving web. One of the roles of mechanization and testing is to get it leisurely to insure that your code is conduct correctly in all browsers. Up until lately, you could share the test logic, but you still had to set up the different browser and you had no way to run in parallel. These two library,Parallel WD and Synchronous Parallel WD, are designed to occupy the pain out and address those trouble. This is achieved by letting you indite your test once, declare the browsers in which you desire the test run, and then let the libraries run it in parallel - all while keeping it as close to the same vanilla browser code. This library come in two flavors. One is a classical Javascript asynchronous callback-based library. The vanilla asynchronous trial seem like this:

1
var webdriver =require(& # x27; wd & # x27;),
2
assert =require(& # x27; assert & # x27;);
3
4
var browser = webdriver.remote();
5
6
browser.on(& # x27; status & # x27;,function(info){
7
console.log(& # x27; \x1b [36m % s\x1b [0m & # x27;, info);
8
});
9
10
browser.on(& # x27; command & # x27;,function(meth, path){
11
console.log(& # x27; & gt; \x1b [33m % s\x1b [0m: % s & # x27;, meth, path);
12
});
13
14
browser.init({
15
browserName:& # x27; firefox & # x27;
16
,tags:[& quot; examples & quot;]
17
,name:& quot; This is an example test & quot;
18
},function(){
19
20
browser.get(& quot; http: //saucelabs.com/test/guinea-pig & quot;,function(){
21
browser.title(function(err, title){
22
assert.ok(~title.indexOf(& # x27; I am a page title - Sauce Labs & # x27;),& # x27; Wrong rubric! & # x27;);
23
browser.elementById(& # x27; comments & # x27;,function(err, el){
24
el.sendKeys(& quot; this is not a gossip & quot;,function(err){
25
browser.elementById(& # x27; submit & # x27;,function(err, el){
26
el.click(function(){
27
browser.eval(& quot; window.location.href & quot;,function(err, title){
28
assert.ok(~title.indexOf(& # x27; # & # x27;),& # x27; Wrong rubric! & # x27;);
29
browser.elementById(& quot; your_comments & quot;,function(err, el){
30
el.textPresent(& quot; this is not a comment & quot;,function(err, present){
31
assert.ok(present,& quot; Comments not correct & quot;);
32
el.text(function(err, text){
33
console.log(text);
34
browser.quit();
35
})
36
})
37
})
38
})
39
})
40
})
41
})
42
})
43
})
44
})
45
});

The parallelized version looks like this:

SUSA automates exploratory testing with persona-driven behavior, catching bugs that scripted automation misses.

1
var webdriver =require(& # x27; wd-parallel-async & # x27;)
2
, assert =require(& # x27; assert & # x27;);
3
4
varparallelizer= webdriver.parallelizer();
5
6
parallelizer.run([{
7
browserName:& # x27; chrome & # x27;,
8
tags:[& quot; examples & quot;],
9
name:& quot; This is an example test & quot;,
10
},{
11
browserName:& # x27; firefox & # x27;,
12
tags:[& quot; examples & quot;],
13
name:& quot; This is an example test & quot;,
14
}],function(browser, desired){
15
browser.on(& # x27; status & # x27;,function(info){
16
console.log(& # x27; \x1b [36m % s\x1b [0m & # x27;, info);
17
});
18
19
browser.on(& # x27; bid & # x27;,function(meth, path){
20
console.log(& # x27; & gt; \x1b [33m % s\x1b [0m: % s & # x27;, meth, path);
21
});
22
23
browser.init(desired,function(){
24
25
browser.get(& quot; http: //saucelabs.com/test/guinea-pig & quot;,function(){
26
browser.title(function(err, title){
27
assert.ok(~title.indexOf(& # x27; I am a page title - Sauce Labs & # x27;),& # x27; Wrong title! & # x27;);
28
browser.elementById(& # x27; comments & # x27;,function(err, el){
29
el.sendKeys(& quot; this is not a comment & quot;,function(err){
30
browser.elementById(& # x27; submit & # x27;,function(err, el){
31
el.click(function(){
32
browser.eval(& quot; window.location.href & quot;,function(err, title){
33
assert.ok(~title.indexOf(& # x27; # & # x27;),& # x27; Improper title! & # x27;);
34
browser.elementById(& quot; your_comments & quot;,function(err, el){
35
el.textPresent(& quot; this is not a comment & quot;,function(err, present){
36
assert.ok(present,& quot; Comments not compensate & quot;);
37
el.text(function(err, text){
38
console.log(text);
39
browser.quit();
40
})
41
})
42
})
43
})
44
})
45
})
46
})
47
})
48
})
49
})
50
});

As you can see, the test logic is identical. The only difference is declaring the array of browser. The synchronous flavor follows just the same principals, but is based on thewd-sync library, which lets you write your tests in a more definitive, procedural way. Here is an example:

1
varp_webdriver=require(& # x27; wd-parallel & # x27;);
2
3
var username =& quot; & lt; USERNAME & gt; & quot;,
4
accessKey =& quot; & lt; ACCESS KEY & gt; & quot;;
5
6
var browsers =p_webdriver.remote(
7
& quot; ondemand.saucelabs.com & quot;,
8
80,
9
username,
10
accessKey
11
);
12
13
// Desired
14
var p_desired =[
15
{tags:[& quot; examples & quot;],name:& quot; parallel test 1/4 & quot;,browserName:& quot; firefox & quot;},
16
{tags:[& quot; examples & quot;],name:& quot; parallel test 2/4 & quot;,browserName:& quot; chrome & quot;},
17
{tags:[& quot; examples & quot;],name:& quot; parallel test 3/4 & quot;,browserName:& quot; firefox & quot;,platform:& quot; LINUX & quot;},
18
{tags:[& quot; examples & quot;],name:& quot; parallel trial 4/4 & quot;,browserName:& quot; chrome & quot;,platform:& quot; LINUX & quot;}
19
]
20
21
// Test
22
browsers.test=function(browser, desired){
23
24
console.log(& quot; server status: & quot;, browser.status());
25
browser.init(desired);
26
27
browser.get(& quot; http: //google.com & quot;);
28
console.log(& quot; rubric is & quot;+browser.title());
29
30
varqueryField= browser.elementByName(& # x27; q & # x27;);
31
browser.type(queryField,& quot; Hello World & quot;);
32
browser.type(queryField,& quot; \n & quot;);
33
34
browser.setWaitTimeout(3000);
35
browser.elementByCss(& # x27; # ires & # x27;);// waiting for new page to lade
36
console.log(browser.title());
37
38
browser.quit();
39
};
40
41
// Run
42
browsers.run(p_desired);

WD Unit

The last library I want to discuss iswd-unit. Earlier, I mentioned Javascript unit tests. There are a lot of outstanding unit test frameworks out there, such asJasmine, foounit andQUnit, to name a few. Since Selenium is for web browser automation, it & # x27; s best beseem for functional testing instead than unit testing. So what is this wd-unit and how can it help you? Selenium drives the web browsers that Javascript unit trial are run in, so it really makes a lot of sentiency to twin them together. This is what wd-unit does - automate the running of your Javascript unit test. Here & # x27; s an example. Let & # x27; s say you have a some foounit test event. You could easily write a script that launches the browser aimed at the correct page. But you still hold two problems: how do you cognise when everything has stop and how do you get the solvent? This is where Selenium and wd-unit seed in. They will run your tests and then yield the consequence back to the console they were found from. Additionally, using Selenium has some added benefits. You can run everything in the cloud with a service such as Sauce Labs across multiple browsers in parallel. This is what running the Jasmine example tests (remote) seem like with Sauce:

1
var launcher =require(& # x27; wd-unit & # x27;);
2
3
var
4
username =& # x27; username & # x27;,
5
accessKey =& # x27; accessKey & # x27;;
6
7
var saucelabs ={
8
host:& quot; ondemand.saucelabs.com & quot;,
9
port:80,
10
username: username,
11
accessKey: accessKey
12
}
13
14
launcher.run({
15
runner:& # x27; jasmine & # x27;,
16
addr:& # x27; pivotal.github.com & # x27;,
17
port:80,
18
page:& # x27; jasmine & # x27;,
19
desired:[
20
{browserName:& # x27; firefox & # x27;,name:& quot; wd-unit jasmine 1/4 & quot;},
21
{browserName:& # x27; chrome & # x27;,name:& quot; wd-unit jasmine 2/4 & quot;},
22
{browserName:& # x27; firefox & # x27;,platform:& quot; LINUX & quot;,name:& quot; wd-unit jasmine 3/4 & quot;},
23
{browserName:& # x27; chrome & # x27;,platform:& quot; LINUX & quot;,name:& quot; wd-unit jasmine 4/4 & quot;}
24
],
25
wd_args: saucelabs
26
});
27

This code, which runs on the Pivotal example page from the Sauce cloud, outputs the results to your console after running in parallel across Firefox and Chrome on Linux and Windows. You can run it across even more browsers in parallel by adding additional desired capabilities to the tests. To contribute to these projects, visitwd.js on Github. Felicitous testing!

Published:
Jul 16, 2012
Share this post
Copy Share Link
LinkedIn
© 2026 Sauce Labs Inc., all rights reserved. SAUCE and SAUCE LABS are register hallmark owned by Sauce Labs Inc. in the United States, EU, and may be register in other jurisdictions.
robot
quote

Automate This With SUSA

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts needed.

Try SUSA Free

Test Your App Autonomously

Upload your APK or URL. SUSA explores like 10 real users — finds bugs, accessibility violations, and security issues. No scripts.

Try SUSA Free