Skip to content

Instantly share code, notes, and snippets.

@nhatthuyld
Created July 22, 2018 14:51
Show Gist options
  • Save nhatthuyld/21fcbf172a438521efff4afbf18b609d to your computer and use it in GitHub Desktop.
Save nhatthuyld/21fcbf172a438521efff4afbf18b609d to your computer and use it in GitHub Desktop.
here is code show how to switch to next tab
package projectname.com.example;
import java.util.List;
import java.util.Set;
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);
}
@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
//Focus on new Tab: https://stackoverflow.com/questions/39449524/how-to-focus-on-second-tab-and-work-on-it-using-selenium-webdriver
Thread.sleep(2000);
Set<String> handles = driver.getWindowHandles();
String currentHandle = driver.getWindowHandle();
for (String handle : handles) {
if (!handle.equals(currentHandle)) {
driver.switchTo().window(handle);
}
}
Thread.sleep(500);
findCss("[name='xhpc_message_text']").sendKeys("my watch");
clickButtonCss("#u_0_1w");
Thread.sleep(500);
driver.switchTo().window(currentHandle);
}
// @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");
}
@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 + ";" + ex.getMessage());
// 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