📺 Develpreneur YouTube Episode

Video + transcript

WebDriver IO - Part 2

2022-07-14 •Youtube

Detailed Notes

In this presentation, we are going to be talking about using the WebDriver IO: Building Automation Scripts using Javascript. Now, this is not really going to be a slide-driven presentation. Instead, we are going to go through the website and talk about the different technologies, and things you can do with WebDriver. Then, we're going to jump into a full hands-on example, building your first project, and looking at some of the different ways you can use WebDriver for testing.

In the past, I've talked about code test frameworks. Such as, topics about the different ways you can build your tests, and how you should test. Plus, we've talked about different things like unit testing, PHP testing, JUnit, every language, essentially own way of testing, and JavaScript's, no different. So open up the link below and enjoy the presentation.

WebDriver IO: Building Automation Scripts in JS Overview:

What is WebDriver IO How to build tests. The different types of frameworks it supports Reporting Hands-on Demo

Transcript Text
[Music]
so if you were to open up chrome
which i closed
let's see go over here so if you go to
chrome
and if we open up the developer tools
simply open up our developer tools we
have this console here
now if you're doing any like
console lookups you have
dollar you have dollar dollars
and this allows you to run different
commands different lookups
uh you can look for images you can look
for
uh different things so what webdriver io
essentially does is it uses all of those
chrome
developer tools
and it allows you to essentially look up
the exact same way you would in
webdriver
that you would in the chrome developer
tools so you can look up by c uh css
query h2 sub query a
and use the dollar
uh you can do by xpath uh you can do the
full xpath lookup again dollar and you
do the path uh you can do it by tag name
so you can do a weight parent so see
here you can look up okay find me the
top of the dom tree and then you can do
parent dot get tag name and it outputs
body
very simple so if you're if you are used
to using the
chrome
developer tools
it's almost a no-brainer it translates
almost one-to-one
so you basically create your objects or
your constants and then you use the
constants button.click
so
this command here returns you the web
element if you were in webdriver
and here's you're essentially getting
the element itself as a constant and
then you just walk through it
you can set timeouts
so instead of pause you actually set
your page timeouts you set your session
timeouts page load timeouts
all that's here browser objects so just
like in selenium webdriver you have the
capability of configuring the different
um
browser objects for chrome firefox ie
etc
mock and spies this is interesting
i never really thought about this
because i've always had to write my own
but they give you the ability to do that
top-down bottom-up approach of testing
so it allows you to create mocks and
spies to simulate calls to apis so if
something doesn't exist or you want to
kind of get to the next piece you can
actually put in those drivers and those
steps the option spies so this was kind
of cool
uh custom commands
so you can create your own commands
page object pattern
uh we've i've talked about this
exhaustively in other presentations but
unfortunately webdriver i o
the page object pattern here has the
same limitation if you're looking at the
page object pattern for selenium for
java or for any of the other back-end
tools reason is the page object pattern
still loads everything into memory while
you're testing now granted this is
javascript it's a lot lighter you're not
taking as much of a hit but if you have
a larger page you're going to run into
the same problem where you're going to
be trying to load a lot of elements in
the memory
and work with them at once even if you
don't need them
and we have the capability of using uh
debugging i'll show you how this
configure
visual code so you can actually test
your code
we have things like test runner
frameworks so you can embed use existing
frameworks a jasmine cucumber
so like i said it gives you out of the
box it helps you
pick a framework
i'm not a huge fan of cucumber but mocha
and jasmine look pretty straightforward
pretty easy to use
kind of still follows that behavior
driven development so you describe
your test and
or your describe your suite and here's
your test it should do something here
and we call them some things
they still push the surgeons which i
like because if you create your own
custom assertions
you can reuse them throughout your
projects so this is kind of nice
so you can do a weight for this and then
you can do a weight expect
expect chai
so you have some asserts which are good
uh you can do test suites you can do in
run individual tests
uh there's some migration if you're
trying to move from older
integration so here's
another thing that i like about this so
they give you auto completion
docker
github actions so you can integrate with
github jenkins and bamboo
reporter if you want to do different
types of reporting here's all the
examples and things that it incorporates
with
it's really nice
and services here's all the different
services it also can interact with and
engage with there's a lot
so from a front-end perspective if
you're dealing with javascript this is a
wonderful tool to use
however it does have a lot of the
limitations that it is built on top of
saline
so the fact that this is essentially a
javascript wrapper for an existing
library
it makes me a little concerned that
maybe this might not be supported over
time so it's just something to be
considerate about
if you go down this route and use
webdriver io
all right so that's kind of a very high
level of web driver i o uh some other
things we can use you have the api here
which actually gives you the different
commands and that that you can use to
set up webdriver
i'm not going to get into that you can
look at that on your own
uh here's all those commands i was
telling you the chrome driver
you can use react
command save pdf
so this is neat you don't have to worry
about making your own pdf it's got
already got that built in
it does screenshots you can upload a
file that's uh handy
all these exist
in selenium webdriver as well you just
have to write
the actual uh you just have to use
or write a method to actually implement
this so you could do it this way
in either
all right so let's jump out of here
and go back to our example here
all right
clear this mess
so here we are in our example but let me
go back up one level to my
working directory here
and let me
open up digital code
all right so
here's visual code i've checked out
some of those example projects
so i have i checked out the appium i
checked out the cucumber
the jasmine and the webdriver i o master
file now before we go look at this let's
actually look at setting up our example
project here that we just did
and
get it to where it will run in debug
mode so the first thing you need to do
is open up terminal
and come down here
and do command shift
b
yeah no not b
it is
command shift p
i had to write down because i don't use
visual studio all that much
so ctrl shift p brings up the properties
and then you want to look for
debug toggle auto attach so if you
select this one
it gives you the option
to
enable
the debugger to run
every time you run a node.js process
uh
only when you're in your particular
project you can do it by flag so i set
it to always so anytime i run a node.js
project is going to run the debugger
so now if i come down here and i go into
my example two
and if i wanted to just run
the project i just hit run you notice
down here at the bottom it turned orange
the debugger was attached
i see my debugger options here
and it failed to run because i actually
forgot a command so if we go back out to
our docs and go back to getting started
so if i want to run this
we just run the config
so running with wdios config this will
run the entire test suite
we'll say yes
should be there
we'll look at example two
we have our test yeah everything should
be there there it is w i o config ps
okay let me close terminal and reopen
your model
criminal new terminal there we go
if i run the
run command
this will run the entire test suite
and it attaches to debugger
and it runs the exact same test that
example 2 had the only difference here
is in this particular project i actually
have some breakpoints
so in visual code i've come out here and
i've applied a break
and when i ran the test now i have my
debug tools so now i can step over i can
run to the end
or i can just
skip
each step
so here if i step over it's going to
click log in
let me go back now i'm on the secure
page
now it's going to check
to make sure that the
flush alert to be expected and having
text containing you've logged into a
secured area
you've logged into a secure area
so now if i just keep walking through
and we're done
so that is
how to run in debug
that's all it is you just turn on the
debugger for node.js and then you come
in and you add breakpoints
so let's go look at the example project
so example one is essentially the same
as example two except apparently i
included the alert reporting and
another library that was required for
visual code
so let's walk through so we have the
wdio config
this is just the standard configuration
file for webdriver io
you can set the different specs
tell where the tests are you can exclude
specific files
how many instances do you want to run
you can set your capabilities so you can
set the browser names uh multiple
browsers so you can change from chrome
to firefox ie safari
uh top level info so you can change your
logger to any of the
uh
standards which is nice
uh bail how many tests run before you
kick out
your base url in case you
don't
uh if you all your tests want to start
from the
sample url or the top
uh here's your default wait timeout for
the weight for connect retry connection
retry count
again this looks very similar to my
framework so all you essentially are
doing are setting property files
we have our package json so this is kind
of similar to maven so this is where you
put all your dependencies
so here we have chrome chrome driver
jasmine cli
oh that's right i did mocha for this one
and i did jasmine for this one
so here's our webdriver
web driver i o tests
and the scripts
then we have the lock json so
this gets generated for you
json config
so you add your compiler options
webdriver sync mocha
node
uh your node modules these are installed
for you
as part of node that was where it was
asking you did you want to install the
npm
modules yes
your alert results so we ran our test so
here's our test results
you
Transcript Segments
0.43

