Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nhatthuyld/a38c6a5089874608aa3b1559277786dc to your computer and use it in GitHub Desktop.
Save nhatthuyld/a38c6a5089874608aa3b1559277786dc to your computer and use it in GitHub Desktop.
package com.examples.seleniumrc;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.server.handler.FindElement;
import org.openqa.selenium.remote.server.handler.GetElementAttribute;
public class KyotokimonoRentalTest {
WebDriver driver;
int num_person;
WebElement DE;
String xpath_Date_Element, indr_date, indc_date;
int indr_first_selected_date, indr_last_seleted_date, indc_first_selected_date, indc_last_seleted_date;
String name_customer, email_customer, phone_customer, address_customer, birth_customer;
int price_dress;
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "D:\\DEV\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://kyotokimono-rental.com/reserve");
Thread.sleep(4000);
}
@After
public void tearDown() throws Exception {
// driver.quit();
}
public boolean AssertDate() throws InterruptedException {
try {
Alert alertDate = driver.switchTo().alert();
alertDate.accept();
Thread.sleep(400);
return true;
} catch (NoAlertPresentException Ex) {
Thread.sleep(400);
return false;
}
}
@Test
public void fisrtSelectBox() throws InterruptedException {
System.out.println("****************************************************************************");
num_person = 8;
// select people
SeLectNumberPerson(num_person);
// click next button
ClickButton(".//*[@id='formAddPlan']/div[1]/div/ul/li[1]/div[3]/div[2]/div[1]/a");
Thread.sleep(2000);
// get price of dress
price_dress = Integer
.parseInt(Get_Attribute_Element(".//*[@id='plans_1_persons_0']/div/div[3]/p/span", "data-value"));
// click place and date
// if (!SeLectShopAndDate("kamakura"))
// return;
// if(!SeLectShopAndDate("kinkakuji")) return;
// if (!SeLectShopAndDate("shinkyogoku"))return;
// if(!SeLectShopAndDate("gionshijo")) return;
// if (!SeLectShopAndDate("kyoto"))return;
// if (!SeLectShopAndDate("tokyo"))return;
// if(!SeLectShopAndDate("kiyomizuzaka")) return;
if (!SeLectShopAndDate("osaka"))
return;
Thread.sleep(1000);
ChooseOption(num_person);
if (!Check_Price_OptionTable(num_person))
return;
if (!checkPricePhotoTable())
return;
// int k = GetSelectedCellDateTable();
// System.out.println("so date=" + k);
// System.out.println("chi so dau =" + indr_first_selected_date + ","
// + indc_first_selected_date);
// System.out.println("chi so cot cuoi =" + indr_last_seleted_date + ","
// + indc_last_seleted_date);
// int row =
// GetRowTableDate(".//*[@id='choose-date']/div[2]/div/table/tbody/tr");
// System.out.println(row);
if (!CheckMessageDateTable())
System.out.println("Message date is wrong");
else
System.out.println("Message date is right");
System.out.println("***********************************************************************************");
inputCustomerInfomation();
// click to complete option
ClickButton(".//*[@id='booking_confirm']");
}
public Boolean ClickButton(String idButton) throws InterruptedException {
try {
WebElement itemElement = driver.findElement(By.xpath(idButton));
itemElement.click();
return true;
} catch (Exception ex) {
System.out.println("Not found " + idButton);
return false;
}
}
public WebElement findXpath(String idButton) throws InterruptedException {
try {
WebElement itemElement = driver.findElement(By.xpath(idButton));
Thread.sleep(100);
return itemElement;
} catch (Exception ex) {
System.out.println("Not found " + idButton);
return null;
}
}
// this function is only to use verify price option , not for date table
// because it is only have data-checked attribute
public Boolean Verify_Checked(String idButton) throws InterruptedException {
String str = Get_Attribute_Element(idButton, "data-checked");
// System.out.println(str);
if (str == null) {
// System.out.println("Checkbox is NOT slected"+idButton);
return false;
}
if ("1".equals(str)) {
// System.out.println("Checkbox is selected");
return true;
}
return false;
}
// select a number person with argument
public void SeLectNumberPerson(int number) throws InterruptedException {
WebElement checkBoxElement = driver.findElement(By.id("list_plans_1SelectBoxItContainer"));
checkBoxElement.click();
Thread.sleep(200);
number++;
ClickButton(".//*[@id='list_plans_1SelectBoxItOptions']/li[" + Integer.toString(number) + "]/a/span[2]");
Thread.sleep(500);
}
// get attribute by create a element and find xpath
public String Get_Attribute_Element(String s, String attb) throws InterruptedException {
WebElement e = driver.findElement(By.xpath(s));
String result = e.getAttribute(attb);
return result;
}
// click date in present table
public Boolean ChooseRanDomDateOneTable(Integer row) throws InterruptedException {
String DateText, parentClass, DateString;
for (int j = 1; j <= 7; j++) {
for (int i = 1; i <= row; i++) {
indr_date = Integer.toString(i);
indc_date = Integer.toString(j);
// find class name of weekdays
parentClass = Get_Attribute_Element(
".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + indr_date + "]", "class");
// System.out.println("tên class day:" + parentClass);
// compare the class name of current DE element is like 'thead'
if (parentClass.equals("thead")) {
continue;// if it is that continue with i++;
}
// if not, get a DE element
DateString = ".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + indr_date + "]/td[" + indc_date
+ "]/div/div";
DE = driver.findElement(By.xpath(DateString));
DateText = DE.getText();
DE.click();
if (AssertDate() || DateText.equals("-") || DateText.equals("×") || DateText.equals("☎")) {
// nothing
} else {
Thread.sleep(200);
return true;
}
}
}
return false;
}
// Click another week if have not select a date whole table
public void ChooseRanDomDate(Integer row) throws InterruptedException {
while (!ChooseRanDomDateOneTable(row)) {
ClickButton(".//*[@id='page-next']");
Thread.sleep(2000);
ChooseRanDomDateOneTable(row);
}
}
// select shop name based on xpath with represent shop name
// in case not found shop, the date is not selected
public Boolean SeLectShop(String shopname) throws InterruptedException {
if (shopname.equals("kyoto")) {
if (ClickButton(".//*[@id='choose-shop']/li[1]/label[@for='id-5-down']"))
return true;
} else if (shopname.equals("gionshijo")) {
if (ClickButton(".//*[@id='choose-shop']/li[2]/label[@for='id-6-down']"))
return true;
} else if (shopname.equals("shinkyogoku")) {
if (ClickButton(".//*[@id='choose-shop']/li[1]/label[@for='id-1-down']"))
return true;
} else if (shopname.equals("kiyomizuzaka")) {
if (ClickButton(".//*[@id='choose-shop']/li[2]/label[@for='id-2-down']"))
return true;
} else if (shopname.equals("kinkakuji")) {
if (ClickButton(".//*[@id='choose-shop']/li[3]/label[@for='id-4-down']"))
return true;
} else if (shopname.equals("osaka")) {
if (ClickButton(".//*[@id='choose-shop']/li[1]/label[@for='id-7-down']"))
return true;
} else if (shopname.equals("tokyo")) {
if (ClickButton(".//*[@id='choose-shop']/li[1]/label[@for='id-8-down']"))
return true;
} else if (shopname.equals("kamakura")) {
if (ClickButton(".//*[@id='choose-shop']/li[2]/label[@for='id-9-down']"))
return true;
}
return false;
}
// select shop name to choose row of date table
public Boolean SeLectShopAndDate(String shopname) throws InterruptedException {
if (!SeLectShop(shopname))
return false;
Thread.sleep(3000);
if (shopname.equals("kyoto") || shopname.equals("gionshijo")) {
ChooseRanDomDate(20);
return true;
} else if (shopname.equals("shinkyogoku") || shopname.equals("kiyomizuzaka") || shopname.equals("tokyo")
|| shopname.equals("kamakura")) {
ChooseRanDomDate(18);
return true;
} else if (shopname.equals("kinkakuji")) {
ChooseRanDomDate(16);
return true;
} else if (shopname.equals("osaka")) {
ChooseRanDomDate(21);
return true;
}
return false;
}
// tam thoi lam cai ham nay sau nay doc file excel nhieu truong hop hon
// kyoto voi osaka co them 1 lua chon delivery, con shop cuoi cung kamakura
// chi co cai photo nho xi
public void ChooseOption(int numberperson) throws InterruptedException {
for (int i = 0; i < numberperson; i++) {
ClickButton(".//*[@id='plans_1_persons_" + Integer.toString(i) + "']/div/div[1]/div[2]/p[1]/label");
Thread.sleep(200);
ClickButton(".//*[@id='plans_1_persons_" + Integer.toString(i)
+ "']/div/div[2]/div/table/tbody/tr/td[3]/ul/li[3]/label");
Thread.sleep(200);
ClickButton(".//*[@id='plans_1_persons_" + Integer.toString(i)
+ "']/div/div[2]/div/table/tbody/tr/td[3]/ul/li[5]/label");
Thread.sleep(200);
}
// choose photo book
ClickButton(".//*[@id='book_options[58]SelectBoxItText']");
Thread.sleep(200);
ClickButton(".//*[@id='book_options[58]SelectBoxItOptions']/li[6]/a");
Thread.sleep(200);
}
public int GetPrice_TableOption(String pricetext) throws InterruptedException {
WebElement a = driver.findElement(By.xpath(pricetext));
int pr = Integer.parseInt(a.getAttribute("data-value"));
return pr;
}
public int Get_Total_Price_TableOption(String s, int beg, int end) throws InterruptedException {
String b, linkOp;
int sum = 0, j;
for (j = beg; j <= end; j++) {
b = Integer.toString(j);
linkOp = s + b + "]/input";
if (Verify_Checked(linkOp)) {
sum = sum + GetPrice_TableOption(linkOp);
}
}
return sum;
}
public Boolean Check_Price_OptionTable_person(int Locationperson) throws InterruptedException {
String a, b, SumElement;
int sum = 0, SumTotal;
a = Integer.toString(Locationperson);
sum = sum
+ Get_Total_Price_TableOption(
".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[3]/ul/li[", 3, 5)
+ Get_Total_Price_TableOption(
".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[5]/ul/li[", 3, 5)
+ Get_Total_Price_TableOption(
".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/ul[1]/li[", 2, 3)
+ Get_Total_Price_TableOption(
".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/ul[2]/li[", 2, 10);
SumTotal = GetPrice_TableOption(
".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/div/span");
if (sum == SumTotal) {
return true;
} else {
System.out.println("Price of Option table of person " + a + " is NOT exactly");
return false;
}
}
public Boolean Check_Price_OptionTable(int numberperson) throws InterruptedException {
for (int i = 0; i < numberperson; i++) {
if (!Check_Price_OptionTable_person(i)) {
return false;
}
}
return true;
}
public int getPricePhoto(String s, int beg, int end) throws InterruptedException {
int sum = 0;
WebElement e;
for (int i = beg; i <= end; i++) {
e = driver.findElement(By.xpath(s + Integer.toString(i) + "]/select"));
sum = sum + (Integer.parseInt(e.getAttribute("data-last-val"))
* Integer.parseInt(e.getAttribute("data-value")));
Thread.sleep(200);
}
return sum;
}
public Boolean checkPricePhotoTable() throws InterruptedException {
int sum_photo = 0, sum;
sum_photo = sum_photo
+ getPricePhoto(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[1]/ul/li[", 2, 2)
+ getPricePhoto(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[3]/ul/li[", 3, 5)
+ getPricePhoto(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[5]/ul/li[", 2, 4)
+ getPricePhoto(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[7]/ul/li[", 2, 6);
sum = Integer.parseInt(
driver.findElement(By.xpath(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[7]/div/span"))
.getAttribute("data-value"));
if (sum == sum_photo) {
return true;
} else {
System.out.println("Price of Photos table of customer is NOT exactly");
return false;
}
}
public int GetRowTableDate(String s) throws InterruptedException {
List<WebElement> rows = driver.findElements(By.xpath(s));
int r = rows.size() - 1;// because it add a last row that is finished
// head tag
return r;
}
public Boolean CheckMessageEarlyPriceDate(int price_early, String text_message) {
int price_message;
text_message = text_message.replace("(早朝料金:", "");
text_message = text_message.replace("¥", "");
text_message = text_message.replace(",", "");
text_message = text_message.replace(")", "");
text_message = text_message.trim();
price_message = Integer.parseInt(text_message);
if (price_message == price_early)
return true;
return false;
}
public Boolean CheckMessageDiscountPriceDate(int price_Discount, String text_message) {
int price_message;
text_message = text_message.replace("(夕方割引 :", "");
text_message = text_message.replace("¥", "");
text_message = text_message.replace(",", "");
text_message = text_message.replace(")", "");
text_message = text_message.trim();
price_message = Integer.parseInt(text_message);
if (price_message == price_Discount)
return true;
return false;
}
// get all selected cell of date table
public int GetSelectedCellDateTable() throws InterruptedException {
int count_cell = 0, i, j, stop = 0;
int row = GetRowTableDate(".//*[@id='choose-date']/div[2]/div/table/tbody/tr");
String a, b, attclass, parentClass;
WebElement ele;
// get the last select date of table
for (j = 1; j <= 7; j++) {
for (i = 1; i <= row && stop <= row - indr_first_selected_date + 1; i++) {
a = Integer.toString(i);
b = Integer.toString(j);
parentClass = Get_Attribute_Element(".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + a + "]",
"class");
// compare the class name of current DE element is like 'thead'
if (parentClass.equals("thead")) {
continue;
} // if it is that continue with i++;
ele = driver.findElement(
By.xpath(".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + a + "]/td[" + b + "]/div"));
attclass = ele.getAttribute("class");
if (attclass.equals("hour selected")) {
count_cell++;
stop++;
if (count_cell == 1) {
indr_first_selected_date = i;
indc_first_selected_date = j;
}
indr_last_seleted_date = i;
indc_last_seleted_date = j;
}
}
}
return count_cell;
}
// xpath cell with xpath 'tr[x]'
public int GetPriceDate(String xpath_cell) throws InterruptedException {
String parent_class = Get_Attribute_Element(xpath_cell, "class");
if (parent_class.contains("discount")) {
return -300;
} else {
if (parent_class.contains("new even-early")) {
return 500;
}
}
return 0;
}
// xpath cell with xpath to 'tr[x]'
public Boolean CheckOneMessageDateTable(String xpath_cell, String text_message, String text_price_message,
int num_cell, int pr) throws InterruptedException {
String xpath_selected, getted_message, hour_cell;
int price_cell;
xpath_selected = xpath_cell + "/td[" + Integer.toString(indc_first_selected_date) + "]/div";
hour_cell = Get_Attribute_Element(xpath_selected, "data-time_hour");
if (pr > 0) {
price_cell = num_cell * pr;
if (!CheckMessageEarlyPriceDate(price_cell, text_price_message))
return false;
getted_message = hour_cell + " から" + Integer.toString(num_cell) + " 名様のお着付けを開始します" + text_price_message;
System.out.println(getted_message);
System.out.println(text_message);
if (getted_message.equals(text_message)) {
return true;
}
} else {
if (pr < 0) {
price_cell = num_cell * pr;
if (!CheckMessageDiscountPriceDate(price_cell, text_price_message))
return false;
getted_message = hour_cell + " から" + Integer.toString(num_cell) + " 名様のお着付けを開始します" + text_price_message;
System.out.println(getted_message);
System.out.println(text_message);
if (getted_message.equals(text_message)) {
return true;
}
} else {
getted_message = hour_cell + " から" + Integer.toString(num_cell) + " 名様のお着付けを開始します";
System.out.println(getted_message);
System.out.println(text_message);
if (getted_message.equals(text_message)) {
return true;
}
}
}
return false;
}
public Boolean GetCheckMessageOneDateTable(String xpath_cell, String text_message, String text_price_message,
int num, int pr, int li_message) throws InterruptedException {
text_message = (driver
.findElement(By.xpath(".//*[@id='choose-shop-and-date']/article/div/div[6]/div[2]/span[3]/ul/li" + "["
+ Integer.toString(li_message) + "]"))).getText();
pr = GetPriceDate(xpath_cell);
System.out.println("pr:=" + pr);
if (pr != 0) {
text_price_message = (driver
.findElement(By.xpath(".//*[@id='choose-shop-and-date']/article/div/div[6]/div[2]/span[3]/ul/li["
+ Integer.toString(li_message) + "]/span"))).getText();
} else {
text_price_message = "";
}
if (!CheckOneMessageDateTable(xpath_cell, text_message, text_price_message, num, pr)) {
System.out.println("Message date " + li_message + " is wrong");
return false;
}
return true;
}
public Boolean CheckMessageDateTable() throws InterruptedException {
int count_cell, num_cell, num_lost = 0, li_message = 1, pr = 0;
String xpath_cell, text_message = "", text_price_message = "";
count_cell = GetSelectedCellDateTable();
if (count_cell == 1) {
xpath_cell = ".//*[@id='choose-date']/div[2]/div/table/tbody/tr["
+ Integer.toString(indr_first_selected_date) + "]";
if (!GetCheckMessageOneDateTable(xpath_cell, text_message, text_price_message, num_person, pr,
li_message)) {
return false;
}
return true;
}
for (int i = indr_first_selected_date; i < indr_last_seleted_date; i++) {
xpath_cell = ".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + Integer.toString(i) + "]";
if (Get_Attribute_Element(xpath_cell, "class").equals("head")) {
continue;
}
if (Get_Attribute_Element(xpath_cell + "/td[" + Integer.toString(indc_first_selected_date) + "]" + "/div",
"class").equals("hour selected")) {
num_cell = Integer.parseInt(driver
.findElement(By.xpath(
xpath_cell + "/td[" + Integer.toString(indc_first_selected_date) + "]/div/div/span"))
.getText());
num_lost = num_lost + num_cell;
if (!GetCheckMessageOneDateTable(xpath_cell, text_message, text_price_message, num_cell, pr,
li_message)) {
return false;
}
li_message++;
}
}
num_lost = num_person - num_lost;
System.out.println("num lost" + num_lost);
xpath_cell = ".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + Integer.toString(indr_last_seleted_date)
+ "]";
if (!GetCheckMessageOneDateTable(xpath_cell, text_message, text_price_message, num_lost, pr, li_message)) {
return false;
}
return true;
}
public void inputCustomerInfomation() throws InterruptedException {
WebElement ele = driver.findElement(By.xpath(".//*[@id='Book_rep_name']"));
ele.sendKeys("thuy test");
ele = driver.findElement(By.xpath(".//*[@id='Book_rep_email']"));
ele.sendKeys("nhatthuyld@gmail.com");
ele = driver.findElement(By.xpath(".//*[@id='Book_rep_tel01']"));
ele.sendKeys("8898988787867676");
ele = driver.findElement(By.xpath(".//*[@id='Book_rep_postal_code']"));
ele.sendKeys("1234567890");
ele = driver.findElement(By.xpath(".//*[@id='Book_rep_addr01']"));
ele.sendKeys("1234567890");
// click to choose birthday
ClickButton(".//*[@id='select2-Book_birthday_year-container']");
ClickButton(".//*[@id='Book_birthday_year']/option[4]");
ClickButton(".//*[@id='select2-Book_birthday_month-container']");
ClickButton(".//*[@id='Book_birthday_month']/option[4]");
ClickButton(".//*[@id='select2-Book_birthday_day-container']");
ClickButton(".//*[@id='Book_birthday_day']/option[4]");
ClickButton(".//*[@id='Book_source_id']");
ClickButton(".//*[@id='Book_source_id']/option[3]");
}
public void checkTotalPriceBooking() throws InterruptedException {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment