Java tutorial
/* * PageUtil.java * Copyright (c) 2014, CODEROAD, All Rights Reserved. * * This software is the confidential and proprietary information of CODEROAD * ("Confidential Information"). You shall not disclose such Confidential Information and shall use * it only in accordance with the terms of the license agreement you entered into with * CODEROAD. */ package com.coderoad.automation.common.util; import io.appium.java_client.TouchShortcuts; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.regex.Pattern; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.Dimension; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.Keys; import org.openqa.selenium.Point; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; 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; import org.testng.Assert; import com.coderoad.automation.common.enums.LogLevel; import com.coderoad.automation.common.enums.Timeout; import com.coderoad.automation.common.log.LogUtil; /** * The Class PageUtil. * * @author $Author:$ * @version $Rev:$ $Date:$ */ public class PageUtil { /** * Clear and set text. * * @param driver the driver * @param locator the locator * @param innerText the inner text * @param timeout the timeout */ public static void clearAndSetText(final WebDriver driver, final By locator, final String innerText, final Timeout timeout) { final WebElement element = getElement(driver, locator, timeout); Assert.assertNotNull(element, "Element cannot be null"); element.clear(); element.sendKeys(innerText); element.sendKeys(Keys.ENTER); WaitUtil.waitUntil(Timeout.ATOMIC_TIMEOUT); } /** * Clear and set text press enter. * * @param driver the driver * @param locator the locator * @param innerText the inner text * @param timeout the timeout */ public static void clearAndSetTextPressEnter(final WebDriver driver, final By locator, final String innerText, final Timeout timeout) { final WebElement element = getElement(driver, locator, timeout); element.clear(); element.sendKeys(innerText); element.sendKeys(Keys.ENTER); } /** * Clear text. * * @param driver the driver * @param locator the locator * @param timeout the timeout */ public static void clearText(final WebDriver driver, final By locator, final Timeout timeout) { final WebElement element = getElement(driver, locator, timeout); element.clear(); element.sendKeys(Keys.ENTER); } /** * Do tap. * * @param element the element * @param driver the driver */ public static void doTap(final WebElement element, final WebDriver driver) { Point point = element.getLocation(); double x = point.getX(); double y = point.getY(); Dimension dimesions = element.getSize(); int width = dimesions.width; int height = dimesions.height; final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); // tapObject.put("duration", 0.7); tapObject.put("x", x + width - 2); tapObject.put("y", y + height - 2); js.executeScript("mobile: tap", tapObject); } /** * Do left tap. * * @param element the element * @param driver the driver */ public static void doLeftTap(final WebElement element, final WebDriver driver) { LogUtil.log("Do Left Tap", LogLevel.HIGH); Point point = element.getLocation(); double x = point.getX(); double y = point.getY(); Dimension dimesions = element.getSize(); int height = dimesions.height; final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); x = x + 2; y = y + height + 2; tapObject.put("x", x); tapObject.put("y", y); LogUtil.log("X :" + x, LogLevel.HIGH); LogUtil.log("Y :" + y, LogLevel.HIGH); js.executeScript("mobile: tap", tapObject); } /** * Do middle tap. * * @param element the element * @param driver the driver */ public static void doMiddleTap(final WebElement element, final WebDriver driver) { LogUtil.log("Do Middle Tap", LogLevel.HIGH); Point point = element.getLocation(); double x = point.getX(); double y = point.getY(); Dimension dimesions = element.getSize(); int width = dimesions.width; int height = dimesions.height; final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); tapObject.put("duration", 0.1); int midX = (int) x + (width / 2); int midY = (int) y + (height / 2); LogUtil.log("midX :" + midX, LogLevel.HIGH); LogUtil.log("midY :" + midY, LogLevel.HIGH); tapObject.put("duration", 0.7); tapObject.put("x", (double) midX); tapObject.put("y", (double) midY); js.executeScript("mobile: tap", tapObject); js.executeScript("mobile: tap", tapObject); } /** * Do exact tap. * * @param element the element * @param driver the driver */ public static void doExactTap(final WebElement element, final WebDriver driver) { LogUtil.log("Do Exact Tap", LogLevel.HIGH); final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); Point point = element.getLocation(); double x = point.getX(); double y = point.getY(); tapObject.put("x", x); tapObject.put("y", y); js.executeScript("mobile: tap", tapObject); } /** * Do tap on displayed. * * @param elementList the element list * @param driver the driver */ public static void doTapOnDisplayed(final List<WebElement> elementList, final WebDriver driver) { LogUtil.log("Do Tap on displayed", LogLevel.HIGH); for (WebElement webElement : elementList) { if (webElement != null) { LogUtil.log("WebElement Not Null", LogLevel.HIGH); if (isElementClickable(driver, webElement, Timeout.TINY_TIMEOUT) != null) { LogUtil.log("Click the element found ", LogLevel.HIGH); doTap(webElement, driver); break; } } } } /** * Do tap. * * @param x the x * @param y the y * @param driver the driver */ public static void doTap(final double x, final double y, final WebDriver driver) { Map<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); tapObject.put("duration", 0.7); tapObject.put("x", x); tapObject.put("y", y); final JavascriptExecutor js = (JavascriptExecutor) driver; // LogUtil.log("Page Source : " + driver.getPageSource(), // LogLevel.HIGH); js.executeScript("mobile: tap", tapObject); } /** * Do swipe horizontal. * * @param element the element * @param driver the driver * @param length the length */ public static void doSwipeHorizontal(final WebElement element, final WebDriver driver, final int length) { Point point = element.getLocation(); double x = point.getX(); double y = point.getY(); LogUtil.log("X :" + element.getLocation().getX(), LogLevel.HIGH); LogUtil.log("Y :" + element.getLocation().getY(), LogLevel.HIGH); LogUtil.log("Length :" + length, LogLevel.HIGH); final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); tapObject.put("duration", 0.7); tapObject.put("startX", x + 5); tapObject.put("startY", y); tapObject.put("endX", x + length); tapObject.put("endY", y); js.executeScript("mobile: swipe", tapObject); } /** * Do swipe page left. * * @param element the element * @param driver the driver */ public static void doSwipePageLeft(final WebElement element, final WebDriver driver) { // final Point point = element.getLocation(); // double x = point.getX(); // double y = point.getY(); LogUtil.log("X :" + element.getLocation().getX(), LogLevel.HIGH); LogUtil.log("Y :" + element.getLocation().getY(), LogLevel.HIGH); final Dimension dimesions = element.getSize(); //int width = dimesions.width; //int height = dimesions.height; LogUtil.log("width :" + dimesions.width, LogLevel.HIGH); LogUtil.log("hieght:" + dimesions.height, LogLevel.HIGH); final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); tapObject.put("duration", 0.7); tapObject.put("startX", (double) getWindowWidth(driver)); tapObject.put("startY", (double) (getWindowHeight(driver) / 2)); tapObject.put("endX", 0.0); tapObject.put("endY", (double) (getWindowHeight(driver) / 2)); js.executeScript("mobile: swipe", tapObject); } /** * Do swipe page right. * * @param element the element * @param driver the driver */ public static void doSwipePageRight(final WebElement element, final WebDriver driver) { Point point = element.getLocation(); double x = point.getX(); double y = point.getY(); LogUtil.log("X :" + element.getLocation().getX(), LogLevel.HIGH); LogUtil.log("Y :" + element.getLocation().getY(), LogLevel.HIGH); Dimension dimesions = element.getSize(); int width = dimesions.width; // int height = dimesions.height; LogUtil.log("width :" + dimesions.width, LogLevel.HIGH); LogUtil.log("hieght:" + dimesions.height, LogLevel.HIGH); final JavascriptExecutor js = (JavascriptExecutor) driver; final HashMap<String, Double> tapObject = new HashMap<String, Double>(); tapObject.put("tapCount", 1.0); tapObject.put("touchCount", 1.0); tapObject.put("duration", 0.7); tapObject.put("startX", x + width); tapObject.put("startY", y); tapObject.put("endX", x); tapObject.put("endY", y); js.executeScript("mobile: swipe", tapObject); } /** * Gets the window width. * * @param driver the driver * @return the window width */ public static int getWindowWidth(final WebDriver driver) { final JavascriptExecutor js = (JavascriptExecutor) driver; final String javaScript = "return screen.width"; long widthValue = (long) js.executeScript(javaScript); final int width = (int) widthValue; LogUtil.log("width :" + width, LogLevel.HIGH); return width; } /** * Gets the window height. * * @param driver the driver * @return the window height */ public static int getWindowHeight(final WebDriver driver) { final JavascriptExecutor js = (JavascriptExecutor) driver; final String javaScript = "return screen.height"; long heightValue = (long) js.executeScript(javaScript); final int height = (int) heightValue; LogUtil.log("height :" + height, LogLevel.HIGH); return height; } /** * Checks if is displayed. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return true, if is displayed */ public static boolean isDisplayed(final WebDriver driver, final By locator, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.visibilityOfElementLocated(locator)).isDisplayed(); } catch (Exception ex) { return false; } } /** * Checks if is visible. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return true, if is visible */ public static boolean isVisible(final WebDriver driver, final By locator, final Timeout timeout) { LogUtil.log("Check the Element is Visible(By Locator) ", LogLevel.HIGH); try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.visibilityOf(driver.findElement(locator))).isDisplayed(); } catch (Exception ex) { return false; } } /** * Checks if is visible. * * @param driver the driver * @param element the element * @param timeout the timeout * @return true, if is visible */ public static boolean isVisible(final WebDriver driver, final WebElement element, final Timeout timeout) { LogUtil.log("Check the Element is Visible(WebElement) ", LogLevel.HIGH); try { return new WebDriverWait(driver, timeout.getValue()).until(ExpectedConditions.visibilityOf(element)) .isDisplayed(); } catch (Exception ex) { return false; } } /** * Checks if is not displayed. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return true, if is not displayed */ public static boolean isNotDisplayed(final WebDriver driver, final By locator, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.invisibilityOfElementLocated(locator)); } catch (Exception ex) { return true; } } /** * Checks if is enabled. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return true, if is enabled */ public static boolean isEnabled(final WebDriver driver, final By locator, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.presenceOfElementLocated(locator)).isEnabled(); } catch (Exception ex) { return false; } } /** * Checks if is selected. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return true, if is selected */ public static boolean isSelected(final WebDriver driver, final By locator, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.presenceOfElementLocated(locator)).isSelected(); } catch (Exception ex) { return false; } } /** * Checks if is element clickable. * * @param driver the driver * @param element the element * @param timeout the timeout * @return the web element */ public static WebElement isElementClickable(final WebDriver driver, final WebElement element, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.elementToBeClickable(element)); } catch (Exception te) { return null; } } public static boolean isWebElementClickable(final WebDriver driver, final WebElement element, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.elementToBeClickable(element)).isDisplayed(); } catch (Exception te) { return false; } } /** * Gets the element. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return the element */ public static WebElement getElement(final WebDriver driver, final By locator, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.presenceOfElementLocated(locator)); } catch (Exception ex) { return null; } } /** * Gets the first visible element. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return the first visible element */ public static WebElement getFirstVisibleElement(final WebDriver driver, final By locator, final Timeout timeout) { final List<WebElement> visibleElements = getVisibleElements(driver, locator, timeout); return visibleElements.size() > 0 ? visibleElements.get(0) : null; } /** * Gets the visible elements. * Returns an array of webElements that are visible. * If there are no visible DOM elements that match the criteria, then NULL is returned. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return the visible elements */ public static List<WebElement> getVisibleElements(final WebDriver driver, final By locator, final Timeout timeout) { final List<WebElement> visibleElements = new ArrayList<>(); try { for (final WebElement element : getElements(driver, locator, timeout)) { if (element.isDisplayed()) { visibleElements.add(element); } } } catch (Exception ex) { LogUtil.log("Element either not found or not visible.", LogLevel.HIGH); } return visibleElements; } /** * Gets the element. * * @param driver the driver * @param attributeName the attribute name * @param attributeValue the attribute value * @param timeout the timeout * @return the element */ public static WebElement getElement(final WebDriver driver, final String attributeName, final String attributeValue, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.presenceOfElementLocated(attribute(attributeName, attributeValue))); } catch (Exception ex) { return null; } } /** * Gets the element by text. * * @param driver the driver * @param locator the locator * @param innerText the inner text * @param timeout the timeout * @return the element by text */ public static WebElement getElementByText(final WebDriver driver, final By locator, final String innerText, final Timeout timeout) { for (final WebElement webElement : getElements(driver, locator, timeout)) { if (webElement.isDisplayed() && webElement.isEnabled() && webElement.getText().trim().equalsIgnoreCase(innerText)) { return webElement; } } return null; } /** * Gets the element by text contains. * * @param driver the driver * @param locator the locator * @param innerText the inner text * @param timeout the timeout * @return the element by text contains */ public static WebElement getElementByTextContains(final WebDriver driver, final By locator, final String innerText, final Timeout timeout) { for (final WebElement webElement : getElements(driver, locator, timeout)) { if (webElement.isDisplayed() && webElement.isEnabled() && webElement.getText().trim().contains(innerText)) { return webElement; } } return null; } /** * Gets the elements. * * @param driver the driver * @param locator the locator * @param timeout the timeout * @return the elements */ public static List<WebElement> getElements(final WebDriver driver, final By locator, final Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()) .until(ExpectedConditions.presenceOfAllElementsLocatedBy(locator)); } catch (Exception ex) { return null; } } /** * Gets the element by sub element. * * @param driver the driver * @param element the element * @param locator the locator * @param timeout the timeout * @return the element by sub element */ public static WebElement getElementBySubElement(final WebDriver driver, final WebElement element, final By locator, Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()).until(new ExpectedCondition<WebElement>() { public WebElement apply(final WebDriver driver) { if (element.findElement(locator) != null) { return element.findElement(locator); } return null; } }); } catch (Exception e) { return null; } } /** * Gets the elements by sub element. * * @param driver the driver * @param element the element * @param locator the locator * @param timeout the timeout * @return the elements by sub element */ public static List<WebElement> getElementsBySubElement(final WebDriver driver, final WebElement element, final By locator, Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()).until(new ExpectedCondition<List<WebElement>>() { public List<WebElement> apply(final WebDriver driver) { if (element.findElement(locator) != null) { return element.findElements(locator); } return null; } }); } catch (Exception e) { return null; } } /** * Gets the element by sub element text. * * @param driver the driver * @param subElement the sub element * @param locator the locator * @param innerText the inner text * @param timeout the timeout * @return the element by sub element text */ public static WebElement getElementBySubElementText(final WebDriver driver, final WebElement subElement, final By locator, final String innerText, final Timeout timeout) { for (final WebElement webElement : getElementsBySubElement(driver, subElement, locator, timeout)) { final String valueAttribute = webElement.getText(); if (valueAttribute.trim().equalsIgnoreCase(innerText)) { return webElement; } } return null; } /** * Gets the element by sub element text contains. * * @param driver the driver * @param subElement the sub element * @param locator the locator * @param innerText the inner text * @param timeout the timeout * @return the element by sub element text contains */ public static WebElement getElementBySubElementTextContains(final WebDriver driver, final WebElement subElement, final By locator, final String innerText, final Timeout timeout) { for (final WebElement webElement : getElementsBySubElement(driver, subElement, locator, timeout)) { final String valueAttribute = webElement.getText(); if (valueAttribute.trim().contains(innerText)) { return webElement; } } return null; } /** * Checks if is page load complete. * * @param driver the driver * @return true, if is page load complete */ public static boolean isPageLoadComplete(final WebDriver driver) { try { return new WebDriverWait(driver, 120).until(new ExpectedCondition<Boolean>() { public Boolean apply(final WebDriver driver) { return ((JavascriptExecutor) driver).executeScript("return document.readyState") .equals("complete"); } }); } catch (Exception ex) { return false; } } /** * Checks if is loading complete. * * @param driver the driver * @return true, if is loading complete */ public static boolean isLoadingComplete(final WebDriver driver) { boolean isloaded = false; try { isloaded = !getElement(driver, By.xpath("//div[@class='x-mask-msg-inner']"), Timeout.THREE_SEC) .isDisplayed(); } catch (Exception ex) { isloaded = false; } return isloaded; } /** * Attribute. * * @param tagname the tagname * @param value the value * @return the by */ public static By attribute(final String tagname, final String value) { return By.cssSelector("[" + tagname + "=\"" + value + "\"]"); } /** * Checks if is page loaded. * * @param driver the driver * @param element the element * @return true, if is page loaded */ public static boolean isPageLoaded(WebDriver driver, WebElement... element) { boolean elementLoaded = false; try { for (int i = 0; i < element.length; i++) { elementLoaded = element[i].isDisplayed(); if (!elementLoaded) { break; } } LogUtil.log("Page load :" + elementLoaded, LogLevel.HIGH); elementLoaded = elementLoaded && PageUtil.isPageLoadComplete(driver); } catch (Exception ex) { elementLoaded = false; } return elementLoaded; } /** * Drag and drop. * * @param driver the driver * @param fromElement the from element * @param toElement the to element * @param xOffset the x offset * @param yOffset the y offset */ public static void dragAndDrop(final WebDriver driver, final WebElement fromElement, final WebElement toElement, final int xOffset, final int yOffset) { final Actions builder = new Actions(driver); builder.dragAndDrop(fromElement, toElement).build().perform(); } /** * Move to element. * * @param driver the driver * @param element the element */ public static void moveToElement(final WebDriver driver, final WebElement element) { final Actions builder = new Actions(driver); builder.moveToElement(element); } /** * Double click. * * @param driver the driver * @param element the element */ public static void doubleClick(final WebDriver driver, final WebElement element) { new Actions(driver).doubleClick(element).perform(); } /** * Checks if is text displayed in source. * * @param driver the driver * @param content the content * @param timeout the timeout * @return true, if is text displayed in source */ public static boolean isTextDisplayedInSource(final WebDriver driver, final String content, Timeout timeout) { try { return new WebDriverWait(driver, timeout.getValue()).until(new ExpectedCondition<Boolean>() { public Boolean apply(final WebDriver driver) { return driver.getPageSource().contains(content); } }); } catch (Exception ex) { return false; } } /** * Double tap. * * @param driver the driver * @param element the element */ public static void doubleTap(final WebDriver driver, WebElement element) { Point point = element.getLocation(); int x = point.getX(); int y = point.getY(); Dimension dimesions = element.getSize(); int width = dimesions.width; int height = dimesions.height; ((TouchShortcuts) driver).tap(2, x + width - 2, y + height - 2, 1); /* * for (int i = 0; i < 2; i++) { * * Point point = element.getLocation(); double x = point.getX(); double * y = point.getY(); * * Dimension dimesions = element.getSize(); int width = dimesions.width; * int height = dimesions.height; * * final JavascriptExecutor js = (JavascriptExecutor) driver; final * HashMap<String, Double> tapObject = new HashMap<String, Double>(); * tapObject.put("tapCount", 2.0); tapObject.put("touchCount", 1.0); * tapObject.put("duration", 0.7); tapObject.put("fingers ", 2.0); * tapObject.put("x", x + width - 2); tapObject.put("y", y + height - * 2); js.executeScript("mobile: tap", tapObject); * WaitUtil.waitUntil(1); } */ } /** * Gets the alert text. * * @param driver the driver * @return the alert text */ public static String getAlertText(final WebDriver driver) { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); alert.accept(); return alertText; } /** * Adds the common script. * * @param driver the driver * @param elementToTap the element to tap * @return the string */ private static String addCommonScript(final WebDriver driver, WebElement elementToTap) { return "window.jQuery(document.elementFromPoint(" + elementToTap.getLocation().getX() + "," + elementToTap.getLocation().getY() + "))"; } /** * Execute script by class name. * * @param name the name * @param propertyName the property name * @param driver the driver * @return the object */ public static Object executeScriptByClassName(final String name, final String propertyName, final WebDriver driver) { final JavascriptExecutor javascript = (JavascriptExecutor) driver; final Object val = javascript.executeScript(new StringBuilder( "return window.document.defaultView.getComputedStyle(window.document.getElementsByClassName('") .append(name).append("')[0]).getPropertyValue('").append(propertyName).append("');") .toString()); return val; } /** * Gets the value by id. * * @param id the id * @param driver the driver * @return the value by id */ public static String getValueById(final String id, final WebDriver driver) { final JavascriptExecutor jsExecutor = (JavascriptExecutor) driver; final Object val = jsExecutor.executeScript("return window.document.getElementById('" + id + "').value;"); return val.toString(); } /** * Method to set date in a date picker * @author Ruth Chirinos * @param driver the WebDriver * @param xpathDatePicker the xpath of the datePicker * @param vehicleSelectionPage * year - the year minus 1900. * month - the month between 0-11. * date - the day of the month between 1-31. * @throws InterruptedException * @throws ParseException */ public static boolean setDateInDatePicker(WebDriver driver, String xpathDatePicker, Date dateRequested) throws InterruptedException, ParseException { Calendar dateRequestedCalendar = DateUtil.getCalendarForDate(dateRequested); LogUtil.log("Date for changing is > " + DateUtil.format(dateRequestedCalendar, DateUtil.DATE_MONTH_ABBREVIATION), LogLevel.LOW); boolean processOk = false; String dateSelectedString = ""; Date dateSelected = null; SimpleDateFormat simpleFormat = new SimpleDateFormat(DateUtil.DATE_MONTH_YEAR_FMT); WebElement datePicker = driver.findElement(By.xpath(xpathDatePicker)); datePicker.click(); WebElement monthYearElement = driver .findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]")); WebElement daySelected = driver.findElement(By.xpath("//*[contains(@class, 'x-datepicker-selected')]/a")); dateSelectedString = daySelected.getAttribute("innerHTML") + " " + monthYearElement.getAttribute("innerHTML"); dateSelected = simpleFormat.parse(dateSelectedString); WebElement pickerPrevEl = driver.findElement( By.xpath("//div[@class='x-datepicker-header']/a[contains(@class, 'x-datepicker-prev')]")); WebElement pickerNextEl = driver.findElement( By.xpath("//div[@class='x-datepicker-header']/a[contains(@class, 'x-datepicker-next')]")); //Choosing the year while (dateRequested.getYear() < dateSelected.getYear()) { pickerPrevEl.click(); monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]")); dateSelectedString = daySelected.getAttribute("innerHTML") + " " + monthYearElement.getAttribute("innerHTML"); dateSelected = simpleFormat.parse(dateSelectedString); } while (dateRequested.getYear() > dateSelected.getYear()) { pickerNextEl.click(); monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]")); dateSelectedString = daySelected.getAttribute("innerHTML") + " " + monthYearElement.getAttribute("innerHTML"); dateSelected = simpleFormat.parse(dateSelectedString); } //Choosing the months while (dateRequested.getMonth() < dateSelected.getMonth()) { pickerPrevEl.click(); monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]")); dateSelectedString = daySelected.getAttribute("innerHTML") + " " + monthYearElement.getAttribute("innerHTML"); dateSelected = simpleFormat.parse(dateSelectedString); } while (dateRequested.getMonth() > dateSelected.getMonth()) { pickerNextEl.click(); monthYearElement = driver.findElement(By.xpath("//div[@class='x-datepicker-month']/div/div/a/span[1]")); dateSelectedString = daySelected.getAttribute("innerHTML") + " " + monthYearElement.getAttribute("innerHTML"); dateSelected = simpleFormat.parse(dateSelectedString); } //Choosing the day WebElement dateWidget = driver.findElement(By.className("x-datepicker-inner")); List<WebElement> columns = dateWidget.findElements(By.tagName("td")); int aux = 0; for (WebElement cell : columns) { if (cell.getText().equals(dateRequested.getDate() + "")) { processOk = cell.getAttribute("class").contains("x-datepicker-active"); if (processOk) { cell.findElement(By.tagName("a")).click(); ; LogUtil.log("The date was selected.", LogLevel.LOW); aux = 1; break; } } } if (aux == 0) { LogUtil.log("The date selected is not enabled.", LogLevel.LOW); } return processOk; } /* * Verify if a string contains an email format * * */ public static boolean checkEmailFormat(String email) { boolean result = false; Pattern patternEmail = Pattern.compile( "^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$"); if (patternEmail.matcher(email).matches()) { result = true; } return result; } /** * Switch to next window. * * @param driver the driver */ public static void switchToNextWindow(WebDriver driver) { for (String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle); } } }