[Music]

27.039

so if you were to open up chrome

29.679

which i closed

31.679

let's see go over here so if you go to

34.32

chrome

37.2

and if we open up the developer tools

45.68

simply open up our developer tools we

47.92

have this console here

50

now if you're doing any like

54.96

console lookups you have

57.52

dollar you have dollar dollars

59.84

and this allows you to run different

61.84

commands different lookups

63.92

uh you can look for images you can look

66.24

for

67.28

uh different things so what webdriver io

70.4

essentially does is it uses all of those

73.68

chrome

74.64

developer tools

76.4

and it allows you to essentially look up

80.24

the exact same way you would in

81.68

webdriver

83.28

that you would in the chrome developer

85.04

tools so you can look up by c uh css

88

query h2 sub query a

91.04

and use the dollar

92.96

uh you can do by xpath uh you can do the

96

full xpath lookup again dollar and you

98.88

do the path uh you can do it by tag name

101.84

so you can do a weight parent so see

104.56

here you can look up okay find me the

107.04

top of the dom tree and then you can do

109.439

parent dot get tag name and it outputs

112.159

body

114.24

very simple so if you're if you are used

116.399

to using the

117.84

chrome

118.799

developer tools

120.399

it's almost a no-brainer it translates

122.719

almost one-to-one

