1 of 34

TEST AUTOMATION & DIGITAL QA SUMMIT

#TAS19

Auckland

November 7th, 2019

TESTINGMIND

www.testingmind.com

2 of 34

Postman live demo &�Postman object model

by Vincent Dirks

TESTINGMIND

www.testingmind.com

3 of 34

Postman object models -live demo

abstract

Live demo using Postman and show you can re-use code with a code library.

Dive into a new idea: "Postman Object Models".

A POM uses a code library with classes to persist data as Postman variables. The class objects can be used throughout your scripts with standard object references in your scripts. �eg. session.authToken

The POM classes are abstractions that hide the data persistence inside the class. You can create getters and setters for validation checks, and many other helper routines.

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

4 of 34

Postman object models -live demo

For example:

Use a POM to remember user session variables, such as a

  • authentication token
  • user details
  • list of accounts

Without a POM you would be storing the values in individual Postman variables, with unique, long/cryptic, names. This bloats the number of entries stored in the Postman variables.

Managing a long list of Postman variables is... problematic.

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

5 of 34

whoami

Vincent Dirks - full stack agile test analyst

  • I’ll give anything a go
    • Mobile
    • Web
    • API
    • DevOps
    • visual modelling
    • tools tools tools
    • widgets
    • scripts
  • Automation x 3 years
  • testing x 7 years
    • LeasePlan
    • Trade Me
    • Fiserv
  • test philosophy
    • Context Driven Testing
  • agile is to me
    • Autonomy
    • with Responsibility
    • through Trust

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

6 of 34

whoareyou

test analysts starting their journey into automation

This presentation is aimed at people who are beginning their journey of learning to program automation scripts to help their testing.

I am not a developer, started programing inside tools like ReadyAPI and Postman 3 years ago.

I am aiming this talk to people who are similarly inexperienced with programming and realise we won’t let the developers have all the fun by themselves.

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

7 of 34

overview

  • live demo
  • Postman as you might be using it now
  • how to reuse code
  • using classes in your code
  • Postman Object Models

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

8 of 34

the API under test

let’s explore the API created for this presentation

host: http://localhost:5000

routes

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

9 of 34

Postman

as you might be using it now...

  • create GET requests for
  • parameterise
    • {{hostURL}} = http://localhost:5000
    • create environment file
  • tests
    • code snippets
    • response is 200
    • response body contains info array of strings
    • Chai assertion library

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

10 of 34

aside - javascript arrow functions

are ‘syntactically compact alternatives

function nameOfFunction(inputParams){

do_some_stuff();

return x;

}

is the same as

  • inputParam => x …1 param, return x
  • () => x … no params, return x
  • (p1,p2,...) => x … multiple input params
  • param => {do_some_stuff(); return x;}… with statements

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

11 of 34

aside - javascript array methods

with arrow functions are way cool

At some point we will receive a response containing an array

  • don’t reference items by index

pm.expect(resp.info[2]).to.eql(someValue);)

  • use array methods find(), filter(), forEach(), map() etc.

pm.expect(� resp.info.find(item => item.includes(...))�).to.be.a(...);

  • MDN JavaScript Arrays

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

12 of 34

I keep writing the same test script…

how to reuse code

  • codeLibrary global variable
    • in a globals file
    • in a dummy request
  • eval(codeLibrary) when needed
    • eval(pm.globals.get(`codeLibrary`));
  • I thought eval() was evil?�only when you give users access to the executed string

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

13 of 34

codeLibrary

how to create one in Postman

  • dummy request
    • to the most benign endpoint of your system
  • pre-request script - save `codeLibrary`

pm.globals.set(`codeLibrary`,� `console.log("loading code library");`�);

  • eval(codeLibrary) when you want to use it

eval(pm.globals.get(`codeLibrary`));

  • syntax highlighting

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

14 of 34

codeLibrary

"and" 'javascript' `strings`

these are all javascript strings

  • "double quotes"
    • "can have 'single quotes' inside it"
  • 'single quotes'
    • 'can have "double quotes" in it'
  • `backtick`
    • ` can spread over �multiple lines`
    • `can have ${expr} in the string`

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

15 of 34

let’s code...

checkInfoResponse()

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

16 of 34

let’s test

