Skip to content

Instantly share code, notes, and snippets.

@nhatthuyld
Created July 20, 2018 08:41
Show Gist options
  • Save nhatthuyld/45807a065e2c52860a8da23987dfe4bb to your computer and use it in GitHub Desktop.
Save nhatthuyld/45807a065e2c52860a8da23987dfe4bb to your computer and use it in GitHub Desktop.
package Synova.askwatch;
import java.util.List;
import junit.framework.Assert;
import org.openqa.selenium.WebElement;
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.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AskMeWatch {
//Declare a static driver variable
private WebDriver driver;
@Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "D://chromedriver.exe");
// WebDriver driver = new ChromeDriver();
this.driver = new ChromeDriver();
this.driver.manage().window().maximize();
this.driver.get("https://askme.watch");
Thread.sleep(5000);
}
// @Test
public void watchAdvisor() throws InterruptedException {
//click watch advisor tab
clickButtonXpath("//*[@id='myNavbar']/ul[1]/li[1]/a");
Thread.sleep(4000);
// step 1
clickButtonXpath("//div[@value='1']");
clickButtonCss("#next-ans");
//step 2
clickButtonXpath(".//*[@id='quizarea']/div/div/div[2]/div/div[2]/div/div/div[1]/div");
clickButtonCss("#next-ans");
//step 3
clickButtonXpath(".//*[@id='quizarea']/div/div/div[2]/div/div[3]/div/div/div[3]/div");
clickButtonCss("#next-ans");
//step 4
clickButtonXpath(".//*[@id='quizarea']/div/div/div[2]/div/div[4]/div/div/div[1]/div");
clickButtonCss("#next-ans");
//step 5
clickButtonXpath(".//*[@id='quizarea']/div/div/div[2]/div/div[5]/div/div/div[3]/div");
clickButtonCss("#next-ans");
//step 5
clickButtonCss("#next-ans");
//list results
List<WebElement> listWatchAdvisor = driver.findElements(By.cssSelector(".watch-result div"));
int r = listWatchAdvisor.size();
if (r <= 0)
{
System.out.println("No results for watch advisor");
return;
}
else
{
//click first element
listWatchAdvisor.get(0).click();
}
//get url
Thread.sleep(2000);
//check URL consist of 'review-specifications-quote'
checkConsistOFQuoteString();
}
//@Test
public void priceFinder() throws InterruptedException {
//click price finder tab
clickButtonXpath(".//*[@id='myNavbar']/ul[1]/li[2]/a");
//searching
findXpath(".//*[@id='search-watch']").sendKeys("rolex");
clickButtonXpath(".//*[@id='button-search']");
Thread.sleep(5000);
//paging
WebElement nextbt = findCss("a[rel=\"next\"]");
while(nextbt != null) {
clickButtonCss("a[rel=\"next\"]");
Thread.sleep(2000);
nextbt = findCss("a[rel=\"next\"]");
}
//click view detail
clickButtonXpath(".//*[@id='watches']/div[1]/div");
}
// @Test
public void watchFinder() throws InterruptedException {
//click price finder tab
clickButtonXpath(".//*[@id='myNavbar']/ul[1]/li[3]/a");
//searching
findXpath(".//*[@id='search-watch']").sendKeys("rolex");
clickButtonXpath(".//*[@id='button-search']");
Thread.sleep(5000);
//paging
WebElement nextbt = findCss("a[rel=\"next\"]");
while(nextbt != null) {
clickButtonCss("a[rel=\"next\"]");
Thread.sleep(2000);
nextbt = findCss("a[rel=\"next\"]");
}
//click view detail
clickButtonXpath(".//*[@id='watches']/div[1]/div");
Thread.sleep(2000);
//check URL consist of 'review-specifications-quote'
checkConsistOFQuoteString();
}
// @Test
public void comPare() throws InterruptedException {
//add product to compare
driver.get("https://askme.watch/watch/detail/1");
Thread.sleep(5000);
//click compare bt
clickButtonXpath(".//*[@id='compare-prc']");
Thread.sleep(3000);
//accept pop up
assertMessage();
Thread.sleep(5000);
driver.get("https://askme.watch/watch/detail/2");
Thread.sleep(10000);
//click compare bt
clickButtonXpath(".//*[@id='compare-prc']");
Thread.sleep(3000);
//accept pop up
assertMessage();
//click watch to compare
clickButtonXpath("html/body/div[2]/div[4]/p");
clickButtonXpath(".//*[@id='compare']");
Thread.sleep(3000);
}
// @Test
public void logInFaceBook() throws InterruptedException {
driver.get("https://askme.watch/login");
//log in wtih FB
Thread.sleep(2000);
clickButtonCss(".face");
Thread.sleep(3000);
//input email&pass
findCss("#email").sendKeys("nhatthuysynova@gmail.com");
findCss("#pass").sendKeys("Cud0123456789");
clickButtonCss("#loginbutton");
}
// @Test
public void logInGmail() throws InterruptedException {
driver.get("https://askme.watch/login");
//log in with gmail
Thread.sleep(2000);
clickButtonCss(".google");
Thread.sleep(2000);
//input email & pass
findCss("#identifierId").sendKeys("nhatthuysynova");
clickButtonCss("#identifierNext > content");
Thread.sleep(2000);
findCss(".whsOnd.zHQkBf").sendKeys("Cud0123456789");
clickButtonCss("#passwordNext > content");
}
// @Test
public void addToMyWishlist() throws InterruptedException {
logInFaceBook();
//add product to my wishlist
driver.get("https://askme.watch/watch/detail/33?Search=A.-LANGE-&-SOHNE-LANGE-1-LANGE-1-38,5MM-191021-31000-review-specifications-quote");
Thread.sleep(2000);
clickButtonCss("#wishlist");
driver.get("https://askme.watch/watch/detail/1?Search=A.-LANGE-&-SOHNE-LANGE-1-LANGE-1-38,5MM-101021-31000-review-specifications-quote");
Thread.sleep(2000);
clickButtonCss("#wishlist");
//open my wishlist
driver.get("https://askme.watch/usercp/wishlist");
Thread.sleep(2000);
clickButtonCss(".btn-link");
//share on facebook-<bug
findCss("[name='xhpc_message_text']").sendKeys("my watch");
clickButtonCss("#u_0_1w");
}
// @Test
public void addToGallery() throws InterruptedException {
//open gallery page
driver.get("https://askme.watch/gallery-finder");
Thread.sleep(2000);
//pending , gallery not working
}
public Boolean clickButtonXpath(String idButton) {
try {
WebElement itemElement = this.driver.findElement(By.xpath((idButton)));
itemElement.click();
Thread.sleep(200);
return true;
} catch (Exception ex) {
System.out.println("Not found for click button:" + idButton);
System.exit(0);
return false;
}
}
public Boolean clickButtonCss(String idButton) {
try {
WebElement itemElement = this.driver.findElement(By.cssSelector((idButton)));
itemElement.click();
Thread.sleep(200);
return true;
} catch (Exception ex) {
System.out.println("Not found for click button:" + idButton);
return false;
}
}
public WebElement findXpath(String idButton) throws InterruptedException {
try {
WebElement itemElement = this.driver.findElement(By.xpath(idButton));
return itemElement;
} catch (Exception ex) {
System.out.println("Not found for find button:" + idButton);
return null;
}
}
public WebElement findCss(String idButton) throws InterruptedException {
try {
WebElement itemElement = this.driver.findElement(By.cssSelector(idButton));
return itemElement;
} catch (Exception ex) {
System.out.println("Not found for find button:" + idButton);
System.exit(0);
return null;
}
}
public void waitForElementAppearCSS(String s) {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(s)));
}
public void waitForElementAppearXpath(String s) {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(s)));
}
public Boolean equalTwoString(String s1,String s2) {
if(s1.equals(s2))
{
return true;
}
else
{
System.out.println("String do NOT equal:"+s1+"\n"+s2);
return false;
}
}
public void checkConsistOFQuoteString()
{
String s = driver.getCurrentUrl().trim();
//get 'review-specification-quote' of URL
String lastCharOfUrl = s.substring(s.length()- 27,s.length());
//compare two String
if(!lastCharOfUrl.equals("review-specifications-quote"))
{
System.out.println("URL do not consist of 'review-specifications-quote'"+"\n"+lastCharOfUrl);
}
else
{
System.out.println("Url consist of 'review-specifications-quote'");
}
}
public boolean assertMessage() throws InterruptedException {
try {
Alert alertMessage = driver.switchTo().alert();
alertMessage.accept();
Thread.sleep(400);
return true;
} catch (NoAlertPresentException Ex) {
Thread.sleep(400);
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment