100% found this document useful (1 vote)
6K views

Mini Project Cucumber-Selenium Handson

This document contains code for automated testing of a hotel booking application. It includes code to: 1) Navigate to the login and booking pages, enter valid and invalid credentials, and validate error messages. 2) Attempt to book a hotel with missing/invalid required fields like name, phone number, city, etc. and validate error alerts. 3) Enter valid booking details and check that the booking is successfully completed.

Uploaded by

Souvik Banerjee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
6K views

Mini Project Cucumber-Selenium Handson

This document contains code for automated testing of a hotel booking application. It includes code to: 1) Navigate to the login and booking pages, enter valid and invalid credentials, and validate error messages. 2) Attempt to book a hotel with missing/invalid required fields like name, phone number, city, etc. and validate error alerts. 3) Enter valid booking details and check that the booking is successfully completed.

Uploaded by

Souvik Banerjee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

@Given("^I am on the login page$")

public void I_am_on_the_login_page() throws


Throwable{

hooks.driver.get("file:///./projects/challenge/web/log
in.html");

hooks.driver.manage().window().maximize();

@When("^I check page title$")


public void I_check_page_title() throws Throwable{

String actualTitle=
hooks.driver.findElement(By.xpath("//h1[contains(text(
),'Hotel Booking Application')]")).getText();

if(actualTitle.contains("Hotel Booking
Application")){

System.out.println(actualTitle);
System.out.print("Title matched successfully");

}
}
@When("^I enter username only$")
public void I_enter_username_only() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@name='user
Name']")).sendKeys("Dips");

}
@When("^I click on Login button1$")
public void I_click_on_Login_button1() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@value='Log
in']")).click();

@When("^I check for error message$")


public void I_check_for_error_message() throws
Throwable{

String error=
hooks.driver.findElement(By.xpath("//div[@id='pwdErrMs
g']")).getText();
String actualError = "* Please enter
password.";

if(error.equalsIgnoreCase(actualError)){

System.out.println(actualError);
System.out.println("Error Message is
validated");
}

}
@When("^I enter wrong password$")
public void I_enter_wrong_password() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@name='user
Pwd']")).sendKeys("check");

@When("^I click on Login button2$")


public void I_click_on_Login_button2() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@value='Log
in']")).click();

@When("^I check for alert$")


public void I_check_for_alert() throws Throwable{

String alertMsg
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter correct details$")
public void I_enter_correct_details() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@name='user
Name']")).clear();

hooks.driver.findElement(By.xpath("//input[@name='user
Name']")).sendKeys("admin");

hooks.driver.findElement(By.xpath("//input[@name='user
Pwd']")).clear();
hooks.driver.findElement(By.xpath("//input[@name='user
Pwd']")).sendKeys("admin");

}
@When("^I click on Login Button3$")
public void I_click_on_Login_Button3() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@value='Log
in']")).click();

@Then("^I expect to go to next page$")


public void I_expect_to_go_to_next_page() throws
Throwable{

String newTitle =
hooks.driver.findElement(By.xpath("//h2[text()='Hotel
Booking Form']")).getText();

System.out.println(newTitle);

@Given("^I am on the booking page$")


public void I_am_on_the_booking_page() throws
Throwable{

hooks.driver.get("file:///./projects/challenge/web/hot
elbooking.html");

hooks.driver.manage().window().maximize();

@When("^I check the page title$")


public void I_check_the_page_title() throws
Throwable{

System.out.println(hooks.driver.getTitle());

}
@When("^I click confirm booking button$")
public void I_click_confirm_booking_button()
throws Throwable{

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check for alert for first name$")
public void I_check_for_alert_for_first_name()
throws Throwable{

String alertMsg1
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg1);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter first name and click confirm
booking$")
public void
I_enter_first_name_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("(//input[@name='txt
FN'])[1]")).sendKeys("Puja");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check for alert for last name$")
public void I_check_for_alert_for_last_name()
throws Throwable{

String alertMsg2
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg2);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter last name and click confirm
booking$")
public void
I_enter_last_name_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtLas
tName']")).sendKeys("Kumari");
hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check for alert for email$")
public void I_check_for_alert_for_email() throws
Throwable{

String alertMsg3
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg3);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter email and click confirm booking$")
public void
I_enter_email_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtEma
il']")).sendKeys("[email protected]");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check for alert for mobile number$")
public void I_check_for_alert_for_mobile_number()
throws Throwable{
String alertMsg4
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg4);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter invalid mobile no and click
confirm booking$")
public void
I_enter_invalid_mobile_no_and_click_confirm_booking()
throws Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtPho
ne']")).sendKeys("901451234");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check for alert for contact no$")
public void I_check_for_alert_for_contact_no()
throws Throwable{
String alertMsg5
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg5);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter contact no and click confirm
booking$")
public void
I_enter_contact_no_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtPho
ne']")).clear();

hooks.driver.findElement(By.xpath("//input[@id='txtPho
ne']")).sendKeys("9014512345");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check alert for city$")
public void I_check_alert_for_city() throws
Throwable{

String alertMsg6
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg6);
hooks.driver.switchTo().alert().accept();

}
@When("^I select city and click confirm booking$")
public void
I_select_city_and_click_confirm_booking() throws
Throwable{

Select dropCity = new


Select(hooks.driver.findElement(By.xpath("//select[@na
me='city']")));

dropCity.selectByVisibleText("Pune");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check alert for state$")
public void I_check_alert_for_state() throws
Throwable{
String alertMsg7
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg7);
hooks.driver.switchTo().alert().accept();

}
@When("^I select state and click confirm
booking$")
public void
I_select_state_and_click_confirm_booking() throws
Throwable{

Select dropState = new


Select(hooks.driver.findElement(By.xpath("//select[@na
me='state']")));

dropState.selectByVisibleText("Maharashtra");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check alert for cardholder name$")
public void I_check_alert_for_cardholder_name()
throws Throwable{

String alertMsg8
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg8);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter cardholder name and click confirm
booking$")
public void
I_enter_cardholder_name_and_click_confirm_booking()
throws Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtCar
dholderName']")).sendKeys("puja");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check for alert for debit card number$")
public void
I_check_for_alert_for_debit_card_number() throws
Throwable{

String alertMsg9
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg9);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter card details and click confirm
booking$")
public void
I_enter_card_details_and_click_confirm_booking()
throws Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtDeb
it']")).sendKeys("9014512345123456");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check alert for cvv")
public void I_check_alert_for_cvv() throws
Throwable{

String alertMsg10
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg10);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter cvv and click confirm booking")
public void
I_enter_cvv_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtCvv
']")).sendKeys("123");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check alert for exp month")
public void I_check_alert_for_exp_month() throws
Throwable{

String alertMsg11
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg11);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter exp month and click confirm
booking")
public void
I_enter_exp_month_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtMon
th']")).sendKeys("11");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@When("^I check alert for exp year")
public void I_check_alert_for_exp_year() throws
Throwable{

String alertMsg12
=hooks.driver.switchTo().alert().getText();
System.out.print(alertMsg12);
hooks.driver.switchTo().alert().accept();

}
@When("^I enter exp year and click confirm
booking")
public void
I_enter_exp_year_and_click_confirm_booking() throws
Throwable{

hooks.driver.findElement(By.xpath("//input[@id='txtYea
r']")).sendKeys("2024");

hooks.driver.findElement(By.xpath("//input[@id='btnPay
ment']")).click();

}
@Then("^I should go to success page$")
public void I_should_go_to_success_page() throws
Throwable{

System.out.print(hooks.driver.getTitle());

hooks.driver.close();

@Given("^I am on Success page$")


public void I_am_on_Success_page() throws
Throwable{

hooks.driver.get("file:///./projects/challenge/web/suc
cess.html");

hooks.driver.manage().window().maximize();
}

@When("^i check page title$")


public void i_check_page_title() throws Throwable{

System.out.println(hooks.driver.getTitle());

}
@Then("^close the browser")
public void close_the_browser() throws Throwable{

hooks.driver.close();

You might also like