Hey, guys.
Welcome to day 49 of 100 Days of Code.
We're almost halfway.
And today we've got a really special project for you.
You know that feeling when you're trying to book something?
But by the time you remember to do it, it's already booked out.
I remember encountering this problem, trying to book meeting rooms, tennis courts, or the spin classes
at my gym.
Have you ever been in this situation?
The booking system always seems to open at midnight, but by this time you're already in your pajamas
dreaming about code instead of cardio.
Well, today we're going to solve that problem with selenium.
We'll practice writing our selenium code on my dream gym that I'm calling Snack and Lift.
The motto is lift weights, eat snacks.
Repeat.
Easy.
The landing page is pretty basic, but I think the design and functionality is fairly close to other
gym websites that I've used.
I've added some testimonials by other people who felt seen.
If you have any testimonial suggestions, feel free to post them in the comments section.
In terms of functionality, the main things to understand is that we're simulating the entire gym experience
locally in your browser.
When you load up the website, it sets up a database in your browser that already contains some users
and some bookings.
For example, you can use the student at Test.com email and the password of password one two, three
to login and view the bookings page.
I've set this up for you so that you can see what it looks like for an existing account.
Here you can see that we've got some classes booked and when you click this button, you can book a
class or join the waitlist for that class on the My Bookings page.
You can see all the bookings that were made, and here you can cancel a booking or leave the waitlist.
Now let's log out and take a look at the admin panel.
If you log in with the admin credentials, you get some more fine grained control over the website and
the database.
This is handy when you've run your selenium code to book gym classes, and you'd like to run the code
again after making a small change.
In this case, it's a good idea to have the database in exactly the same state before you ran your code
for the first time.
This is why I've got some information at the top to show what's in the database.
And I've got two buttons at the bottom.
If you click clear Bookings only, then you will delete all the bookings that are currently in the database.
If you click Reset All Data, then you restore the booking system to its initial state with only the
default bookings.
You'll delete all the users that you've registered to.
The stats at the top should show you that everything has been reset.
Remember how I said the database lives in your browser instead of the gym server?
You can see the database in Chrome's developer console.
If you go to the application tab, you should see that under storage there is an Indexeddb with the
gyms data.
If you open a new browser or a different user profile, then you will have a different copy of this
database.
What this means is that if you register a user or make a booking in one profile, that booking won't
show up in the other profile.
This is why, as a first step, we'll make sure that every time selenium runs, we'll use the same Chrome
profile.
So that's your profile picture icon here.
Most people just have one.
But in case you're somebody like me which like 3 or 4, then this is something you should look out for.
This way your bot will always see the same bookings and user data.
In the later part of the day, you'll be presented with a challenge to make your code work even when
some requests fail.
This is what the network simulation option is for.
This is off by default and you should only enable it at the end.
The time simulation is here to make sure your script can run as expected, even if a day passes.
You'll use these two options to QA or test your own code and make sure it works under different conditions
at the end of the day.
This project is going to teach you some seriously powerful automation skills.
We're not just clicking buttons here.
We're going to set up persistent browser profiles so our bot remembers who we are, just like a real
user.
Handle dynamic content that changes based on the day and time.
Deal with different button states.
Is the class available full?
Are we already booked on a waitlist?
Use function wrappers and higher order functions to implement a retry logic for when the network decides
to take a coffee break, and even do some quality assurance by verifying our bookings actually went
through.
These aren't just gym booking skills.
Once you've mastered these techniques, you can automate all sorts of repetitive booking tasks on the
web.
We'll start simple just logging in and booking one class, but by the end, your bot will be booking
multiple classes and handle retries even when the website has issues.
So grab your favorite snack, the gym approves, fire up PyCharm and let's build a bot that never skips
leg day or any day for that matter.