1
\$\begingroup\$

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()
\$\endgroup\$
  • \$\begingroup\$ Is there a reason for the time.sleep(1)? \$\endgroup\$ – 200_success Oct 10 '18 at 13:51
  • \$\begingroup\$ @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(...) \$\endgroup\$ – Ludisposed Oct 10 '18 at 14:03
2
\$\begingroup\$

I think the biggest thing you have to take into account is Separation of Concerns. You should definitely separate page and element definitions from the actual logic of the test. Put your element locators into Page Objects and reference page object attributes in your tests. You can, for instance, have a Search page, a Search Results page, a Booking page on a high level which, in turn, may consist of inner page objects defining blocks/parts of a page.

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

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.