All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bichromate.core.sTestBasePageDeclaration Maven / Gradle / Ivy

Go to download

Java, Selenium, Appium, Winium, Extend, and TestNG automated testing framework. Bichromate integrates the best of these frameworks and takes automation to the next level. With Bichromate there is one function call that builds any type of Web,IOS Mobile, Android, and Windows App driver on any platform (Windows, Mac, Linux). From Local web drivers, to SauceLabs, Browserstack, and Selenium grid. Build data driven tests is never easier. Bichromate also gives you built in Factories that, access DBs, Video Capture, FTP, POM Generation, Hilite element.

There is a newer version: 3.13
Show newest version
/*
 * sTestBasePageDeclaration.java	1.0 2013/01/23
 *
 * Copyright (c) 2001 by David Ramer, Inc. All Rights Reserved.
 *
 * David Ramer grants you ("Licensee") a non-exclusive, royalty free, license to use,
 * modify and redistribute this software in source and binary code form,
 * provided that i) this copyright notice and license appear on all copies of
 * the software; and ii) Licensee does not utilize the software in a manner
 * which is disparaging to David Ramer.
 *
 * This software is provided "AS IS," without a warranty of any kind. ALL
 * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING ANY
 * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
 * NON-INFRINGEMENT, ARE HEREBY EXCLUDED. David Ramer AND ITS LICENSORS SHALL NOT BE
 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
 * OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL David Ramer OR ITS
 * LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER
 * CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF
 * OR INABILITY TO USE SOFTWARE, EVEN IF DRamer HAS BEEN ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control of
 * aircraft, air traffic, aircraft navigation or aircraft communications; or in
 * the design, construction, operation or maintenance of any nuclear
 * facility. Licensee represents and warrants that it will not use or
 * redistribute the Software for such purposes.
 * 
 * 
 * 
 */

package bichromate.core;

import java.util.ArrayList;
import java.util.List;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;

import org.apache.http.client.methods.HttpGet;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;

import bichromate.dataStore.sTestDOMElementTypes;

import org.apache.commons.lang.time.StopWatch;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
/**
 * @author davidwramer
 * @version 1.0
 *
 */
@SuppressWarnings("unused")
public abstract class sTestBasePageDeclaration {
	//
	// For Page text validation
	//
	private final String numberTextItemsToValidate = "numberTextItemsToValidate";
	private final String textItem = "textItem";
	private int textValidationCount = 0;

	public WebDriver driver = null;
	private String parentHandle = null;
	private JavascriptExecutor jsExec = null;
	private ResourceBundle resources;
	private boolean validateTextElements;
	public String pageTitle = null;
	public String pageURL = null;
	public String regExPageURL = null;
	private int invalidLinksCount;
	public static pageObjectModelLogFactory POMLOGGER = null; //new pageObjectModelLogFactory();
	
	//
	// Stop Watch for page load times
	//
	private StopWatch pageLoad = null; //new StopWatch();
	private sTestOSInformationFactory path = null;

