# Automate booking on a travel site

Write a selenium test that makes a booking from Bangalore to New Delhi for today's date with a return of tomorrow using makemytrip.com. Select the cheapest flight and make sure that you come to the booking page.

I'm pretty new to Selenium and I would like to know if my attempt at achieving the aforementioned task is as good as it can get or if there are certain aspects of my code that could be improved.

I've relied on some simple element selectors to achieve the task and one concern I have with automating a test case (such as the one mentioned above) for a web site is to do it in such a way that it remains change proof i.e. a test case written in such a way that it validates certain functions of the site without relying on the structure of the web page itself especially when the "id" attribute isn't available. Are there any general recommendations/practices as far as automation of web sites is concerned ?

from selenium import webdriver,common
from selenium.webdriver.common.keys import Keys
from datetime import datetime
import time,re

def type_and_enter(element,text):
element.clear()
element.send_keys(text)
time.sleep(1)
element.send_keys(Keys.ENTER)

source = "Bangalore"
destination = "New Delhi"

chrome_browser = webdriver.Chrome()
chrome_browser.get("https://www.makemytrip.com/")

# ASSERT WE ARE ON THE CORRECT PAGE
assert "makemytrip" in chrome_browser.title.lower()

# ENTER VALUE FOR "FROM"
from_field = chrome_browser.find_element_by_id("hp-widget__sfrom")
type_and_enter(from_field,source)

# ENTER VALUE FOR "TO"
to_field = chrome_browser.find_element_by_id("hp-widget__sTo")
type_and_enter(to_field,destination)

# SET DEPART DATE TO PRESENT DAY
depart_field = chrome_browser.find_element_by_id("hp-widget__depart")
depart_field.click()

• Is there a reason for the time.sleep(1)? – 200_success Oct 10 '18 at 13:51
• @200_success I guess it is to wait for the webpage to fill in the send_keys which is bad practice... better would be a webdriver.wait(...) – Ludisposed Oct 10 '18 at 14:03

Another way to abstract things away is Behavior Driven Development which would, at least, make you think of the actions and conditions on the target site pages and describe them in a natural language. There are a few Python packages for BDD, in particular - behave which can be, of course, used with selenium: Using the behave Framework for Selenium BDD Testing: A Tutorial