bichromate.tools.sTestPOMFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Bichromate Show documentation
Show all versions of Bichromate Show documentation
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.
The newest version!
/*
* sTestPOMFactory.java 1.0 2016/08/01
*
* 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.tools;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.Scanner;
import javax.swing.JFileChooser;
import bichromate.core.sTestOSInformationFactory;
import bichromate.dataStore.sTestDOMObjects;
@SuppressWarnings("unused")
public class sTestPOMFactory {
private final String sTestButtonObjectTemplate = "sTestButtonObject.tmpl";
private final String sTestDropDownTemplate = "sTestDropDownObject.tmpl";
private final String sTestLinkObjectTemplate = "sTestLinkObject.tmpl";
private final String sTestTableObjectTemplate = "sTestTableObject.tmpl";
private final String sTestTextBoxObjectTemplate = "sTestTextBoxObject.tmpl";
private final String sTestRadioButtonObjectTemplate = "sTestButtonObject.tmpl";
private final String sTestCheckBoxObjectTemplate = "sTestCheckBoxObject.tmpl";
private final String sTestRangeObjectTemplate = "sTestRangeObject.tmpl";
private final String sTestTabLinkObjectTemplate = "sTestTabLinkObject.tmpl";
private String pomFilePackage = null;
private String templatesDirectory = null;
private String propertiesFileDirectory = null;
private String propertiesFileExtension = null;
private String pomFileDirectory = null;
private String pomFileVersion = null;
private String propertiesFileLocation = null;
private PrintWriter writer = null;
private List elementList = null;
private sTestOSInformationFactory path = null;
private static ResourceBundle resources;
static
{
try
{
resources = ResourceBundle.getBundle("tools.sTestPOMFactory",Locale.getDefault());
} catch (MissingResourceException mre) {
System.out.println("sTestPOMFactory.properties not found: "+mre);
System.exit(0);
}
}
/**
* This method demonstrates sTestPOMFactory().
*
Page Object Model generator. Reads in a properties file and generates the POM.java file
*
* @param outsideResources - Bichromate resource bundle
*/
public sTestPOMFactory(ResourceBundle outsideResources){
setup(outsideResources);
}
/**
* This method demonstrates sTestPOMFactory().
*
Page Object Model generator. Reads in a properties file and generates the POM.java file
*
*/
public sTestPOMFactory(){
setup(resources);
}//sTestPOMFactory
private void setup(ResourceBundle myResources){
//
// List of elements to process
//
elementList = new ArrayList();
path = new sTestOSInformationFactory();
//
// properties file location
//
propertiesFileLocation = new String(myResources.getString("sTestPOMFactory.propertiesFileLocation"));
//
// package name
//
pomFilePackage = new String(myResources.getString("sTestPOMFactory.pomFilePackage"));
//
// Directory where all templates are stored
//
templatesDirectory = new String(myResources.getString("sTestPOMFactory.templatesDirectory"));
//
// properties file directory
//
propertiesFileDirectory = new String(myResources.getString("sTestPOMFactory.propertiesFileDirectory"));
//
//file extension for properties files
//
propertiesFileExtension = new String(myResources.getString("sTestPOMFactory.propertiesFileExtension"));
//
// version of files we are creating
//
pomFileVersion = new String(myResources.getString("sTestPOMFactory.pomFileVersion"));
//
// POM file directory
//
pomFileDirectory = new String(resources.getString("sTestPOMFactory.pomFileDirectory"));
}// setup
/**
* This method demonstrates createPOM().
*
Page Object Model. Details in the pomCreator.properties file
*
* @param propertiesFile - File handle
* @param propertiesFileName name of the properties file to create the POM File
*/
public void createPOM(File propertiesFile,String propertiesFileName){
//
// Ensure the properties file exists, we need to read all the page elements
//
if (!readInPropertiesFile(propertiesFile)){
System.err.println("Error reading properties file: "+propertiesFileName);
return;
}
String newPOMFile = new String(path.buildWorkingDirectoryPath(path.setFilePath(pomFileDirectory))+path.fileSeperator()+propertiesFileName+".java");
//
// Check to see if the file already exists
//
File f = new File(newPOMFile);
if(f.exists()){
System.out.println("POM file already exisits: "+newPOMFile );
return;
}
PrintWriter pomFile = null;
try {
pomFile = new PrintWriter(newPOMFile, "UTF-8");
//
// write out copyright
//
flushCopyright(pomFile,propertiesFileName+".java");
//
// write out the IMPORTS
//
flushImports(pomFile);
//
// flush out constructor
//
flushConstructor(pomFile,propertiesFileName);
//
// Create all the functions
//
flushMethods(pomFile, propertiesFileName);
} catch (FileNotFoundException file) {
System.out.println("FileNotFoundException: "+file );
} catch (IOException io) {
System.out.println("IOException: "+io );
} finally{
System.out.println("new POM file created");
//
// Close the class
//
pomFile.write("}// "+propertiesFileName+".java Created By Bichromate, "+getCurrentDate());
pomFile.write("\r\n");
//
// Close the newly created POM File
//
pomFile.flush();
pomFile.close();
}
}// createPOM
private boolean readInPropertiesFile(File propertiesFileName){
BufferedReader br = null;
PrintWriter pomFile = null;
try {
br = new BufferedReader(new FileReader(propertiesFileName));
String line;
boolean startOfElements = false;
while ((line = br.readLine()) != null) {
System.out.println("properties File Data: "+line );
//
// Save only elements we know how to process
//
if(!line.startsWith("#")){
if(line.contains("=")){
//
// We need only the string to the left of the = sign
//
int eEnd = line.indexOf("=");
String subLine= line.substring(0 , eEnd);
//
// Check to see what type of element we have
//
if(subLine.toLowerCase().contains(sTestDOMObjects.TEXTINPUT) && !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}else if(subLine.toLowerCase().contains(sTestDOMObjects.BUTTON)&& !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}else if(subLine.toLowerCase().contains(sTestDOMObjects.LINK)&& !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}else if(subLine.toLowerCase().contains(sTestDOMObjects.TABLE)&& !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}else if(subLine.toLowerCase().contains(sTestDOMObjects.DROPDOWN)&& !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}else if(subLine.toLowerCase().contains(sTestDOMObjects.RANGE)&& !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}else if(subLine.toLowerCase().contains(sTestDOMObjects.TABLINK)&& !subLine.contains("Type")){
int endOfElement = line.indexOf("=");
if(endOfElement > 1)
elementList.add(line.substring(0, endOfElement));
}
}
}
}
br.close();
} catch (FileNotFoundException file) {
System.out.println("Properties FileNotFoundException: "+file );
return false;
} catch (IOException io) {
System.out.println("Properties File IOException: "+io );
return false;
} finally{
System.out.println("Properties file read");
}
return true;
}
/**
* This method demonstrates flushObjectMethod().
*
Writes out the DOM Object Method
*
* @param pomFile File to write all imports. Imports are taken from the imports template
* @param propertiesFileName name of the POM being created
* @param elementName name of the element to check is Enabled
* @throws FileNotFoundException - POM file not found
* @throws IOException - POM File could not be opened
*/
private void flushObjectMethod(PrintWriter pomFile, String propertiesFileName,String elementName,String TemplateFile) throws FileNotFoundException,IOException{
String line = null;
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("pomTemplates/"+TemplateFile).getFile());
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String newLine = scanner.nextLine();
if(newLine.contains("<-FUNCTION NAME->")){
StringBuilder sb = new StringBuilder(elementName);
char c = sb.charAt(0);
if (Character.isLowerCase(c))
sb.setCharAt(0, Character.toUpperCase(c));
newLine = new String(newLine.replace("<-FUNCTION NAME->", sb.toString()));
}
if(newLine.contains("<-ELEMENT NAME->")){
newLine = new String(newLine.replace("<-ELEMENT NAME->", elementName));
}
if(newLine.contains("<-CLASS NAME->")){
newLine = new String(newLine.replace("<-CLASS NAME->", propertiesFileName));
}
pomFile.write(newLine);
pomFile.write("\r\n");
}
scanner.close();
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}//flushIsEnabledMethod
/**
* This method demonstrates flushMethods().
*
Write all the element functions
*
* @param pomFile File to write all imports. Imports are taken from the imports template
* @param propertiesFileName name of the POM being created
* @throws FileNotFoundException - POM file not found
* @throws IOException - POM File could not be opened
*/
private void flushMethods(PrintWriter pomFile, String propertiesFileName) throws FileNotFoundException,IOException{
for(int x = 0; x < elementList.size(); x++){
String elementName = new String(elementList.get(x));
if(elementName.contains(sTestDOMObjects.TEXTINPUT)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestTextBoxObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.BUTTON)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestButtonObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.DROPDOWN)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestDropDownTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.LINK)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestLinkObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.TABLE)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestTableObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.RADIOBUTTON)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestTableObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.CHECKBOX)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestTableObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.RANGE)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestRangeObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
if(elementName.contains(sTestDOMObjects.TABLINK)){
flushObjectMethod(pomFile,propertiesFileName, elementName,sTestTabLinkObjectTemplate);
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}
}
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
}// flushMethods
/**
* This method demonstrates flushConstructor().
*
Write to the new POM File
*
* @param pomFile File to write all imports. Imports are taken from the imports template
* @param propertiesFileName - name of the pom properties file
* @throws FileNotFoundException - POM file not found
* @throws IOException - POM File could not be opened
*/
private void flushConstructor(PrintWriter pomFile, String propertiesFileName) throws FileNotFoundException,IOException{
String line = null;
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("pomTemplates/constructor.tmpl").getFile());
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String newLine = scanner.nextLine();
if(newLine.contains("<-FILE NAME->")){
newLine = new String(newLine.replace("<-FILE NAME->", propertiesFileName));
}
if(newLine.contains("")){
newLine = new String(newLine.replace("", propertiesFileLocation));
}
if(newLine.contains("<-ELEMENTS->")){
pomFile.write("\r\n\t");
pomFile.write ("public String pageTitle = null;");
pomFile.write("\r\n\t");
pomFile.write ("public String pageURL = null;");
pomFile.write("\r\n\t");
for(int x = 0; x < elementList.size(); x++){
String elementName = new String(elementList.get(x));
System.out.println("element name to process: "+ elementName);
pomFile.write ("private String "+elementName+" = \""+elementName+"\";");
pomFile.write("\r\n\t");
}
//
// Objects
//
pomFile.write("//\n");
pomFile.write("\t// Objects\n");
//pomFile.write("\t//\n");
for(int x = 0; x < elementList.size(); x++){
String elementName = new String(elementList.get(x));
System.out.println("element name to process: "+ elementName);
if(elementName.toLowerCase().contains("textinput")){
pomFile.write ("\tprivate sTestTextBoxObject "+elementName+"Object = null;");
}else if(elementName.toLowerCase().contains("button")){
pomFile.write ("\tprivate sTestButtonObject "+elementName+"Object = null;");
}else if(elementName.contains("table")){
pomFile.write ("\tprivate sTestTableObject "+elementName+"Object = null;");
}else if(elementName.toLowerCase().contains("dropdown")){
pomFile.write ("\tprivate sTestDropDownList "+elementName+"Object = null;");
}else if(elementName.toLowerCase().contains("link")){
pomFile.write ("\tprivate sTestLinkObject "+elementName+"Object = null;");
}else if(elementName.toLowerCase().contains("range")){
pomFile.write ("\tprivate sTestRangeObject "+elementName+"Object = null;");
}
pomFile.write("\r\n\t");
}
pomFile.write("");
pomFile.write("\r\n");
pomFile.write("");
pomFile.write("\r\n");
//
// lines have been written
//
newLine = null;
}
if(null != newLine){
pomFile.write(newLine);
pomFile.write("\r\n");
}
}
pomFile.write ("");
pomFile.write("\r\n");
pomFile.write ("");
pomFile.write("\r\n");
scanner.close();
}
/**
* This method demonstrates flushImports().
*
Write all imports to the new POM File
*
* @param pomFile File to write all imports. Imports are taken from the imports template
* @throws FileNotFoundException - POM file not found
* @throws IOException - POM File could not be opened
*/
private void flushImports(PrintWriter pomFile) throws FileNotFoundException,IOException{
String line = null;
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("pomTemplates/imports.tmpl").getFile());
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String newLine = scanner.nextLine();
pomFile.write (newLine);
pomFile.write("\r\n");
}
pomFile.write ("");
pomFile.write("\r\n");
pomFile.write ("");
pomFile.write("\r\n");
scanner.close();
}//flushImports
/**
* This method demonstrates flushCopyright().
*
Write the copy right to the new POM File
*
* @param pomFile File to write the copyright info. copyright is taken from the copyright template
* @param fileName name of the mew POM File
* @throws FileNotFoundException - POM file not found
* @throws IOException - POM File could not be opened
*/
private void flushCopyright(PrintWriter pomFile,String filename) throws FileNotFoundException,IOException{
//
// Open copyright template and flush to new POM file
//
String line = null;
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("pomTemplates/copyright.tmpl").getFile());
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
line = scanner.nextLine();
if(line.contains(" * <-FILENAME-> <-VERSION-> <-DATE->")){
pomFile.write (" * "+filename+ ", Version " +pomFileVersion +", Created "+ getCurrentDate() );
pomFile.write("\r\n");
}else{
pomFile.write (line);
pomFile.write("\r\n");
}
}
pomFile.write ("");
pomFile.write("\r\n");
pomFile.write ("");
pomFile.write("\r\n");
pomFile.write("//TODO enable the package");
pomFile.write("\r\n");
pomFile.write ("// package "+pomFilePackage);
pomFile.write("\r\n");
pomFile.write ("");
pomFile.write("\r\n");
pomFile.write ("");
pomFile.write("\r\n");
scanner.close();
}//flushCopyright
/**
* This method Demonstrates getCurrentDate().
* This function returns the current date in the following format yyyy-MM-dd-HH-mm-ss properties file
* @return String date yyyy-MM-dd-HH-mm-ss
*/
private String getCurrentDate(){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date date = new Date();
String currentDate = new String(dateFormat.format(date));
return currentDate;
}//getCurrentDate
/**
* This method demonstrates pomGenerator().
*
Page Object Model
*
*/
public void selfTest(){
/*
// a jframe here isn't strictly necessary, but it makes the example a little more real
JFrame frame = new JFrame("InputDialog Example #1");
// prompt the user to enter their name
String name = JOptionPane.showInputDialog(frame, "Properties File Name, no extension!!!");
*/
JFileChooser fileChooser = new JFileChooser();
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
String name = new String(file.getName());
name = new String(name.replaceFirst(".properties",""));
System.out.println("pom file to create: "+name );
createPOM(file,name);
} else{
System.out.println("Cancelled file selection" );
}
}//selfTest
/**
* This method demonstrates pomGenerator().
*
Page Object Model
*
* @param propFile - file handle
* @param propertiesFileName - name of the properties file
*/
public void selfTest(File propFile,String propertiesFileName){
String name = new String(propertiesFileName);
System.out.println("pom file to create: "+name );
createPOM(propFile,name);
}//selfTest
//
// Inner class for testing on the command line
//
public static class Test
{
public static void main(final String[] args){
sTestPOMFactory pom = null;
pom = new sTestPOMFactory();
if(pom != null){
// pom.selfTest("pomCreator");
pom.selfTest();
}
} // end Main
} // end Inner class Test
}//sTestPOMFactory