	public sTestBasePageDeclaration(WebDriver driver, ResourceBundle bundle) {
		this.driver = driver;
		parentHandle = driver.getWindowHandle(); // get the current window handle
		jsExec = (JavascriptExecutor) this.driver;
		resources = bundle;
		validateTextElements = false;
		pageLoad = new StopWatch(); //new StopWatch();
		path = new sTestOSInformationFactory();
		POMLOGGER = new pageObjectModelLogFactory();
	}
	/**
	 * This abstract method demonstrates ajaxLoaded(). 
	 * 
For any page that has ajax, you must delcare a method that can deterime if the ajax has loaded * @return boolean - true if the ajax is done doing work * @see WebDriver * @see JavascriptExecutor * @see WebElement * */ public abstract boolean isAjaxLoaded(); /** * switchToActiveModalDialog(). *
* @return String context of the dialog * @see WebDriver * @see JavascriptExecutor * @see WebElement * */ public String getAlertDialogText(){ try { WebDriverWait wait = new WebDriverWait(driver, 8); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alert.accept(); return(alert.getText()); } catch (Exception e) { POMLOGGER.enterInfoLog("sTestBasePageDeclaration:getAlertDialogText failed to get alert text" ); return ""; } }//getAlertDialogText /** * closeAlert(). *
* @see WebDriver * @see JavascriptExecutor * @see WebElement * */ public void closeAlert(){ try { WebDriverWait wait = new WebDriverWait(driver, 8); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alert.dismiss(); } catch (Exception e) { POMLOGGER.enterInfoLog("sTestBasePageDeclaration:closeAlert alert failed to close" ); } }//closeAlert /** * getAlertText(). *
Return the text in the Alert * @return String - Text from the ALert * @see WebDriver * @see JavascriptExecutor * @see WebElement * @author dramer * @version 3.11 * */ public String getAlertText(){ String alertText = ""; try { WebDriverWait wait = new WebDriverWait(driver, 8); wait.until(ExpectedConditions.alertIsPresent()); Alert alert = driver.switchTo().alert(); alertText = alert.getText(); } catch (Exception e) { POMLOGGER.enterInfoLog("sTestBasePageDeclaration:getAlertText alert failed to obtain alert text" ); } return alertText; }//getAlertText /** * getModalDialogText(). *
* @return String context of the dialog * @see WebDriver * @see JavascriptExecutor * @see WebElement * */ public String getModalDialogText(){ String dialogText = ""; try { for (String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window) } dialogText = driver.getTitle(); } catch (Exception e) { POMLOGGER.enterInfoLog("sTestBasePageDeclaration:getAlertDialogText failed to get alert text" ); } return dialogText; }//getModalDialogText /** * closeModal(). *
* @see WebDriver * @see JavascriptExecutor * @see WebElement * */ public void closeModal(){ try { for (String winHandle : driver.getWindowHandles()) { driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window) } driver.close(); // close newly opened window when done with it driver.switchTo().window(parentHandle); // switch back to the original window } catch (Exception e) { POMLOGGER.enterInfoLog("sTestBasePageDeclaration:closeModal alert failed to close" ); } }//closeModal /** * This method demonstrates scrollPageToTheTop(). *
* @see WebDriver * @see JavascriptExecutor * @see WebElement * */ public void scrollPageToTheTop(){ try{ WebElement element = driver.findElement(By.tagName("header")); jsExec.executeScript("arguments[0].scrollIntoView();", element); }catch(Exception e){ POMLOGGER.enterInfoLog("oTestBasePageDeclaration:scrollPageToTheTop failed to scroll the page to the top" ); } } /** * This method demonstrates validateTextItems().
* validateTextItems, public function to validate page text items from the * properties file.
* @return failedCount - number of failed elements on the page * @see WebDriver * @see WebElement * */ public int validateTextItems() { String textToValidateCount; String textToValidate; int failedCount = 0; int numerOfTextItemsToValidate = 0; try { textToValidateCount = new String(resources.getString(numberTextItemsToValidate)).trim(); WebElement bodyTag = driver.findElement(By.tagName("body")); numerOfTextItemsToValidate = Integer.parseInt(textToValidateCount); for (int x = 1; x <= numerOfTextItemsToValidate; x++) { textToValidate = new String(resources.getString(textItem + x)).trim(); if (!(bodyTag.getText().contains(textToValidate))) { POMLOGGER.enterInfoLog( "oTestBasePageDeclaration: Warning, Text not found on page:" + textToValidate); failedCount++; } } } catch (MissingResourceException e) { POMLOGGER.enterSevereLog("oTestBasePageDeclaration: validateTextItems(), resouce not found"); } return failedCount; } /** * This method demonstrates setElementTextValidation().
* setElementTextValidation, public function to set element text validation * to true.
* @param set * - If true will validate all element text items when calling * FindElement * @see WebDriver * @see WebElement * */ public void setElementTextValidation(boolean set) { validateTextElements = set; } /** * This method demonstrates findElement().
* Find the first WebElement using the given method. See the note in * findElements(By)
* about finding via XPath. This method is affected by the 'implicit wait' * times in
* force at the time of execution. The findElement(..) invocation will * return a matching row,
* or try again repeatedly until the configured timeout is reached. * findElement should not be
* used to look for non-present elements, use findElements(By) and assert * zero length response
* instead. "Type" is appended to the elementName to find the type of * element that is being requested.
* EXAMPE: searchField=//input[@name='q']
* searchFieldType=xpath
*
* EXCEPTIONS: If no "Type" resource is defined for the element, xpath will * be selected.
*
* * @param element * from the pages properties file. * @return WebElement if found null otherwise * @see WebDriver * @see WebElement * */ protected WebElement findElement(String element) { String DOM_Element_Type = null; // EXAMPLE: xpath, css, partial link String DOM_Element = null; // oDesk element to find, EXAMPLE // passwordField String DOM_Element_Name = null; // Name of the specific element WebElement el = null; // Selenium web element that is found // // First find the element type from the resource file // try { DOM_Element_Type = new String(resources.getString(element + "Type")).trim(); } catch (MissingResourceException e) { POMLOGGER .enterInfoLog("oTestBasePageDeclaration: Warning, no Type found in properties file for:" + element); DOM_Element_Type = new String(sTestDOMElementTypes.XPATH_SELECTOR); } // // Obtain the DOM element for the element passed in // try { DOM_Element = new String(resources.getString(element)); } catch (MissingResourceException e) { POMLOGGER.enterSevereLog("oTestBasePageDeclaration: Error, element not found in properties file: " + e); return null; } // // Find the element on the page based on the findElement(By setting // try { if (DOM_Element_Type.equals(sTestDOMElementTypes.XPATH_SELECTOR)) { el = driver.findElement(By.xpath(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.CSS_SELECTOR)) { el = driver.findElement(By.cssSelector(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.CLASS_SELECTOR)) { el = driver.findElement(By.className(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.ID_SELECTOR)) { el = driver.findElement(By.id(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.LINK_TEXT_SELECTOR)) { el = driver.findElement(By.linkText(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.TAG_NAME_SELECTOR)) { el = driver.findElement(By.tagName(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.PARTIAL_LINKTEXT_SELECTOR)) { el = driver.findElement(By.partialLinkText(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.NAME_SELECTOR)) { el = driver.findElement(By.name(DOM_Element)); } else {// default is xpath el = driver.findElement(By.xpath(DOM_Element)); POMLOGGER.enterWarningLog( "Error: invalid element type " + DOM_Element + " for the following element " + DOM_Element); } } catch (Exception e) { POMLOGGER.enterWarningLog("Error: element \"Type\" not found on page: " + element); } if (validateTextElements) validateElementText(element); return el; }// findElement /** * This method demonstrates validateElementText(). ValidateElementText will * look in the properties file for the text related to a specific element. * Read the element text from the properties file and then try to validate * it on the page
* * @param element * web element to verify in the properties file * @see WebDriver * @see WebElement * */ public void validateElementText(String element) { String textToValidate; try { textToValidate = new String(resources.getString(element + "Text")).trim(); WebElement bodyTag = driver.findElement(By.tagName("body")); if (null != bodyTag) { if (!(bodyTag.getText().contains(textToValidate))) { POMLOGGER.enterWarningLog( "oTestBasePageDeclaration: Warning, element Text not found on page:" + element); } } else { POMLOGGER.enterWarningLog( "oTestBasePageDeclaration: Warning, element \"body\" tag not found on page :" + element); } } catch (MissingResourceException e) { POMLOGGER.enterSevereLog( "oTestBasePageDeclaration: Severe, no Text type found in properties file for:" + element); } }// validateElementText /** * This methodemonstrates findElements().
* Find the list of WebElements using the given method. See the note in * findElements(By)
* about finding via XPath. This method is affected by the 'implicit wait' * times in
* force at the time of execution. The findElement(..) invocation will * return a matching row,
* or try again repeatedly until the configured timeout is reached. * findElement should not be
* used to look for non-present elements, use findElements(By) and assert * zero length response
* instead. "Type" is appended to the elementName to find the type of * element that is being requested.
* EXAMPE: searchField=//input[@name='q']
* searchFieldType=xpath
*
* EXCEPTIONS: If no "Type" resource is defined for the element, xpath will * be selected.
*
* * @param element * from the pages properties file. * @return theList if found null otherwise * @see WebDriver * @see WebElement * */ protected List findElements(String element) { String DOM_Element_Type = null; // EXAMPLE: xpath, css, partial link String DOM_Element = null; // oDesk element to find, EXAMPLE // passwordField String DOM_Element_Name = null; // Name of the specific element List theList = null; // // First find the element type from the resource file // try { DOM_Element_Type = new String(resources.getString(element + "Type")).trim(); } catch (MissingResourceException e) { POMLOGGER.enterSevereLog("oTestBasePageDeclaration: Warning, no Type found for:" + element); DOM_Element_Type = new String(sTestDOMElementTypes.XPATH_SELECTOR); } // // Obtain the DOM element for the element passed in // try { DOM_Element = new String(resources.getString(element)); } catch (MissingResourceException e) { POMLOGGER.enterSevereLog("oTestBasePageDeclaration: Error, element not found: " + e); return theList; } // // Find the element on the page based on the findElement(By setting // try { if (DOM_Element_Type.equals(sTestDOMElementTypes.XPATH_SELECTOR)) { theList = driver.findElements(By.xpath(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.CSS_SELECTOR)) { theList = driver.findElements(By.cssSelector(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.CLASS_SELECTOR)) { theList = driver.findElements(By.className(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.ID_SELECTOR)) { theList = driver.findElements(By.id(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.LINK_TEXT_SELECTOR)) { theList = driver.findElements(By.linkText(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.TAG_NAME_SELECTOR)) { theList = driver.findElements(By.tagName(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.PARTIAL_LINKTEXT_SELECTOR)) { theList = driver.findElements(By.partialLinkText(DOM_Element)); } else if (DOM_Element_Type.equals(sTestDOMElementTypes.NAME_SELECTOR)) { theList = driver.findElements(By.name(DOM_Element)); } else {// default is xpath theList = driver.findElements(By.xpath(DOM_Element)); POMLOGGER.enterWarningLog( "Error: invalid element type " + DOM_Element + " for the following element " + DOM_Element); } } catch (Exception e) { POMLOGGER.enterSevereLog("Error: element list \"Type\" not found on page: " + element); } return theList; }// findElements /** * verifyFoundPageByURL() * * @param waitTime * How long to wait for page to respond * @param URL * - URL of the page loading * @param testName * - Name of the test running * @see WebDriverWait * @author DRamer * @version 1.0 */ public void verifyFoundPageByURL(int waitTime, String URL, String testName) { final String thisURL = new String(URL); try { (new WebDriverWait(driver, waitTime)).until(new ExpectedCondition() { public Boolean apply(WebDriver d) { boolean result = d.getCurrentUrl().contains(thisURL); if (true == result) { POMLOGGER.enterInfoLog("verifyFoundPageByURL : MATCHED: Actual URL: " + d.getCurrentUrl() + " vs Expected URL: " + thisURL); } else { POMLOGGER.enterInfoLog("verifyFoundPageByURL : MISMATCH: Actual URL: " + d.getCurrentUrl() + " vs Expected URL: " + thisURL); } return result; }// apply }); } catch (TimeoutException e) { POMLOGGER.enterInfoLog(testName + ": Driver.getCurrentUrl did not match: " + thisURL); throw new TimeoutException(); } } /** * validateInvalidLinks(). Find out broken links on website using selenium * webdriver and HTTP Client * @return int - number of invalid links * @author DRamer * @version 1.0 */ public int validateInvalidLinks() { try { invalidLinksCount = 0; List anchorTagsList = driver.findElements(By.tagName("a")); POMLOGGER.enterInfoLog("Total no. of links are " + anchorTagsList.size()); for (WebElement anchorTagElement : anchorTagsList) { if (anchorTagElement != null) { String url = anchorTagElement.getAttribute("href"); if (url != null && !url.contains("javascript")) { verifyURLStatus(url); } else { invalidLinksCount++; } } } Reporter.log("Total no. of invalid links are " + invalidLinksCount, true); return invalidLinksCount; } catch (Exception e) { e.printStackTrace(); POMLOGGER.enterInfoLog(e.getMessage()); return invalidLinksCount; } }// validateInvalidLinks /** * verifyURLStatus(). check URL status * * @exception None. * */ private void verifyURLStatus(String URL) { HttpClient client = HttpClientBuilder.create().build(); HttpGet request = new HttpGet(URL); try { HttpResponse response = client.execute(request); // verifying response code and The HttpStatus should be 200 if not, // increment invalid link count //// We can also check for 404 status code like // response.getStatusLine().getStatusCode() == 404 if (response.getStatusLine().getStatusCode() != 200) invalidLinksCount++; } catch (Exception e) { POMLOGGER.enterInfoLog("STestWebDriverFactory:verifyURLStatus: Exception caught" + e); } } /** * method demonstrates scrollGrid Used to perform scroll operations. * * @param row * element to search for while scrolling in grid * @return boolean - true if scroll happened */ public boolean scrollGrid(WebElement row) { // Clicking on an element inside grid to get it into focus row.click(); WebElement ele = null; int flag = 0; int count = 0; do { try { // element to search for while scrolling in grid row.isDisplayed(); flag = 1; } catch (Throwable e) { // scrolling the grid using the grid's xpath row.sendKeys(Keys.PAGE_DOWN); try { Thread.sleep(3000); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } } while ((flag == 0) || ((++count) == 250)); if (flag == 1) { POMLOGGER.enterInfoLog("sTestWebDriverFactory: scrollGrid: Element has been found.!!"); return true; } else { POMLOGGER.enterInfoLog("sTestWebDriverFactory: scrollGrid: Element has not been found.!!"); } try { Thread.sleep(5000L); } catch (InterruptedException e) { POMLOGGER.enterSevereLog("sTestWebDriverFactory: scrollGrid: Exception caught"); } // to check if the element scrolled to is highlighted. return false; }//// ScrollGrid /** * openURLInNewTab() * Used to open a new tab for testing * @author DRamer * @version 1.0 */ public void openNewTab(){ if(path.getOSName().contains(Platform.MAC.toString())){ driver.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND +"t"); ArrayList tabs = new ArrayList (driver.getWindowHandles()); Reporter.log("number of Tabs" + tabs.size(),true); driver.switchTo().window(tabs.get(0)); }else{ driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t"); ArrayList tabs = new ArrayList (driver.getWindowHandles()); Reporter.log("number of Tabs" + tabs.size(),true); driver.switchTo().window(tabs.get(0)); } }//openURLInNewTab /** * startStopWatch() * Used to calculate page load times * @return osInfo */ private void stopWatchStart(){ if(null != pageLoad){ pageLoad.start(); }else{ POMLOGGER.enterInfoLog("sTestPageDeclaration : stopWatchStart - pageLoad = null"); } }//stopWatchStart /** * stopWatchStop() * Used to calculate page load times * @return osInfo */ private long stopWatchStop(String url){ if(null != pageLoad){ pageLoad.stop(); long pageLoadTime_ms = pageLoad.getTime(); long pageLoadTime_Seconds = pageLoadTime_ms / 1000; POMLOGGER.enterInfoLog("Load Time for: "+url+" is: " + pageLoadTime_ms + " milliseconds"); pageLoad.reset(); return pageLoadTime_ms; }else{ return 0; } }//stopWatchStop }// sTestBasePageDeclaration




© 2015 - 2024 Weber Informatics LLC | Privacy Policy