Share
Embed
The State of Front-End Testing
Web Directions | Code 16
Sydney, July 29 2016
Melbourne, August 2 2016
Alicia Sedlock
July 29, 2016
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
96 slides
-
• Web Developer @ Society of Grownups • @aliciability on
Twitter • Teacher with Girl Develop It • Run Hedgehogs & Reflogs -
#frontendtesting As developers we don’t want to • Give users
broken products • Break trust with QA & business teams • Waste development time -
#frontendtesting Front-end testing is… • A collection of techniques to
hold developers accountable for writing and maintaining functioning and usable code bases. -
#frontendtesting The Beginnings of Testing • Hi, server-side developers! •
Here comes the client-side! • Opportunities galore and (sometimes neglected) responsibilities -
#frontendtesting Unit tests are for ensuring small pieces of code
work as expected. -
#frontendtesting describe(“Calculator Operations”, function () { it(“Should add two numbers”,
function () { Calculator.init(); var result = Calculator.addNumbers(7,3); expect(result).toBe(10); }); }); -
#frontendtesting describe(“Calculator Operations”, function () { it(“Should add two numbers”,
function () { Calculator.init(); var result = Calculator.addNumbers(7,3); expect(result).toBe(10); }); }); -
#frontendtesting describe(“Calculator Operations”, function () { it(“Should add two numbers”,
function () { Calculator.init(); var result = Calculator.addNumbers(7,3); expect(result).toBe(10); }); }); -
#frontendtesting describe(“Calculator Operations”, function () { it(“Should add two numbers”,
function () { Calculator.init(); var result = Calculator.addNumbers(7,3); expect(result).toBe(10); }); }); -
#frontendtesting describe(“Calculator Operations”, function () { it(“Should add two numbers”,
function () { Calculator.init(); var result = Calculator.addNumbers(7,3); expect(result).toBe(10); }); }); -
#frontendtesting describe(“Calculator Operations”, function () { beforeEach(function () { Calculator.init();
}); ... afterEach(function () { Calculator.teardown(); }); }); -
#frontendtesting it(“Should remember the last calculation”, function () { spyOn(Calculator,
“updateCurrentValue”); Calculator.addNumbers(7,10); expect(Calculator.updateCurrentValue).toHaveBeenCalled(); expect(Calculator.updateCurrentValue) .toHaveBeenCalledWith(17); expect(Calculator.currentValue).toBe(17); }); -
#frontendtesting it(“Should remember the last calculation”, function () { spyOn(Calculator,
“updateCurrentValue”); Calculator.addNumbers(7,10); expect(Calculator.updateCurrentValue).toHaveBeenCalled(); expect(Calculator.updateCurrentValue) .toHaveBeenCalledWith(17); expect(Calculator.currentValue).toBe(17); }); -
#frontendtesting it(“Should remember the last calculation”, function () { spyOn(Calculator,
“updateCurrentValue”); Calculator.addNumbers(7,10); expect(Calculator.updateCurrentValue).toHaveBeenCalled(); expect(Calculator.updateCurrentValue) .toHaveBeenCalledWith(17); expect(Calculator.currentValue).toBe(17); }); -
#frontendtesting it(“Should remember the last calculation”, function () { spyOn(Calculator,
“updateCurrentValue”); Calculator.addNumbers(7,10); expect(Calculator.updateCurrentValue).toHaveBeenCalled(); expect(Calculator.updateCurrentValue) .toHaveBeenCalledWith(17); expect(Calculator.currentValue).toBe(17); }); -
#frontendtesting it(“Should remember the last calculation”, function () { spyOn(Calculator,
“updateCurrentValue”); Calculator.addNumbers(7,10); expect(Calculator.updateCurrentValue).toHaveBeenCalled(); expect(Calculator.updateCurrentValue) .toHaveBeenCalledWith(17); expect(Calculator.currentValue).toBe(17); }); -
#frontendtesting Some big players • Jasmine • Mocha • Chai
• Webdriver.io • QUnit • Unit.js • Sinon -
#frontendtesting Integration tests check that all the small pieces play
well together in the bigger picture. -
#frontendtesting test('it renders completed class properly', function(assert) { set(this, 'currentStepIndex',
0); set(this, 'step', { index: 1 }); this.render(hbs`{{progress-bar/progress-step this}}`); assert.notOk(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is not marked complete'); run(() => { set(this, 'currentStepIndex', 1); }); assert.ok(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is marked complete'); }); -
#frontendtesting test('it renders completed class properly', function(assert) { set(this, 'currentStepIndex',
0); set(this, 'step', { index: 1 }); this.render(hbs`{{progress-bar/progress-step this}}`); assert.notOk(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is not marked complete'); run(() => { set(this, 'currentStepIndex', 1); }); assert.ok(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is marked complete'); }); -
#frontendtesting test('it renders completed class properly', function(assert) { set(this, 'currentStepIndex',
0); set(this, 'step', { index: 1 }); this.render(hbs`{{progress-bar/progress-step this}}`); assert.notOk(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is not marked complete'); run(() => { set(this, 'currentStepIndex', 1); }); assert.ok(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is marked complete'); }); -
#frontendtesting test('it renders completed class properly', function(assert) { set(this, 'currentStepIndex',
0); set(this, 'step', { index: 1 }); this.render(hbs`{{progress-bar/progress-step this}}`); assert.notOk(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is not marked complete'); run(() => { set(this, 'currentStepIndex', 1); }); assert.ok(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is marked complete'); }); -
#frontendtesting test('it renders completed class properly', function(assert) { set(this, 'currentStepIndex',
0); set(this, 'step', { index: 1 }); this.render(hbs`{{progress-bar/progress-step this}}`); assert.notOk(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is not marked complete'); run(() => { set(this, 'currentStepIndex', 1); }); assert.ok(this.$(‘.progress-bar__step').hasClass('progress-bar__step--completed'), 'step is marked complete'); }); -
#frontendtesting describe(“Acceptance tests”, function () { var page; beforeEach(function (done)
{ page = visit(“/home”); page.ready(done); }); describe(“Sign Up Failure state”, function () { beforeEach(function (done) { page.fill_in(“input[name='email']", “Not An Email”); page.click(“button[type=submit]”); page.onBodyChange(done); }); it(“Shouldn’t allow signup with invalid information”, function () { expect(page.find(“#signupError”) .hasClass(“hidden”)).toBeFalsy(); }); }); }); -
#frontendtesting describe(“Acceptance tests”, function () { var page; beforeEach(function (done)
{ page = visit(“/home”); page.ready(done); }); describe(“Sign Up Failure State ”, function () { beforeEach(function (done) { page.fill_in(“input[name='email']", “Not An Email”); page.click(“button[type=submit]”); page.onBodyChange(done); }); it(“Shouldn’t allow signup with invalid information”, function () { expect(page.find(“#signupError”) .hasClass(“hidden”)).toBeFalsy(); }); }); }); -
#frontendtesting describe(“Acceptance tests”, function () { var page; beforeEach(function (done)
{ page = visit(“/home”); page.ready(done); }); describe(“Sign Up Failure state”, function () { beforeEach(function (done) { page.fill_in(“input[name='email']", “Not An Email”); page.click(“button[type=submit]”); page.onBodyChange(done); }); it(“Shouldn’t allow signup with invalid information”, function () { expect(page.find(“#signupError”) .hasClass(“hidden”)).toBeFalsy(); }); }); }); -
#frontendtesting describe(“Acceptance tests”, function () { var page; beforeEach(function (done)
{ page = visit(“/home”); page.ready(done); }); describe(“Sign Up Failure state”, function () { beforeEach(function (done) { page.fill_in(“input[name='email']", “Not An Email”); page.click(“button[type=submit]”); page.onBodyChange(done); }); it(“Shouldn’t allow signup with invalid information”, function () { expect(page.find(“#signupError”) .hasClass(“hidden”)).toBeFalsy(); }); }); }); -
#frontendtesting More tools! • jasmine-integration • Karma • Selenium •
Nightwatch • Again, many others -
#frontendtesting The Evolution • Front-end development is more than just
business logic • There are other parts of the code that can fail • Apply the tactic of testing expected outcomes -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(“#signUpForm”, { name: “Alicia Sedlock”, email: “[email protected]” }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(‘#signUpForm', { name: ‘Alicia Sedlock’, email: ‘[email protected]’ }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(‘#signUpForm', { name: ‘Alicia Sedlock’, email: ‘[email protected]’ }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(‘#signUpForm', { name: ‘Alicia Sedlock’, email: ‘[email protected]’ }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(‘#signUpForm', { name: ‘Alicia Sedlock’, email: ‘[email protected]’ }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(‘#signUpForm', { name: ‘Alicia Sedlock’, email: ‘[email protected]’ }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/home”).then(function(){ // Initial state of form phantomcss .screenshot(“#signUpForm”, “sign
up form”); // Hit the sign up button (should trigger error) casper.click(“button#signUp”); // Take a screenshot of the UI component phantomcss .screenshot(“#signUpForm”, “sign up form error”); // Fill in form by name attributes & submit casper.fill(‘#signUpForm', { name: ‘Alicia Sedlock’, email: ‘[email protected]’ }, true); // Take a second screenshot of success state phantomcss .screenshot(“#signUpForm”, “sign up form success”); }); -
#frontendtesting casper.start(“/styleguide”).then(function(){ phantomcss .screenshot(“a.link”, “link example”); phantomcss .screenshot(“.btn-primary”, “primary button”);
}).then(function() { this.mouse.move(“.btn-primary”'); phantomcss .screenshot(“.btn-primary”, “button hover”); }); -
#frontendtesting phantomcss: { options: { mismatchTolerance: 0.05, screenshots: ‘tests/visual/baselines’, results:
‘tests/visual/results’, viewportSize: [1280, 800] }, src: [‘tests/visual/tests.js’] } -
#frontendtesting phantomcss: { big_size: { options: { ... viewportSize: [1280,
800] } }, small_size: { options: { ... viewportSize: [480, 320] } } } -
#frontendtesting Visual regression tools • PhantomCSS/PhantomFlow • BackstopJS • Wraith
• Webdriver.io • Percy.io -
#frontendtesting Tools I’ve heard of • a11y • use grunt-a11y
for build tool automation • Pa11y • ember-a11y-testing • protractor-accessibility-plugin (Angular) • react-a11y -
#frontendtesting perfbudget: { default: { options: { url: [your site’s
URL], budget: { visualComplete: '3000', SpeedIndex: ‘1500’, render: ‘500’, fullyLoaded: ‘3000’, requests: ’25’, bytesInDoc: '100000' } } } } -
#frontendtesting gremlin formFiller input 5 in <input type="number" name="age"> gremlin
formFiller input [email protected] in <input type="email"> gremlin clicker click at 1219 301 gremlin scroller scroll to 100 25 ... mogwai fps 33.21 mogwai fps 59.45 mogwai fps 12.67 err > mogwai fps 7.54 < err -
#frontendtesting Interested in linting? • CSSLint • JSLint/JSHint • UnCSS
• HTMLLint • Framework-specific linting, too! -
So, you’re saying I should use all these tools, right?
#frontendtesting -
#frontendtesting Assessment • How much business logic are we off-loading
to the client-side? • What is our history with regressions? • Are we constantly fixing, and re-fixing, visual/UI issues? • In general, which piece of our stack is the most instable/Jenga-like? • Is there a lot of inconsistency in how we write code? -
This is more code to write, and my code will
still have bugs. #frontendtesting -
I’m working with legacy code, and it’ll be impossible to
test old features. #frontendtesting -
#frontendtesting • Frontend Architecture for Design Systems by Micah Godbolt
• FrontEndTesting.com Looking for more?