/** Switche to the child window */ public void switchToChildWindow(MethodParameters model) { model.getElement().get(0).click(); String parent = WebDriverClass.getInstance().getWindowHandle(); Set<String> windows = WebDriverClass.getInstance().getWindowHandles(); try { if (windows.size() > 1) { for (String child : windows) { if (!child.equals(parent)) { if (WebDriverClass.getInstance() .switchTo() .window(child) .getTitle() .equals(model.getData())) { WebDriverClass.getInstance().switchTo().window(child); } } } } } catch (Exception e) { throw new RuntimeException("Exception", e); } }
/** * @param model Lets say there is header menu bar, on hovering the mouse, drop down should be * displayed */ public void dropDownByMouseHover(MethodParameters model) { Actions action = new Actions(WebDriverClass.getInstance()); action.moveToElement(model.getElement().get(0)).perform(); WebElement subElement = WebDriverClass.getInstance().findElement(By.xpath(model.getData())); action.moveToElement(subElement); action.click().build().perform(); }
/** @param model Not tested */ public void DragAndDrop(MethodParameters model) { String[] actType = model.getActionType().split("$"); WebElement sourceElement = WebDriverClass.getDriver().findElement(By.xpath(actType[0])); WebElement destinationElement = WebDriverClass.getDriver().findElement(By.xpath(actType[1])); Actions action = new Actions(WebDriverClass.getDriver()); action.dragAndDrop(sourceElement, destinationElement).build().perform(); }
/** Find Element By CSS */ private void findElementByCssSelector(String objectLocators) { WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(objectLocators))); List<WebElement> list1 = WebDriverClass.getInstance().findElements(By.cssSelector(objectLocators)); listOfElements = list1; }
/** Alert dismiss meaning click on Cancel button */ public void alertDismiss(MethodParameters model) { WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.until(ExpectedConditions.alertIsPresent()); MainTestNG.LOGGER.info("inside alertDismiss()"); wait1(2000); model.getElement().get(0).click(); Alert alert = WebDriverClass.getInstance().switchTo().alert(); wait1(2000); alert.dismiss(); }
/** Find Element By Name */ public void findElementByName(String objectLocators) { MainTestNG.LOGGER.info("findElementByName==" + objectLocators); WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.until( ExpectedConditions.visibilityOfAllElements( WebDriverClass.getDriver().findElements(By.name(objectLocators)))); MainTestNG.LOGGER.info("element found==" + objectLocators); List<WebElement> list1 = WebDriverClass.getInstance().findElements(By.name(objectLocators)); MainTestNG.LOGGER.info("list size" + list1.size()); listOfElements = list1; }
/** Find Element By Xpath */ public void findElementByXpath(String objectLocators) { WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath(objectLocators))); List<WebElement> list1 = wait.until( ExpectedConditions.visibilityOfAllElements( WebDriverClass.getDriver().findElements(By.xpath(objectLocators)))); listOfElements = list1; }
/** Get the title of the page and verify the title */ public void verifyTitleOfPage(MethodParameters model) { MainTestNG.LOGGER.info( "inside verifyTitleOfPage()" + "title==" + WebDriverClass.getInstance().getTitle() + "data from excel=" + model.getData()); wait1(2000); String actual = WebDriverClass.getInstance().getTitle().toString(); String expected = model.getData().toString(); Assert.assertEquals(actual, expected); MainTestNG.LOGGER.info("assert verification successful verifyTitleOfPage()"); }
/** Alert accept meaning click on OK button */ public void alertAccept(MethodParameters model) { WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.until(ExpectedConditions.alertIsPresent()); MainTestNG.LOGGER.info("inside alertAccept()"); wait1(2000); Alert alert = WebDriverClass.getInstance().switchTo().alert(); wait1(2000); alert.accept(); MainTestNG.LOGGER.info("completed alertAccept()"); }
/** Scrolls down the page till the element is visible */ public void scrollElementIntoView(MethodParameters model) { wait1(1000); MainTestNG.LOGGER.info("scrollElementIntoView started"); ((JavascriptExecutor) WebDriverClass.getDriver()) .executeScript("arguments[0].scrollIntoView(true);", model.getElement().get(0)); wait1(1000); }
/** Click on button/checkbox/radio button */ public void click(MethodParameters model) { WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.until(ExpectedConditions.elementToBeClickable(model.getElement().get(0))).click(); MainTestNG.LOGGER.info("click method started" + model.getObjectLocators()); MainTestNG.LOGGER.info("click method completed"); }
/** * Select from the drop down list,if the drop down element tag is "SELECT" then use this method */ public void selectDropDownByVisibleText(MethodParameters model) { wait1(2000); MainTestNG.LOGGER.info("inside selectDropDownByVisibleText"); WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 30); wait.pollingEvery(2, TimeUnit.SECONDS) .until(ExpectedConditions.elementToBeClickable(model.getElement().get(0))); Select sel = new Select(model.getElement().get(0)); sel.selectByVisibleText(model.getData()); wait1(2000); }
/** @param model Verify the alert text */ public void verifyalertText(MethodParameters model) { model.getElement().get(0).click(); wait1(1000); Alert alert = WebDriverClass.getInstance().switchTo().alert(); wait1(1000); if (!(alertText == null)) { alertText = null; } alertText = alert.getText(); Assert.assertEquals(alertText.toString(), model.getData()); alert.accept(); }
/** Downloads a file from IE browser */ public void downloadFileIE(MethodParameters model) { FileDownloader downloadTestFile = new FileDownloader(WebDriverClass.getDriver()); String downloadedFileAbsoluteLocation; try { downloadedFileAbsoluteLocation = downloadTestFile.downloadFile(model.getElement().get(0)); Assert.assertTrue(new File(downloadedFileAbsoluteLocation).exists()); } catch (Exception e) { MainTestNG.LOGGER.info("exception occured"); } }
/** Selects the Date */ public void selectDate(String date) { WebElement datePicker = WebDriverClass.getDriver().findElement(By.id("ui-datepicker-div")); List<WebElement> noOfColumns = datePicker.findElements(By.tagName("td")); // Loop will rotate till expected date not found. for (WebElement cell : noOfColumns) { // Select the date from date picker when condition match. if (cell.getText().equals(date)) { cell.findElement(By.linkText(date)).click(); break; } } }
public static WebElement waitForElement(By by) { int count = 0; WebDriverWait wait = null; while (!(wait.until(ExpectedConditions.presenceOfElementLocated(by)).isDisplayed())) { wait = new WebDriverWait(WebDriverClass.getInstance(), 60); wait.pollingEvery(5, TimeUnit.MILLISECONDS); wait.until(ExpectedConditions.visibilityOfElementLocated(by)).isDisplayed(); wait.until(ExpectedConditions.presenceOfElementLocated(by)).isDisplayed(); count++; if (count == 100) { break; } return wait.until(ExpectedConditions.presenceOfElementLocated(by)); } return wait.until(ExpectedConditions.presenceOfElementLocated(by)); }
/** @param model Not tested */ public void webTableClick(MethodParameters model) { String[] actType = model.getActionType().split("\\$"); WebElement mytable = WebDriverClass.getDriver().findElement(By.xpath(actType[0])); List<WebElement> rowstable = mytable.findElements(By.tagName("tr")); int rows_count = rowstable.size(); for (int row = 0; row < rows_count; row++) { List<WebElement> Columnsrow = rowstable.get(row).findElements(By.tagName("td")); int columnscount = Columnsrow.size(); for (int column = 0; column < columnscount; column++) { String celtext = Columnsrow.get(column).getText(); celtext.getClass(); } } }
/** Reads the url of current web page */ public void readUrlOfPage(MethodParameters model) { WebDriverClass.getInstance().getCurrentUrl(); }
/** @param model Select date from date picker */ public void selectDateFromCalendar(MethodParameters model) { String[] actionType = model.getActionType().split("$$"); String[] data = model.getData().split("/"); List<String> monthList = Arrays.asList( "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); int expMonth; int expYear; String expDate = null; // Calendar Month and Year String calMonth = null; String calYear = null; boolean dateNotFound; // WebDriverClass.getDriver() // .findElement(By.xpath(".//*[@id='ui-datepicker-div']")).click(); WebDriverClass.getDriver().findElement(By.xpath(actionType[0])).click(); dateNotFound = true; // Set your expected date, month and year. expDate = data[0]; expMonth = Integer.parseInt(data[1]); expYear = Integer.parseInt(data[2]); // This loop will be executed continuously till dateNotFound Is true. while (dateNotFound) { // Retrieve current selected month name from date picker popup. calMonth = WebDriverClass.getDriver().findElement(By.className("ui-datepicker-month")).getText(); // Retrieve current selected year name from date picker popup. calYear = WebDriverClass.getDriver().findElement(By.className("ui-datepicker-year")).getText(); /* * If current selected month and year are same as expected month and * year then go Inside this condition. */ if (monthList.indexOf(calMonth) + 1 == expMonth && (expYear == Integer.parseInt(calYear))) { /* * Call selectDate function with date to select and set * dateNotFound flag to false. */ selectDate(expDate); dateNotFound = false; } // If current selected month and year are less than expected month // and year then go Inside this condition. else if (monthList.indexOf(calMonth) + 1 < expMonth && (expYear == Integer.parseInt(calYear)) || expYear > Integer.parseInt(calYear)) { // Click on next button of date picker. /* * WebDriverClass .getDriver() .findElement( * By.xpath(".//*[@id='ui-datepicker-div']/div[2]/div/a/span")) * .click(); */ WebDriverClass.getDriver().findElement(By.xpath(actionType[1])).click(); } // If current selected month and year are greater than expected // month and year then go Inside this condition. else if (monthList.indexOf(calMonth) + 1 > expMonth && (expYear == Integer.parseInt(calYear)) || expYear < Integer.parseInt(calYear)) { // Click on previous button of date picker. /* * WebDriverClass .getDriver() .findElement( * By.xpath(".//*[@id='ui-datepicker-div']/div[1]/div/a/span")) * .click(); */ WebDriverClass.getDriver().findElement(By.xpath(actionType[2])).click(); } } wait1(3000); }
/** Closes the driver */ public void close(MethodParameters model) { WebDriverClass.getInstance().close(); }
/** @param model clear the content of the text field */ public void clear(MethodParameters model) { wait1(2000); WebDriverWait wait = new WebDriverWait(WebDriverClass.getDriver(), 60); wait.until(ExpectedConditions.visibilityOf(model.getElement().get(0))); model.getElement().get(0).clear(); }
/** Refresh the current web page */ public void refreshPage(MethodParameters model) { WebDriverClass.getInstance().navigate().refresh(); }
/** @param model SSL errors that appear on IE browser can be resolved */ public void certificateErrorsIE(MethodParameters model) { WebDriverClass.getDriver() .navigate() .to("javascript:document.getElementById('overridelink').click()"); }
/** Switch back to the parent window */ public void switchToParentWindow(MethodParameters model) { String parentWindow = WebDriverClass.getInstance().getWindowHandle(); WebDriverClass.getInstance().switchTo().window(parentWindow); }
/** @param model Read title of the page and verify it */ public void readTitleOfPage(MethodParameters model) { if (!(titleOfPage == null)) { titleOfPage = null; } titleOfPage = WebDriverClass.getInstance().getTitle(); }
/** Scrolls down the page till the element is visible and clicks on the element */ public void scrollElementIntoViewClick(MethodParameters model) { Actions action = new Actions(WebDriverClass.getDriver()); action.moveToElement(model.getElement().get(0)).click().perform(); }
/** Provide Login name for window authentication */ public static void windowAuthenticationLoginName(MethodParameters model) { Alert alert = WebDriverClass.getDriver().switchTo().alert(); alert.sendKeys(model.getData()); }
/** Mouse hovering on the element is performed */ public void singleMouseHover(MethodParameters model) { Actions action = new Actions(WebDriverClass.getDriver()); action.moveToElement((WebElement) model.getElement()).perform(); }
/** Right clicks on the element */ public void rightClick(MethodParameters model) { Actions action = new Actions(WebDriverClass.getDriver()); action.contextClick((WebElement) model.getElement()).perform(); }
/** Navigates to the specified url */ public void navigateToURL(MethodParameters model) { WebDriverClass.getInstance().navigate().to(model.getData()); }