Skip to content

Instantly share code, notes, and snippets.

@nhatthuyld
Created May 31, 2016 04:39
Show Gist options
  • Save nhatthuyld/a6dbcd240b486a86d5e23eac21d14a5f to your computer and use it in GitHub Desktop.
Save nhatthuyld/a6dbcd240b486a86d5e23eac21d14a5f to your computer and use it in GitHub Desktop.
package com.examples.seleniumrc;
import java.awt.event.ItemEvent;
import java.io.NotActiveException;
import java.util.NoSuchElementException;
import javax.swing.plaf.synth.SynthStyleFactory;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.NotFoundException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByXPath;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.events.WebDriverEventListener;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.thoughtworks.selenium.webdriven.commands.Click;
import com.thoughtworks.selenium.webdriven.commands.WaitForPageToLoad;
public class KyotokimonoRentalTest {
WebDriver driver;
int num_person;
WebElement DE;
String xpath_Date_Element;
@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");
}
@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 {
num_person = 3;
// 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(1000);
// 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 (!Check_Price_Photo_Table())
// return;
}
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 Boolean Verify_Checked(String idButton) throws InterruptedException {
String str = driver.findElement(By.xpath(idButton)).getAttribute("data-checked");
// System.out.println(str);
if (str == null) {
// System.out.println("Checkbox is NOT selected"+idButton);
return false;
}
if (str.equals("1")) {
// 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);
}
// click date in present table
public Boolean ChooseRanDomDateOneTable(Integer row) throws InterruptedException {
String a, b, DateString, DateText;
for (int j = 1; j <= 7; j++) {
for (int i = 1; i <= row; i++) {
a = Integer.toString(i);
b = Integer.toString(j);
// find class name of weekdays
xpath_Date_Element = ".//*[@id='choose-date']/div[2]/div/table/tbody/tr[" + a + "]";
DE = driver.findElement(By.xpath(xpath_Date_Element));
String parentClass = DE.getAttribute("class");
// compare the class name of current DE element is like 'thead'
// class name
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[" + a + "]/td[" + b + "]/div/div";
DE = driver.findElement(By.xpath(DateString));
DateText = DE.getText();
if (DateText.equals("-") || DateText.equals("×") || DateText.equals("☎") || AssertDate()) {
// nothing
} else {
DE.click();
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"));
// System.out.println(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, linkOp, SumElement;
int sum = 0, SumTotal ,j;
a = Integer.toString(Locationperson);
/*
* for (j = 3; j <= 5; j++) {
*
* b = Integer.toString(j); linkOp = ".//*[@id='plans_1_persons_" + a +
* "']/div/div[2]/div/table/tbody/tr/td[3]/ul/li[" + b + "]/input";
*
* if (Verify_Checked(linkOp)) { sum = sum +
* GetPrice_TableOption(linkOp); } }
*/
sum=sum+Get_Total_Price_TableOption(".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[3]/ul/li[",
3, 5);
/*for (j = 3; j <= 5; j++) {
b = Integer.toString(j);
linkOp = ".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[5]/ul/li[" + b + "]/input";
if (Verify_Checked(linkOp)) {
sum = sum + GetPrice_TableOption(linkOp);
}
}*/
sum=sum+Get_Total_Price_TableOption( ".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[5]/ul/li[", 3, 5);
/*for (j = 2; j <= 3; j++) {
b = Integer.toString(j);
linkOp = ".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/ul[1]/li[" + b
+ "]/input";
if (Verify_Checked(linkOp)) {
sum = sum + GetPrice_TableOption(linkOp);
}
}*/
sum=sum+Get_Total_Price_TableOption(".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/ul[1]/li[",2, 3);
for (j = 2; j <= 10; j++) {
b = Integer.toString(j);
linkOp = ".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/ul[2]/li[" + b
+ "]/input";
if (Verify_Checked(linkOp)) {
sum = sum + GetPrice_TableOption(linkOp);
}
}
sum=sum+Get_Total_Price_TableOption(".//*[@id='plans_1_persons_" + a + "']/div/div[2]/div/table/tbody/tr/td[7]/ul[2]/li[" , 2, 10);
// price total with span tag so Not use get price function
/* SumElement = driver.findElement(By.xpath(
".//*[@id='plans_1_persons_" + a+ "']/div/div[2]/div/table/tbody/tr/td[7]/div/span"))
.getAttribute("data-value");*/
SumTotal=GetPrice_TableOption(".//*[@id='plans_1_persons_" + a+ "']/div/div[2]/div/table/tbody/tr/td[7]/div/span");
if (sum == SumTotal) {
System.out.println("Price of Option table of person " + a + " is exactly");
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 Get_Price_Photo(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 Check_Price_Photo_Table() throws InterruptedException {
int sum_photo = 0, sum;
sum_photo = sum_photo
+ Get_Price_Photo(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[1]/ul/li[", 2, 2)
+ Get_Price_Photo(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[3]/ul/li[", 3, 5)
+ Get_Price_Photo(".//*[@id='booking_form']/div[3]/div[2]/div/table/tbody/tr/td[5]/ul/li[", 2, 4)
+ Get_Price_Photo(".//*[@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) {
System.out.println("Price of Photos table of customer is exactly");
return true;
} else {
System.out.println("Price of Photos table of customer is NOT exactly");
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment