Given below is my task
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"
# CREATE A NEW GOOGLE CHROME OBJECT & LOGIN TO makemytrip.com
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()
time.sleep(1)
? \$\endgroup\$send_keys
which is bad practice... better would be awebdriver.wait(...)
\$\endgroup\$