WEBVTT

00:00.360 --> 00:01.000
Hey, guys.

00:01.000 --> 00:04.320
Welcome to day 49 of 100 Days of Code.

00:04.920 --> 00:06.120
We're almost halfway.

00:06.120 --> 00:09.320
And today we've got a really special project for you.

00:09.800 --> 00:12.480
You know that feeling when you're trying to book something?

00:12.480 --> 00:15.880
But by the time you remember to do it, it's already booked out.

00:16.360 --> 00:21.840
I remember encountering this problem, trying to book meeting rooms, tennis courts, or the spin classes

00:21.840 --> 00:22.720
at my gym.

00:22.920 --> 00:24.880
Have you ever been in this situation?

00:25.600 --> 00:31.160
The booking system always seems to open at midnight, but by this time you're already in your pajamas

00:31.160 --> 00:33.560
dreaming about code instead of cardio.

00:34.040 --> 00:37.120
Well, today we're going to solve that problem with selenium.

00:37.360 --> 00:43.440
We'll practice writing our selenium code on my dream gym that I'm calling Snack and Lift.

00:44.040 --> 00:46.800
The motto is lift weights, eat snacks.

00:46.800 --> 00:47.600
Repeat.

00:47.720 --> 00:48.200
Easy.

00:48.640 --> 00:54.040
The landing page is pretty basic, but I think the design and functionality is fairly close to other

00:54.040 --> 00:55.640
gym websites that I've used.

00:56.040 --> 00:59.760
I've added some testimonials by other people who felt seen.

01:00.200 --> 01:05.590
If you have any testimonial suggestions, feel free to post them in the comments section.

01:06.190 --> 01:12.430
In terms of functionality, the main things to understand is that we're simulating the entire gym experience

01:12.470 --> 01:14.110
locally in your browser.

01:14.310 --> 01:19.830
When you load up the website, it sets up a database in your browser that already contains some users

01:19.830 --> 01:20.830
and some bookings.

01:21.150 --> 01:28.990
For example, you can use the student at Test.com email and the password of password one two, three

01:29.150 --> 01:31.510
to login and view the bookings page.

01:31.910 --> 01:36.350
I've set this up for you so that you can see what it looks like for an existing account.

01:36.950 --> 01:41.830
Here you can see that we've got some classes booked and when you click this button, you can book a

01:41.830 --> 01:46.990
class or join the waitlist for that class on the My Bookings page.

01:47.070 --> 01:53.070
You can see all the bookings that were made, and here you can cancel a booking or leave the waitlist.

01:53.630 --> 01:57.030
Now let's log out and take a look at the admin panel.

01:59.350 --> 02:04.470
If you log in with the admin credentials, you get some more fine grained control over the website and

02:04.470 --> 02:05.390
the database.

02:05.630 --> 02:11.060
This is handy when you've run your selenium code to book gym classes, and you'd like to run the code

02:11.060 --> 02:13.540
again after making a small change.

02:14.540 --> 02:20.420
In this case, it's a good idea to have the database in exactly the same state before you ran your code

02:20.420 --> 02:21.380
for the first time.

02:21.860 --> 02:26.020
This is why I've got some information at the top to show what's in the database.

02:26.180 --> 02:28.100
And I've got two buttons at the bottom.

02:28.100 --> 02:34.460
If you click clear Bookings only, then you will delete all the bookings that are currently in the database.

02:34.740 --> 02:42.220
If you click Reset All Data, then you restore the booking system to its initial state with only the

02:42.220 --> 02:43.260
default bookings.

02:43.300 --> 02:46.260
You'll delete all the users that you've registered to.

02:47.140 --> 02:51.140
The stats at the top should show you that everything has been reset.

02:51.860 --> 02:56.260
Remember how I said the database lives in your browser instead of the gym server?

02:56.580 --> 02:59.780
You can see the database in Chrome's developer console.

03:00.140 --> 03:06.700
If you go to the application tab, you should see that under storage there is an Indexeddb with the

03:06.700 --> 03:07.580
gyms data.

03:07.980 --> 03:14.000
If you open a new browser or a different user profile, then you will have a different copy of this

03:14.000 --> 03:14.720
database.

03:15.200 --> 03:21.560
What this means is that if you register a user or make a booking in one profile, that booking won't

03:21.560 --> 03:23.600
show up in the other profile.

03:23.840 --> 03:30.040
This is why, as a first step, we'll make sure that every time selenium runs, we'll use the same Chrome

03:30.040 --> 03:30.760
profile.

03:31.240 --> 03:33.920
So that's your profile picture icon here.

03:33.960 --> 03:35.240
Most people just have one.

03:35.240 --> 03:40.640
But in case you're somebody like me which like 3 or 4, then this is something you should look out for.

03:41.040 --> 03:45.080
This way your bot will always see the same bookings and user data.

03:45.520 --> 03:50.680
In the later part of the day, you'll be presented with a challenge to make your code work even when

03:50.680 --> 03:51.840
some requests fail.

03:52.120 --> 03:54.920
This is what the network simulation option is for.

03:55.200 --> 03:59.120
This is off by default and you should only enable it at the end.

03:59.480 --> 04:05.120
The time simulation is here to make sure your script can run as expected, even if a day passes.

04:05.280 --> 04:12.440
You'll use these two options to QA or test your own code and make sure it works under different conditions

04:12.440 --> 04:13.440
at the end of the day.

04:14.120 --> 04:18.150
This project is going to teach you some seriously powerful automation skills.

04:18.150 --> 04:19.550
We're not just clicking buttons here.

04:19.550 --> 04:25.470
We're going to set up persistent browser profiles so our bot remembers who we are, just like a real

04:25.470 --> 04:26.030
user.

04:26.590 --> 04:30.190
Handle dynamic content that changes based on the day and time.

04:30.630 --> 04:32.670
Deal with different button states.

04:32.910 --> 04:35.310
Is the class available full?

04:35.590 --> 04:37.950
Are we already booked on a waitlist?

04:38.550 --> 04:45.150
Use function wrappers and higher order functions to implement a retry logic for when the network decides

04:45.150 --> 04:51.670
to take a coffee break, and even do some quality assurance by verifying our bookings actually went

04:51.670 --> 04:52.070
through.

04:52.710 --> 04:54.870
These aren't just gym booking skills.

04:55.150 --> 04:59.990
Once you've mastered these techniques, you can automate all sorts of repetitive booking tasks on the

04:59.990 --> 05:00.350
web.

05:00.750 --> 05:06.710
We'll start simple just logging in and booking one class, but by the end, your bot will be booking

05:06.710 --> 05:11.550
multiple classes and handle retries even when the website has issues.

05:11.950 --> 05:18.150
So grab your favorite snack, the gym approves, fire up PyCharm and let's build a bot that never skips

05:18.190 --> 05:20.630
leg day or any day for that matter.