124.88

so you basically create your objects or

127.04

your constants and then you use the

128.8

constants button.click

131.039

so

131.92

this command here returns you the web

134.4

element if you were in webdriver

137.2

and here's you're essentially getting

139.2

the element itself as a constant and

141.84

then you just walk through it

144.959

you can set timeouts

147.92

so instead of pause you actually set

149.84

your page timeouts you set your session

152.239

timeouts page load timeouts

154.959

all that's here browser objects so just

157.84

like in selenium webdriver you have the

160.72

capability of configuring the different

163.12

um

164.48

browser objects for chrome firefox ie

168.48

etc

170.16

mock and spies this is interesting

173.12

i never really thought about this

175.12

because i've always had to write my own

177.12

but they give you the ability to do that

181.04

top-down bottom-up approach of testing

184.239

so it allows you to create mocks and

186.64

spies to simulate calls to apis so if

190.48

something doesn't exist or you want to

192.4

kind of get to the next piece you can

194.48

actually put in those drivers and those

196.64

steps the option spies so this was kind

198.8

of cool

200.159

uh custom commands

202

so you can create your own commands

205.12

page object pattern

206.72

uh we've i've talked about this

208.319

exhaustively in other presentations but

212.239

unfortunately webdriver i o

214.64

the page object pattern here has the

217.2

same limitation if you're looking at the

219.28

page object pattern for selenium for

222

java or for any of the other back-end

224.84

tools reason is the page object pattern

228.72

still loads everything into memory while

231.36

you're testing now granted this is

233.68

javascript it's a lot lighter you're not

236.4

taking as much of a hit but if you have

239.36

a larger page you're going to run into

241.519

the same problem where you're going to

242.799

be trying to load a lot of elements in

245.12

the memory

246.239

and work with them at once even if you

248.4

don't need them

250.4

and we have the capability of using uh

252.799

debugging i'll show you how this

254.72

configure

256.239

visual code so you can actually test

258.32

your code

260.88

we have things like test runner

263.6

frameworks so you can embed use existing

266.479

frameworks a jasmine cucumber

269.12

so like i said it gives you out of the

270.72

box it helps you

272.479

pick a framework

274.4

i'm not a huge fan of cucumber but mocha

276.639

and jasmine look pretty straightforward

278.8

pretty easy to use

281.12

kind of still follows that behavior

282.88

driven development so you describe

285.68

your test and

287.44

or your describe your suite and here's

289.44

your test it should do something here

292

and we call them some things

298.32

they still push the surgeons which i

300

like because if you create your own

301.6

custom assertions

303.759

you can reuse them throughout your

305.44

projects so this is kind of nice

308.16

so you can do a weight for this and then

310.72

you can do a weight expect

315.12

expect chai

318.16

so you have some asserts which are good

321.52

uh you can do test suites you can do in

324.4

run individual tests

327.36

uh there's some migration if you're

329.12

trying to move from older

331.36

integration so here's

333.68

another thing that i like about this so

335.28

they give you auto completion

337.36

docker

338.639

github actions so you can integrate with

340.4

github jenkins and bamboo

343.919

reporter if you want to do different

345.68

types of reporting here's all the

347.36

examples and things that it incorporates

349.84

with

351.44

it's really nice

353.52

and services here's all the different

355.84

services it also can interact with and

358.639

engage with there's a lot

361.919

so from a front-end perspective if

364.16

you're dealing with javascript this is a

366.16

wonderful tool to use

368.72

however it does have a lot of the

370.72

limitations that it is built on top of

374.639

saline

375.84

so the fact that this is essentially a

378.08

javascript wrapper for an existing

379.919

library

381.12

it makes me a little concerned that

383.28

maybe this might not be supported over

385.199

time so it's just something to be

386.96

considerate about

388.479

if you go down this route and use

390.319

webdriver io

393.44

all right so that's kind of a very high

395.28

level of web driver i o uh some other

398.56

things we can use you have the api here

400.96

which actually gives you the different

402.319

commands and that that you can use to

404.56

set up webdriver

406.24

i'm not going to get into that you can

407.84

look at that on your own

410

uh here's all those commands i was

411.599

telling you the chrome driver

414.72

you can use react

416.56

command save pdf

418.639

so this is neat you don't have to worry

420.56

about making your own pdf it's got

422.319

already got that built in

424.16

it does screenshots you can upload a

426.72

file that's uh handy

429.36

all these exist

431.12

in selenium webdriver as well you just

433.759

have to write

435.599

the actual uh you just have to use

439.039

or write a method to actually implement

441.199

this so you could do it this way

443.84

in either

446.16

all right so let's jump out of here

449.52

and go back to our example here

453.28

all right

454.4

clear this mess

457.039

so here we are in our example but let me

459.759

go back up one level to my

462.16

working directory here

464.96

and let me

467.68

open up digital code

472

all right so

473.84

here's visual code i've checked out

477.28

some of those example projects

482.4

so i have i checked out the appium i

485.12

checked out the cucumber

487.199

the jasmine and the webdriver i o master

489.84

file now before we go look at this let's

492.56

actually look at setting up our example

495.44

project here that we just did

497.599

and

498.56

get it to where it will run in debug

500.639

mode so the first thing you need to do

502.56

is open up terminal

505.44

and come down here

506.879

and do command shift

509.84

b

511.28

yeah no not b

513.919

it is

521.68

command shift p

525.839

i had to write down because i don't use

528.32

visual studio all that much

531.04

so ctrl shift p brings up the properties

533.519

and then you want to look for

540.08

debug toggle auto attach so if you

543.36

select this one

545.279

it gives you the option

547.6

to

548.64

enable

549.92

the debugger to run

551.839

every time you run a node.js process

555.12

uh

556

only when you're in your particular

558.399

project you can do it by flag so i set

561.44

it to always so anytime i run a node.js

565.519

project is going to run the debugger

568.64

so now if i come down here and i go into

571.36

my example two

579.36

and if i wanted to just run

582.56

the project i just hit run you notice

585.2

down here at the bottom it turned orange

587.12

the debugger was attached

589.76

i see my debugger options here

595.12

and it failed to run because i actually

597.36

forgot a command so if we go back out to

602.8

our docs and go back to getting started

605.6

so if i want to run this

608.079

we just run the config

618.16

so running with wdios config this will

621.2

run the entire test suite

626.32

we'll say yes

640.079

should be there

642

we'll look at example two

645.36

we have our test yeah everything should

647.12

be there there it is w i o config ps

653.839

okay let me close terminal and reopen

655.68

your model

658

criminal new terminal there we go

661.519

if i run the

663.36

run command

665.279

this will run the entire test suite

671.68

and it attaches to debugger

675.36

and it runs the exact same test that

677.04

example 2 had the only difference here

679.839

is in this particular project i actually

682.64

have some breakpoints

684.32

so in visual code i've come out here and

686.72

i've applied a break

688.56

and when i ran the test now i have my

690.399

debug tools so now i can step over i can

693.12

run to the end

695.279

or i can just

696.88

skip

697.76

each step

698.959

so here if i step over it's going to

701.279

click log in

703.2

let me go back now i'm on the secure

705.12

page

706.079

now it's going to check

708.079

to make sure that the

709.6

flush alert to be expected and having

712.48

text containing you've logged into a

714.88

secured area

717.519

you've logged into a secure area

720

so now if i just keep walking through

726.16

and we're done

729.519

so that is

731.279

how to run in debug

735.04

that's all it is you just turn on the

736.8

debugger for node.js and then you come

739.68

in and you add breakpoints

741.68

so let's go look at the example project

745.68

so example one is essentially the same

748.079

as example two except apparently i

750.16

included the alert reporting and

753.36

another library that was required for

755.68

visual code

758.079

so let's walk through so we have the

759.76

wdio config

761.92

this is just the standard configuration

764.16

file for webdriver io

767.92

you can set the different specs

772.079

tell where the tests are you can exclude

774.32

specific files

777.12

how many instances do you want to run

780.24

you can set your capabilities so you can

782.16

set the browser names uh multiple

784.48

browsers so you can change from chrome

786.32

to firefox ie safari

789.92

uh top level info so you can change your

792.88

logger to any of the

795.2

uh

796

standards which is nice

798.959

uh bail how many tests run before you

801.68

kick out

802.8

your base url in case you

805.6

don't

806.72

uh if you all your tests want to start

808.72

from the

810.24

sample url or the top

812.639

uh here's your default wait timeout for

814.8

the weight for connect retry connection

817.76

retry count

819.36

again this looks very similar to my

821.12

framework so all you essentially are

823.76

doing are setting property files

831.76

we have our package json so this is kind

834

of similar to maven so this is where you

835.839

put all your dependencies

838.639

so here we have chrome chrome driver

841.12

jasmine cli

844.399

oh that's right i did mocha for this one

846.32

and i did jasmine for this one

850.56

so here's our webdriver

852.88

web driver i o tests

855.199

and the scripts

858.48

then we have the lock json so

862.56

this gets generated for you

867.44

json config

870.48

so you add your compiler options

872.399

webdriver sync mocha

874.8

node

876.639

uh your node modules these are installed

879.44

for you

880.639

as part of node that was where it was

882.24

asking you did you want to install the

883.76

npm

885.04

modules yes

886.88

your alert results so we ran our test so

889.76

here's our test results

909.76

you