Automated Web Testing using JavaScript

56
Automated Web T es ting using JavaScript Simon Gues t, Distinguished Engineer. Neudesic, LL C

description

Session from GIDS 2014, showing how to do automated Web testing using a variety of JavaScript frameworks, including QUnit, Jasmine, Protractor, Selenium, and PhantomJS

Transcript of Automated Web Testing using JavaScript

Page 1: Automated Web Testing using JavaScript

Automated Web Testing using JavaScript

Simon Guest, Distinguished Engineer. Neudesic, LLC

Page 2: Automated Web Testing using JavaScript

Web testing over the years...

Page 3: Automated Web Testing using JavaScript
Page 4: Automated Web Testing using JavaScript

Paradigm Shift

Page 5: Automated Web Testing using JavaScript

JavaScript Testing Tools

Page 6: Automated Web Testing using JavaScript

Why JavaScript for Testing?4 Free

4 Open source

4 Modular

4 Active and vibrant community

4 Client and server in JavaScript - why not tests?

Page 7: Automated Web Testing using JavaScript

Goals of this session

Page 8: Automated Web Testing using JavaScript

Explore automated Web testing using JavaScript

Page 9: Automated Web Testing using JavaScript

Unit testing vs. end-to-end testing

Page 10: Automated Web Testing using JavaScript

Interchangeable frameworks for JavaScript Testing

Page 11: Automated Web Testing using JavaScript

When not to write tests!

Page 12: Automated Web Testing using JavaScript

Simple function to add two numbersvar add = function(x, y){ var result = x + y; return result;};

Page 13: Automated Web Testing using JavaScript

Let's write a test!var add = function(x, y){ var result = x + y; return result;};

test("test that add function doesn't return null", function() { var result = add(2, 3); notEqual(result, null, "We expect the value to be non null");});

Page 14: Automated Web Testing using JavaScript

Test that is performing an assertion4 Lot's of code just to test a single value

4 Test will rarely, if ever, fail

4 Quality vs. quantity of tests

Page 15: Automated Web Testing using JavaScript

Simple function to add two numbersvar add = function(x, y){ var result = x + y; return result;};

Page 16: Automated Web Testing using JavaScript

Simple function to add two numbersvar add = function(x, y){ var result = x + y; console.assert(result, "X should not be null!"); return result;};

Page 17: Automated Web Testing using JavaScript

Assertions in production code?

Page 18: Automated Web Testing using JavaScript

Simple function to add two numbersvar add = function(x, y){ var result = x + y; assert(result, "An error has occured with this page. Please refresh."); return result;};

Page 19: Automated Web Testing using JavaScript

Testing logic using unit tests

Page 20: Automated Web Testing using JavaScript

QUnit

4 http://qunitjs.com

4 JavaScript Unit Test Framework

4 Used to test jQuery, jQuery UI, and jQuery Mobile

Page 21: Automated Web Testing using JavaScript

QUnittest("a basic test example", function() { var value = "hello"; equal( value, "hello", "We expect value to be hello" );});

Page 22: Automated Web Testing using JavaScript

Demo - QUnit

Page 23: Automated Web Testing using JavaScript

Testing what the user actually sees...

Page 24: Automated Web Testing using JavaScript

Demo - What happens if the UI breaks?

Page 25: Automated Web Testing using JavaScript

Selenium

4 http://seleniumhq.org

4 Web Application Testing Platform

4 Open source (Apache 2.0) released by ThoughtWorks in 2004

Page 26: Automated Web Testing using JavaScript

Selenium

4 Selenium IDE - Basic test recorder, implemented as a Firefox extension

4 Selenium WebDriver - Formerly Selenium RC, remote control for browser

4 Selenium Grid - Instantiate browsers on remote machines

Page 27: Automated Web Testing using JavaScript

Demo - Selenium IDE

Page 28: Automated Web Testing using JavaScript

Demo - Selenium Builder

Page 29: Automated Web Testing using JavaScript

Selenium IDE/Builder4 Quick/easy to create tests

4 However, no test inheritence

4 Output is HTML or JSON, not script

4 Difficult to inject any complex logic

4 Has to be manually run through Firefox

Page 30: Automated Web Testing using JavaScript

Selenium WebDriver