/api/v1/info

  • run Code Library request to update the global variable
    • don’t forget to remove `syntax` highlighting
  • get & eval the codeLibrary

eval(pm.globals.get(`codeLibrary`));

  • run a function from the codeLibrary

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

17 of 34

rinse and repeat

/api/v1/*/info

  • /api/v1/rates/info
  • /api/v1/rates
  • /api/v1/person
  • /api/v1/person/login (GET)
  • /api/v1/person/login (POST)
  • /api/v1/person/accounts
  • /api/v1/person/account/{{accountId}}/transactions

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

18 of 34

let’s reflect & any questions?

and how are we doing for time...

  • simple API - had a look in Chrome
  • created requests in Postman
  • added checks
  • used {{variables}}
  • saved the {{sessionId}} after successful login
  • fetched the user’s accounts
  • created the codeLibrary as a global variable
  • implemented checkInfoResponse() function �and used it for all /api/v1/*/info response validation

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

19 of 34

classes in the codeLibrary ?

I wonder if we could …?

  • let’s try encapsulate the account validation

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

20 of 34

using classes in the codeLibrary

account class

Notes

  • valid account types array
  • constructor
  • using try..catch to manage exceptions
  • logging errors to console
  • could use schema check

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

21 of 34

using classes in the codeLibrary

get account response handler

Notes

  • can use response input parameter
  • throw exception for incorrect usage
  • try..catch to manage JSON parsing exceptions
  • logging errors to console
  • account.isValid() checks itself ;-)

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

22 of 34

create the User class

design

  • properties
      • name
      • password
  • constructor with parameters
      • { "name":"Grace Hopper", "password": "Password123" }
  • use getters and setters
      • internal properties are prefixed with ‘_’
  • set globals for login
      • {{name}} & {{password}}
  • response handlers for login and logout

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

23 of 34

User class

implementation

Notes

  • slightly different class declaration
  • login handler is static

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

24 of 34

can we persist js objects ?

let’s try save js objects to the globals

  • only globals & environment variables persist between scripts and requests
  • javascript variable scope is limited to the script that declared it
  • wouldn’t be cool if we could create javascript objects in one script, and then be accessed in any other scripts… ?
  • maybe we can serialise js objects, and save them in the globals?

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

25 of 34

object persistence

design

  • each instance has to know its global variable name
  • save() serialises & saves instance to the globals
  • constructor() fetches & parses data from the globals
  • something needs to trigger the reconstruction of the object
    • usually whenever the codeLibrary is loaded (ie. eval’d)

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

26 of 34

object persistence

implementation (User class)

Notes

  • fetch & parse JSON
  • save()
  • sessionId set/get/...

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

27 of 34

let’s use it...

the User class

  • codeLibrary
    • automatically reloads the currentUser & sessionId if logged in
  • login pre-request script
  • login test script

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

28 of 34

have some fun

select a random user

  • login pre-request
  • User class method

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

29 of 34

could we add the user’s accounts ?

after all they belong to the user

  • re-use the accounts class
  • add array of accounts to User class
  • get accounts response handler
    • checks status & accounts are all valid
    • adds valid accounts from response to user account list
  • save user

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

30 of 34

user accounts

implementation (User class)

Notes

  • check status
  • check accounts
  • push accounts

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

31 of 34

user accounts

User constructor changes

  • (re)construction from serialised global loses object type of properties
  • need to (re)cast each account to Account class

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

32 of 34

some more fun

get random account’s rate details and transactions

  • set globals for rate & transactions request in Accounts class
  • pre-request scripts

or

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

33 of 34

let’s reflect

Postman Object Model ...

  • we can use classes in the codeLibrary
  • encapsulated the account validation in Account class
  • created the User class with login/logout response handlers
  • persisted user objects by serialising into a global variable
  • used the persisted user in the login pre-req & test scripts
  • fun = selecting random users
  • added user accounts to User class
  • casting class properties to their proper type
  • random account’s rate and transactions

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland

34 of 34

thank you & questions ?

resources

  • GitHub
    • POM Postman Collection
    • simple test API
  • this google slide deck
    • feel free to share it
  • see me on

?

TESTINGMIND CONSULTING

- Test Automation and Digital QA Event | Auckland