Summary Table

Categories Total Count
PII 0
URL 0
DNS 0
EKL 0
IP 0
PORT 0
VsID 0
CF 0
AI 0
VPD 0
PL 0
Other 0

File Content

package automation;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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 po.CommonPO;
import po.LoginPO;



/***
*
* @author KYibrie, PMohite, CMota,
* @version 2.0
*/
public class SharedCodeClass extends TestSuite {
static BufferedWriter bw;
private static LoginPO login;
private static CommonPO common;


/**
* This waits for the page to load to continue with the proceeding task.
*
*/
public static void waitForLoad(WebDriver driver) {
ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
}
};
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(pageLoadCondition);
}

/**
* This finds all elements by tag.
*
* @param tag
* String
* @param thTitles
* List of String
*/
public static void findAllEllementsTag(String tag, List<String> text) {
List<WebElement> allElements = driver.findElements(By.tagName(tag));
Iterator<WebElement> itElement = allElements.iterator();
Iterator<String> itThName = text.iterator();

while (itElement.hasNext() && itThName.hasNext()) {
String elementText = itElement.next().getText();
if (elementText.equals("")) {
continue;
}
String testText = itThName.next();
System.out.println(testText + "-------" + elementText);
Assert.assertTrue(elementText.equalsIgnoreCase(testText));
}
}

/**
* This finds an element by class. It takes on one class at a time
*
* @param name
* String
*/
public static void findElementClass(String name) {
List<WebElement> allElements = driver.findElements(By.className(name));
for (WebElement element : allElements) {
Assert.assertTrue(driver.findElement(By.className(name)).getAttribute("innerHTML") != null);
}
}

/**
* This finds an element by ID. It takes on one ID at a time
*
* @param name
* String
*/
public static void findElementId(String name) {

List<WebElement> allElements = driver.findElements(By.id(name));
if (allElements == null || allElements.isEmpty()) {
Assert.fail("There is no element that has an \"id\" of \"" + name + "\"");
}

for (WebElement element : allElements) {
// System.out.println("findElementId : Assert True : name = " +
// name);
Assert.assertTrue(driver.findElement(By.id(name)).getAttribute("innerHTML") != null);
}
}

/**
* Assert class's, one class or multiple
*
* @param className
* List of String
*/
public static void assertByClass(ArrayList<String> className) {
for (String id : className) {
for (WebElement element : driver.findElements(By.className(id))) {
Assert.assertTrue(driver.findElement(By.className(id)).getAttribute("innerHTML") != null);
}
}
}

/**
* Assert class's, one ID or multiple
*
* @param name
* List of String
*/
public static void assertById(ArrayList<String> name) {
for (String id : name) {
// System.out.println(driver.findElements(By.id(id)));
for (WebElement element : driver.findElements(By.id(id))) {
Assert.assertTrue(driver.findElement(By.id(id)).getAttribute("innerHTML") != null);
}
}

}

/**
* Assert text inside of an ID
*
* @param elements
* Map of elements
*/
public static void assertByTextById(Map<String, String> elements) {
for (Map.Entry<String, String> id : elements.entrySet()) {
for (WebElement element : driver.findElements(By.id(id.getKey()))) {
// System.out.printf("id is :%s ++++++++++ %s +++++++++ value
// is: %s \n", id,element.getText() ,id.getValue());
if (element.getText().trim().length() > 0) {

Assert.assertTrue(element.getText().equals(id.getValue()));
}
}
}
}

/**
* Assert text inside of an Class
*
* @param elements
* Map of elements
*/
public static void assertByTextByClass(Map<String, String> elements) {
for (Map.Entry<String, String> className : elements.entrySet()) {
for (WebElement element : driver.findElements(By.className(className.getKey()))) {
// System.out.println("expected : " +element.getText() + " real
// : "+className.getValue());
Assert.assertTrue(element.getText().equals(className.getValue()));
}
}
}

/**
* Give an ID and it will fill in the text
*
* @param id
* String
* @param text
* Text to fill
*/
public static void fillElementById(String id, String text) {
// for (Map.Entry<String, String> id : elements.entrySet()) {
driver.findElement(By.id(id)).sendKeys(text);
// }
}

