Skip to content

Instantly share code, notes, and snippets.

View timothyshort's full-sized avatar

Tim Short timothyshort

View GitHub Profile
@timothyshort
timothyshort / Log4j.xml
Last active March 23, 2018 00:46
Logging with Log4j XML configuration file
<!--
Dependencies:
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
<appender name="fileAppender" class="org.apache.log4j.FileAppender">
@timothyshort
timothyshort / CelltoString.java
Last active July 7, 2017 21:15
CelltoString Function
// This method takes a HSSFCell value (a field in Excel file) and returns the value in String format
private static String cellToString(HSSFCell cell) {
int type = cell.getCellType();
Object result;
switch (type) {
case HSSFCell.CELL_TYPE_NUMERIC: //0
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_STRING: //1
result = cell.getStringCellValue();
@timothyshort
timothyshort / Excel.java
Last active November 20, 2021 21:59
Read an Excel file and return a double array
// Dependencies: POI | HSSF Workbook/Sheet/Row/Cell
// This method will read and return Excel data into a double array
public static String[][] get(String filename) {
String[][] dataTable = null;
File file = new File(filename);
try {
// Create a file input stream to read Excel workbook and worksheet
FileInputStream xlfile = new FileInputStream(file);
HSSFWorkbook xlwb = new HSSFWorkbook(xlfile);
HSSFSheet xlSheet = xlwb.getSheetAt(0);
@timothyshort
timothyshort / CSV.java
Last active March 30, 2022 08:19
Read a CSV file and return a List of arrays
// This method will read a CSV file and return a List of String[]
public static List<String[]> get(String filename) {
List<String[]> data = new ArrayList<String[]>();
String testRow;
try {
// Open and read the file
BufferedReader br = new BufferedReader(new FileReader(filename));
// Read data as long as it's not empty
// Parse the data by comma using .split() method
// Place into a temporary array, then add to List
@timothyshort
timothyshort / Database.java
Last active September 10, 2020 00:27
JDBC: Connect to a database and read records (SQL Server)
// Dependencies: sqljdbc4 JAR
// This method will connect to a SQL Server M.S. database and return a double array
public static String[][] get(String query) {
int numRows;
int numCols = 4;
String dataTable[][] = null;
// Define connection string
String connectionString;
/*
@timothyshort
timothyshort / Screenshot.java
Last active January 12, 2024 13:28
Take a screenshot of Selenium WebDriver
// This method takes a screenshot of the browser and saves as a file
public static void snap(WebDriver driver, String filename) {
String rootFolder = "C:/Users/SeleniumProjects/TestOutput/";
File ScreenshotFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); // Create file from the screenshot of driver
try {
FileUtils.copyFile(ScreenshotFile, new File (rootFolder + filename + ".jpg"));
} catch (IOException e) {
System.out.println("Could not save the file");
e.printStackTrace();
}
@timothyshort
timothyshort / POM.xml
Last active July 13, 2017 16:11
Maven's POM.xml file for TestNG, JUnit, Selenium
<!-- ADD PROJECT DEPENDENCIES -->
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
@timothyshort
timothyshort / POM.xml
Created July 13, 2017 17:45
Maven's POM for Cucumber
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
@timothyshort
timothyshort / DriveryFactory.java
Last active September 9, 2020 22:27
This class generates a WebDriver
/*
* This class returns a WebDriver object using 3 overloaded .get() methods:
* 1. get() - default
* 2. get(String browserType)
* 3. get(String browserType, String webURL)
*/
public class DriverFactory {
string rootFolder = "C:\\Selenium\\Software\\";
@timothyshort
timothyshort / GlobalConfigs.java
Created July 26, 2017 12:39
This function reads an XML document to define global settings
// Dependencies: javax.xml.xpath | org.w3c | org.xml
public static void setFramework(String file) {
// 1. Prepare Xpath
XPathFactory xpf = XPathFactory.newInstance();
XPath xPath = xpf.newXPath();
try {
// 2. Create XML File
InputSource inputSource = new InputSource(file);