Skip to content

Instantly share code, notes, and snippets.

@nhatthuyld
Created July 20, 2018 13:38
Show Gist options
  • Save nhatthuyld/bae449c06de72b4e1aab67138d515a22 to your computer and use it in GitHub Desktop.
Save nhatthuyld/bae449c06de72b4e1aab67138d515a22 to your computer and use it in GitHub Desktop.
fix need to scroll intoView issue.
package projectname.com.example;
import java.util.List;
import junit.framework.Assert;
import org.openqa.selenium.WebElement;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
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.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
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;
@BeforeClass
public static void beforeClass() {
System.setProperty("webdriver.chrome.driver", "/Users/ducnguyen/Workspace/chromedriver");
// WebDriver driver = new ChromeDriver();
}
@Before
public void setUp() throws Exception {
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
// List<WebElement> listPaging = driver.findElements(By.cssSelector(".pagination
// li"));
// System.out.println(listPaging.size());
// for (int i = 0; i < listPaging.size() - 3; i++) {
// System.out.println("i:"+i);
// // have problem can not click
// clickButtonCss("a[rel=\"next\"]");
// Thread.sleep(5000);
// }
WebElement link = findCss("a[rel=\"next\"]");
while (link != null) {
clickButtonCss("a[rel=\"next\"]");
Thread.sleep(5000);
link = findCss("a[rel=\"next\"]");
}
// click view detail
// clickButtonXpath(".//*[@id='watches']/div[1]/div");
// driver.close();
}
// @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
List<WebElement> listPaging = driver.findElements(By.cssSelector(".row.first_step_pagination li"));
// for(int i = 0; i < listPaging.size() - 3; i++)
// {//have problem can not click
// clickButtonCss(".row.first_step_pagination .boxpaging-search li:last-child
// a");
// Thread.sleep(7000);
// }
// 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
this.driver.get("https://askme.watch/watch/detail/1");
Thread.sleep(5000);
// click compare bt //*[@id="compare-prc"]
clickButtonCss("button#compare-prc");
Thread.sleep(3000);
// accept pop up
acceptAlertMessage();
Thread.sleep(5000);
// driver.get("https://askme.watch/watch/detail/2");
this.driver.navigate().to("https://askme.watch/watch/detail/2");
Thread.sleep(5000);
WebElement compareBtnElm = findCss("button#compare-prc");
if(!clickButtonCss(compareBtnElm)) {
scrollElementIntroView(compareBtnElm, -100);
clickButtonCss(compareBtnElm);
}
Thread.sleep(5000);
// accept pop up
acceptAlertMessage();
Thread.sleep(5000);
// click watch to compare
clickButtonXpath("html/body/div[2]/div[4]/p");
clickButtonXpath(".//*[@id='compare']");
Thread.sleep(3000);
}
@After
public void name() throws InterruptedException {
Thread.sleep(30000);
this.driver.close();
}
public void scrollElementIntroView(WebElement element, int padding) {
JavascriptExecutor jse = (JavascriptExecutor) this.driver;
jse.executeScript("arguments[0].scrollIntoView(true);window.scrollBy(0,"+padding+");", element);
}
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 + ":" + ex.getMessage());
return false;
}
}
public Boolean clickButtonCss(String cssSelector) {
try {
WebElement itemElement = this.driver.findElement(By.cssSelector((cssSelector)));
itemElement.click();
Thread.sleep(200);
return true;
} catch (Exception ex) {
System.out.println("Not found element to click:" + cssSelector + ":" + ex.getMessage());
return false;
}
}
public Boolean clickButtonCss(WebElement itemElement) {
try {
itemElement.click();
Thread.sleep(200);
return true;
} catch (Exception ex) {
System.out.println("Can not click on button:" + itemElement.getAttribute("innerHTML") + ":" + ex.getMessage());
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 acceptAlertMessage() throws InterruptedException {
try {
Alert alertMessage = driver.switchTo().alert();
alertMessage.accept();
Thread.sleep(400);
driver.switchTo().defaultContent();
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