1
00:00:02,510 --> 00:00:04,280
Now that we had a look at unit tests,

2
00:00:04,280 --> 00:00:07,280
what they are, why we use them and integration tests,

3
00:00:07,310 --> 00:00:12,770
let's have a look at end-to-end tests or user interface tests. For this, I'll need to install another tool.

4
00:00:12,770 --> 00:00:21,290
So I'll quit my jest process here and I will install, with save dev, puppeteer, which is a headless version

5
00:00:21,290 --> 00:00:23,060
of Chrome, of the Chrome browser.

6
00:00:23,060 --> 00:00:27,010
So it basically is a browser we can use to interact with the DOM and so on,

7
00:00:27,050 --> 00:00:29,660
we can even run in a version with the head,

8
00:00:29,660 --> 00:00:35,960
so where we see the browser and we can basically define steps that should be executed in that browser

9
00:00:36,160 --> 00:00:39,950
so that we can automate certain processes on our web page

10
00:00:40,070 --> 00:00:44,390
and then of course test the result of these processes as well.

11
00:00:44,390 --> 00:00:49,940
Now this download will take a while because in the end it downloads the entire Chrome codebase because

12
00:00:49,970 --> 00:00:54,200
it needs that to be able to simulate that browser

13
00:00:54,410 --> 00:00:56,440
ANd once it's done, I'll be back.

14
00:00:56,710 --> 00:00:57,820
So I'm done installing it,

15
00:00:57,830 --> 00:01:05,330
LEt's start using puppeteer now and for that, I'll go to my utiltest.js file and I will import puppeteer.

16
00:01:06,300 --> 00:01:15,380
So I'll import puppeteer by requiring it from puppeteer like this and I will write a new test

17
00:01:15,380 --> 00:01:23,730
now and there, I will just write should click around for now, we'll fix this later or change this later

18
00:01:23,890 --> 00:01:27,480
and in this function which should be executed here, I want to use puppeteer.

19
00:01:27,520 --> 00:01:28,640
Now how can I use it?

20
00:01:28,780 --> 00:01:37,180
First of all I set up a browser by using puppeteer launch.

21
00:01:37,190 --> 00:01:41,350
Now this will launch a browser with some options which I can define here,

22
00:01:41,390 --> 00:01:47,720
for example I can set headless to false to actually run a browser with a user interface as well,

23
00:01:47,930 --> 00:01:52,640
you can set a bunch of stuff here and you can find it all in the official docs of puppeteer of course.

24
00:01:52,700 --> 00:01:55,700
I will also set slomo here,

25
00:01:55,700 --> 00:02:00,820
that will basically slow down the automated operations so that we have a chance of seeing what's happening

26
00:02:00,830 --> 00:02:08,240
and I'll set it to a value of 80 and we can also set some args here which is an array where I can

27
00:02:08,240 --> 00:02:14,600
pass dash dash in and for example window size to launch this browser with these arguments and window

28
00:02:14,600 --> 00:02:21,470
size allows me to set a width height pair, so I can set this to 1920 and 1080 for

29
00:02:21,470 --> 00:02:27,080
example, like this, this should launch a browser with this size and that of course can be great

30
00:02:27,080 --> 00:02:32,290
to also test if certain responsive features are working the way you want.

31
00:02:32,450 --> 00:02:34,790
So now this will create a browser.

32
00:02:34,790 --> 00:02:39,090
Now actually, this will return a promise,

33
00:02:39,380 --> 00:02:47,450
so we can use async await here if we want to by placing async in front of this pair of arguments here

34
00:02:47,690 --> 00:02:49,530
or this empty argument list

35
00:02:49,670 --> 00:02:55,760
and now we can await this to launch. The alternative would be to just use then and catch, async await

36
00:02:55,760 --> 00:02:57,590
in the end does this for you.

37
00:02:57,590 --> 00:03:04,730
So here I'm awaiting this and storing the result in browser you could say, as a next step,

38
00:03:04,730 --> 00:03:16,220
I can now create a page object by awaiting the browser to create a new page, like this.

39
00:03:16,240 --> 00:03:23,020
Now I'll also need to tell the browser which page to load and I'll grab my URL here and then we can

40
00:03:23,020 --> 00:03:26,420
say page, go to

41
00:03:26,440 --> 00:03:31,840
and then you enter your URL which should be loaded and you also should await this because all these

42
00:03:31,840 --> 00:03:36,490
browser related things are promises that simply take some time to execute

43
00:03:36,490 --> 00:03:38,730
so you have to await each step.

44
00:03:38,740 --> 00:03:42,510
So now this should open a browser and load this page.

45
00:03:42,580 --> 00:03:51,200
Let's try this by running npm test and it should indeed open up a brand new window in Chromium which

46
00:03:51,200 --> 00:03:53,390
is basically like a Chrome or

47
00:03:53,390 --> 00:04:00,620
the core functionality of Chrome and you also see that this is being tested or controlled by automated

48
00:04:00,620 --> 00:04:02,120
test software.

49
00:04:02,120 --> 00:04:04,680
So this was opened automatically,

50
00:04:04,820 --> 00:04:08,720
now let's go back and let's add some logic here.

51
00:04:08,750 --> 00:04:10,480
We don't just want to go to that page,

52
00:04:10,550 --> 00:04:18,040
let's say we also want to enter something. For that I want to enter something into my input with the

53
00:04:18,040 --> 00:04:19,630
ID name and age,

54
00:04:19,630 --> 00:04:27,640
so in my test I will await a step or I use that page where I navigate into my app where I will then

55
00:04:27,640 --> 00:04:35,080
click into the field with that selector, so click takes the selector of the item it should click on.

56
00:04:35,080 --> 00:04:38,580
Once I click there, I will type

57
00:04:38,860 --> 00:04:43,090
and again you can find all commands in the official puppeteer docs, so you can really like instruct

58
00:04:43,090 --> 00:04:45,460
the browser, the headless browser,

59
00:04:45,460 --> 00:04:50,950
what to do. So I want to type and I want to type into the same input and we wouldn't have to click on

60
00:04:50,950 --> 00:04:54,400
it first but I want to simulate all the steps a user would do

61
00:04:54,580 --> 00:05:00,250
and I want to insert let's say Anna here to mix it up, not always Max.

62
00:05:00,250 --> 00:05:01,850
So now I type in there,

63
00:05:01,870 --> 00:05:06,600
I will repeat these steps once I type the name for the age,

64
00:05:06,670 --> 00:05:12,920
so for the age field I will enter 28 let's say. And if you save that,

65
00:05:15,770 --> 00:05:18,840
it will open up a new window where you can see that it enters

66
00:05:18,850 --> 00:05:29,110
these things. Now we can now also add another step and click on this button at the bottom of our page,

67
00:05:29,110 --> 00:05:36,030
so in app.js, in the end it's this button we're listening on for an event here with ID button add

68
00:05:36,040 --> 00:05:42,790
user. We can grab that selector and make sure we click on that in our setup and now we get a full flow setup

69
00:05:45,820 --> 00:05:53,040
where you see that this gets entered and then we click this button here at the end. You also saw that

70
00:05:53,070 --> 00:05:59,040
it didn't add an item and this already shows us that writing this text was not the worst idea because

71
00:05:59,040 --> 00:06:06,090
it looks like something was broken and indeed here in our automated test, if we open the developer tools,

72
00:06:06,750 --> 00:06:13,710
we see that it didn't find the validate input function here. So since that was missing or is missing,

73
00:06:14,640 --> 00:06:21,120
we have to check our util.js file and see what's wrong with validate input in here I'm using validate

74
00:06:21,120 --> 00:06:24,620
input here and I'm defining it as a function here

75
00:06:24,700 --> 00:06:28,440
but one thing I never did is I never recompiled my code,

76
00:06:28,480 --> 00:06:34,000
that's something we have to do because the code that gets imported in the browser is packed with webpack

77
00:06:34,390 --> 00:06:37,430
and I never recompiled it after changing my code.

78
00:06:37,510 --> 00:06:43,370
So in a separate terminal window, I'll quickly run npm start to get webpack to run my code and

79
00:06:43,460 --> 00:06:46,090
It'll now also watch my code and recompile if I change something

80
00:06:49,180 --> 00:06:54,860
and with that, it should have executed your test automatically, in case it didn't, simply add a blank and

81
00:06:55,070 --> 00:07:02,040
reopen your browser and enter and it will reopen the browser and execute this

82
00:07:02,060 --> 00:07:07,640
and now you see this works and this puppeteer being used for automated testing with a graphical user

83
00:07:07,640 --> 00:07:08,660
interface.

84
00:07:08,660 --> 00:07:13,760
Now obviously, we can automate this a bit more and check the result programmatically now to see if we

85
00:07:13,760 --> 00:07:18,460
got to the expected outcome because what I can now do is I can also await 

86
00:07:18,530 --> 00:07:22,550
and now that everything executed I can check if something is the case,

87
00:07:22,550 --> 00:07:29,000
for example I can check for the existence of this list item with a class of user item being added to

88
00:07:29,000 --> 00:07:39,560
it. So in my test, I can await my page and there, I can use $eval to get access to an element and

89
00:07:39,560 --> 00:07:48,700
I will look for the first element with a class of user item and I pass a function as a second argument

90
00:07:48,700 --> 00:07:55,140
to eval where I then define what I want to do with the element I selected there and here I will

91
00:07:55,140 --> 00:08:00,750
get an element as an input and I will for example return the text content

92
00:08:00,750 --> 00:08:05,850
and this means that now the result of this function will be returned by this overall promise,

93
00:08:05,910 --> 00:08:07,960
so I can store it here.

94
00:08:07,980 --> 00:08:14,760
Final text for example will be the text that I fetch from this element that was created and now I can

95
00:08:14,760 --> 00:08:22,620
expect final text to be Anna, 28 years old.

96
00:08:22,620 --> 00:08:27,120
Now it's again checking for that text but it's now using the full flow in the browser,

97
00:08:27,150 --> 00:08:30,550
it's not just a unit or integration test as we have it up there,

98
00:08:30,660 --> 00:08:33,990
it's really going through all the steps we would execute manually as well

99
00:08:34,320 --> 00:08:37,560
and then of course we should have that text but we could check anything here.

100
00:08:37,560 --> 00:08:43,980
We could also check for the existence of this class and we implicitly do so because if that class would

101
00:08:43,980 --> 00:08:48,810
not be there, we couldn't select the element by it and therefore for final text would certainly not be

102
00:08:48,810 --> 00:09:00,300
that text but now if I save this, it executes all our steps here and it actually fails here because it basically

103
00:09:00,300 --> 00:09:01,170
timed out and

104
00:09:01,290 --> 00:09:08,510
that's just a limit set up by jest to ensure that our tests don't take too long. What we can do for that

105
00:09:08,580 --> 00:09:15,100
is the test function actually takes a third argument and that is the maximum timeout and we can set

106
00:09:15,100 --> 00:09:21,330
this to 10 seconds to 10000 to increase the time we give our browser to complete its operation

107
00:09:21,600 --> 00:09:23,130
so that it doesn't timeout

108
00:09:23,190 --> 00:09:28,920
and now you see it finishes successfully because should click around, to which we should assign a better

109
00:09:28,920 --> 00:09:32,180
name, does give us the result we want

110
00:09:32,340 --> 00:09:46,460
and here I will now say should create an element with text and correct class, something like this.

111
00:09:46,580 --> 00:09:52,610
By the way we can of course now also set headless to true, disable the slow motion and not set these

112
00:09:52,670 --> 00:09:53,670
arguments

113
00:09:53,990 --> 00:09:59,690
and this will now speed up the testing because now it doesn't have to go through that step in a real

114
00:09:59,690 --> 00:10:01,740
browser, it does so behind the scenes

115
00:10:01,790 --> 00:10:06,530
and that's one of the advantages of having a headless browser, that you don't have to watch it execute

116
00:10:06,530 --> 00:10:09,480
the steps but it can be nice to see the steps, you can do both

117
00:10:10,640 --> 00:10:14,510
and now we got a full e-to-e test or a user interface test in place too.