Page 31: Automated Web Testing using JavaScript

WebDriver Specification4 W3C Working Draft

4 Platform and language neutral interface to allow control of a Web browser

4 Wire level protocol using HTTP and JSON

4 https://dvcs.w3.org/hg/webdriver/raw-file/default/webdriver-spec.html

Page 32: Automated Web Testing using JavaScript

Selenium WebDriver

4 Implementation of WebDriver protocol

4 Runs on host, listens on ports

4 Invokes browser (as configured)

4 Drives browser to test application

Page 33: Automated Web Testing using JavaScript

What do we use to write those tests?

Page 34: Automated Web Testing using JavaScript

Jasmine

4 http://pivotal.github.io/jasmine/

4 BDD Framework for writing JavaScript tests

4 Clean syntax

4 Support for mocks (spies)

Page 35: Automated Web Testing using JavaScript

Jasminedescribe("a basic test example", function() { it(“tests that true is always the truth”, function() { expect(true).toBe(true); });});

Page 36: Automated Web Testing using JavaScript

Protractor

4 https://github.com/angular/protractor

4 Testing framework for AngularJS, but can be used for any application

4 Supports Jasmine tests by default (Mocha coming soon)

4 Scripts to support easy install of Selenium

Page 37: Automated Web Testing using JavaScript

Protractor4 Also installs Selenium dependencies

npm install –g protractorwebdriver-manager updatewebdriver-manager start

Page 38: Automated Web Testing using JavaScript

Demo - WebDriver, Protractor, Jasmine

Page 39: Automated Web Testing using JavaScript

What did we see?

4 Tests written using Jasmine

4 Being invoked using Protractor

4 Sent to Selenium server using WebDriver protocol

4 Selenium invoking Chrome to run tests

Page 40: Automated Web Testing using JavaScript

Demo - Using WebBuilder to create Protractor tests

Page 41: Automated Web Testing using JavaScript

Demo - Can I do unit testing with WebDriver

also?

Page 42: Automated Web Testing using JavaScript

Do I have to launch a browser?

Page 43: Automated Web Testing using JavaScript

PhantomJS

4 http://phantomjs.org

4 Headless (Qt-based) WebKit with JavaScript API

4 Ability to act as a browser without actually having a GUI

4 Ideal for running in hosted instances (e.g. docker)

Page 44: Automated Web Testing using JavaScript

PhantomJSpage.open(‘http://localhost:8088’, function(status) { page.evaluate(function() { /* test elements on the page */ });});

Page 45: Automated Web Testing using JavaScript

Demo - PhantomJS

Page 46: Automated Web Testing using JavaScript

Nice - but yet another framework?

Page 47: Automated Web Testing using JavaScript

Nice - but yet another framework?4 PhantomJS can be invoked by

Selenium as a virtual browser

4 (Configure Selenium to invoke PhantomJS CLI instead of Chrome executable)

Page 48: Automated Web Testing using JavaScript

Demo - Invoking PhantomJS using Selenium

WebDriver

Page 49: Automated Web Testing using JavaScript

Simplifying the Stack4 Selenium acting as a proxy

4 PhantomJS supports WebDriver wire-level protocol

Page 50: Automated Web Testing using JavaScript

Simplifying the Stack4 Selenium acting as a proxy

4 PhantomJS supports WebDriver wire-level protocol

phantomjs --webdriver=4444

Page 51: Automated Web Testing using JavaScript

Demo - Using GhostDriver

Page 52: Automated Web Testing using JavaScript

Wrapping Up

Page 53: Automated Web Testing using JavaScript

Wrapping Up4 Many options for creating powerful Web tests using

JavaScript

4 Abundance of frameworks, many of which are modular

4 Unit and end-to-end tests in both browser-based and headless mode

Page 54: Automated Web Testing using JavaScript

Thank You!

Page 55: Automated Web Testing using JavaScript

Q&A4 Simon Guest, Distinguished Engineer, Neudesic LLC

4 simonguest.com (@simonguest)

4 http://github.com/simonguest/gids

4 http://slideshare.net/simonguest

Page 56: Automated Web Testing using JavaScript

-- http://www.amazon.com/File-New-Presentation-Developers-Professionals/dp/0615910459