/**
* Give an ID and it will fill in the text
*
* @param id
* String
* @param text
* Text to fill
*/
public static void fillAllElementById(String id, String text) throws Exception {
List<WebElement> allElements = driver.findElements(By.id(id));
if (allElements == null || allElements.isEmpty()) {
Assert.fail("There is no element that has an \"id\" of \"" + id + "\"");
}

for (WebElement element : allElements) {
element.sendKeys(text);
}
}

/**
* Give a Class and it will fill in the text
*
* @param className
* String
* @param text
* Text to fill
*/
public static void fillElementByClassName(String className, String text) {
driver.findElement(By.className(className)).sendKeys(text);
}

/**
* Give multiple class names and it will fill in the text
*
* @param className
* String
* @param text
* Text to fill
*/
public static void fillAllElemenstByClassName(String className, String text) throws Exception {
List<WebElement> allElements = driver.findElements(By.className(className));
for (WebElement element : allElements) {
// System.out.println(element);
element.sendKeys(text);
}
}

/**
* Give multiple class names and it will fill in the text
*
* @param className
* String
* @param text
* Text to fill
*/
public static void clickById(String id) {
driver.findElement(By.id(id)).click();
}

public static void setDropDown(String id, String text,String buttonId) throws InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 15);
Select dropdown = new Select(driver.findElement(By.id(id)));
wait.until(ExpectedConditions.visibilityOfNestedElementsLocatedBy(By.id(id), By.tagName("option")));
dropdown.selectByVisibleText(text);
if(buttonId!=null&&!buttonId.isEmpty())
driver.findElement(By.id(buttonId)).click();
}

/**
* Give an attribute to search for and the value within it
*
* @param attribute
* String attribute to search for example id, alt, value
* @param value
* String of the attribute contents
*/
public static void universalSelector(String attribute, String value) {
waitForLoad(driver);
driver.findElement(By.cssSelector("" + attribute + "[alt=" + value.trim() + "]")).click();

}

/**
* Create a report result.txt file
*
*/
public static void createReport() {
try {
// Create new file
String fileName = "Report.txt";
String path = "report";
File file = new File(path + "/" + fileName);
Date dNow = new Date();
SimpleDateFormat ft = new SimpleDateFormat("hh:mm:ss a zzz 'on' MMMM.dd.yy");
// If file doesn't exists, then create it
if (!file.exists()) {
file.createNewFile();
}

FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
bw = new BufferedWriter(fw);
bw.write("\r\n************");
bw.write("Test started at " + ft.format(dNow) + "****************************" + "\r\n");

} catch (Exception e) {
System.out.println(e);
}
}

/**
* Add test results to the result.txt file
*
* @throws IOException
*
*/
public static void addReport(String result) throws IOException {
// Write in file

bw.write(result + "\r\n");
// bw.write("***********************************************************"
// + "\r\n");
}

/**
* Close connection
*
* @throws IOException
*/
public static void closeReport() throws IOException {
bw.write("***********************************************************" + "\r\n");
bw.close();
}

// Print to console and report if test case PASSED
public static void assertPassed(String testName) throws IOException {
System.out.println("------------------------------------------------------------------");
System.out.println("----------Verify a " + testName + " is passed");
System.out.println("------------------------------------------------------------------");
addReport("Verify a " + testName + " is passed");
}

// Print to console and report if test case FAILED
public static void assertFailed(String testName, Throwable e) throws IOException {
System.out.println("------------------------------------------------------------------");
System.out.println("!!!!!!!---Verify a " + testName + " is FAILED with a error: " + e);
System.out.println("------------------------------------------------------------------");
SharedCodeClass.addReport("!!!!!!!!!!!!!Verify a " + testName + " is FAILED with a error: " + e.getMessage());
Assert.assertFalse(true);
}


public static void navigateToFPS(){
login = new LoginPO(driver);
common = new CommonPO();

login.submitBtnClick();
common.feePaymentBtnClick();
}

}