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
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:
1var webdriver =require(& # x27; wd & # x27;),2assert =require(& # x27; assert & # x27;);34var browser = webdriver.remote();56browser.on(& # x27; status & # x27;,function(info){7console.log(& # x27; \x1b [36m % s\x1b [0m & # x27;, info);8});910browser.on(& # x27; command & # x27;,function(meth, path){11console.log(& # x27; & gt; \x1b [33m % s\x1b [0m: % s & # x27;, meth, path);12});1314browser.init({15browserName:& # x27; firefox & # x27;16,tags:[& quot; examples & quot;]17,name:& quot; This is an example test & quot;18},function(){1920browser.get(& quot; http: //saucelabs.com/test/guinea-pig & quot;,function(){21browser.title(function(err, title){22assert.ok(~title.indexOf(& # x27; I am a page title - Sauce Labs & # x27;),& # x27; Wrong rubric! & # x27;);23browser.elementById(& # x27; comments & # x27;,function(err, el){24el.sendKeys(& quot; this is not a gossip & quot;,function(err){25browser.elementById(& # x27; submit & # x27;,function(err, el){26el.click(function(){27browser.eval(& quot; window.location.href & quot;,function(err, title){28assert.ok(~title.indexOf(& # x27; # & # x27;),& # x27; Wrong rubric! & # x27;);29browser.elementById(& quot; your_comments & quot;,function(err, el){30el.textPresent(& quot; this is not a comment & quot;,function(err, present){31assert.ok(present,& quot; Comments not correct & quot;);32el.text(function(err, text){33console.log(text);34browser.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.
1var webdriver =require(& # x27; wd-parallel-async & # x27;)2, assert =require(& # x27; assert & # x27;);34varparallelizer= webdriver.parallelizer();56parallelizer.run([{7browserName:& # x27; chrome & # x27;,8tags:[& quot; examples & quot;],9name:& quot; This is an example test & quot;,10},{11browserName:& # x27; firefox & # x27;,12tags:[& quot; examples & quot;],13name:& quot; This is an example test & quot;,14}],function(browser, desired){15browser.on(& # x27; status & # x27;,function(info){16console.log(& # x27; \x1b [36m % s\x1b [0m & # x27;, info);17});1819browser.on(& # x27; bid & # x27;,function(meth, path){20console.log(& # x27; & gt; \x1b [33m % s\x1b [0m: % s & # x27;, meth, path);21});2223browser.init(desired,function(){2425browser.get(& quot; http: //saucelabs.com/test/guinea-pig & quot;,function(){26browser.title(function(err, title){27assert.ok(~title.indexOf(& # x27; I am a page title - Sauce Labs & # x27;),& # x27; Wrong title! & # x27;);28browser.elementById(& # x27; comments & # x27;,function(err, el){29el.sendKeys(& quot; this is not a comment & quot;,function(err){30browser.elementById(& # x27; submit & # x27;,function(err, el){31el.click(function(){32browser.eval(& quot; window.location.href & quot;,function(err, title){33assert.ok(~title.indexOf(& # x27; # & # x27;),& # x27; Improper title! & # x27;);34browser.elementById(& quot; your_comments & quot;,function(err, el){35el.textPresent(& quot; this is not a comment & quot;,function(err, present){36assert.ok(present,& quot; Comments not compensate & quot;);37el.text(function(err, text){38console.log(text);39browser.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:
1varp_webdriver=require(& # x27; wd-parallel & # x27;);23var username =& quot; & lt; USERNAME & gt; & quot;,4accessKey =& quot; & lt; ACCESS KEY & gt; & quot;;56var browsers =p_webdriver.remote(7& quot; ondemand.saucelabs.com & quot;,880,9username,10accessKey11);1213// Desired14var 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]2021// Test22browsers.test=function(browser, desired){2324console.log(& quot; server status: & quot;, browser.status());25browser.init(desired);2627browser.get(& quot; http: //google.com & quot;);28console.log(& quot; rubric is & quot;+browser.title());2930varqueryField= browser.elementByName(& # x27; q & # x27;);31browser.type(queryField,& quot; Hello World & quot;);32browser.type(queryField,& quot; \n & quot;);3334browser.setWaitTimeout(3000);35browser.elementByCss(& # x27; # ires & # x27;);// waiting for new page to lade36console.log(browser.title());3738browser.quit();39};4041// Run42browsers.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:
1var launcher =require(& # x27; wd-unit & # x27;);23var4username =& # x27; username & # x27;,5accessKey =& # x27; accessKey & # x27;;67var saucelabs ={8host:& quot; ondemand.saucelabs.com & quot;,9port:80,10username: username,11accessKey: accessKey12}1314launcher.run({15runner:& # x27; jasmine & # x27;,16addr:& # x27; pivotal.github.com & # x27;,17port:80,18page:& # x27; jasmine & # x27;,19desired:[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],25wd_args: saucelabs26});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!
Share this post
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 FreeTest 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