/* ---------------------------------------------------------------------------------- */
package myWeekdays;
import com.thoughtworks.selenium.*;
import org.testng.annotations.*;
import org.testng.Reporter;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class MyDay {
private Selenium selenium;
private Boolean assertChecker;
private String errorShot = System.getProperty("user.dir") + "\\SomethingIsWrong\\";
private String inputFile;
protected static HttpCommandProcessor proc;
public void setInputFile(String inputFile) {
this.inputFile = inputFile;
}
public String getDateTime() {
DateFormat dateFormat = new SimpleDateFormat("MM.dd.yyyy");
Date date = new Date();
return dateFormat.format(date);
}
public String getLunchTime() {
DateFormat dateFormat = new SimpleDateFormat("hh:mm");
Date date = new Date();
return dateFormat.format(date);
}
public void assertElementPresent (String elementName, String description){
assertChecker = selenium.isElementPresent(elementName);
if(assertChecker.equals(false)) {
String fileName = description + "_" + getDateTime() + ".png";
selenium.captureEntirePageScreenshot(errorShot + fileName, "");
}
return;
}
@BeforeClass
public void setup() throws IOException {
setInputFile(System.getProperty("user.dir") + "\\WhatINeed.xls");
File inputWorkbook = new File(inputFile);
Workbook w;
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet config = w.getSheet(0);
String myDay = config.getCell(1, 0).getContents();
String happyThoughtsAndPatience = config.getCell(1, 1).getContents();
proc = new HttpCommandProcessor("localhost", 4444, happyThoughtsAndPatience, myDay);
selenium = new DefaultSelenium(proc);
selenium.start();
selenium.windowFocus();
selenium.windowMaximize();
}
catch (BiffException e) {
Reporter.log("I am not in a good mood!");
e.printStackTrace();
}
catch (Exception e) {
Reporter.log("My day is a mess!");
e.printStackTrace();
}
}
@Test public void myMorning() throws IOException {
setInputFile(System.getProperty("user.dir") + "\\WhatINeed.xls");
File inputWorkbook = new File(inputFile);
Workbook w;
String[] idleTime = { "30000" };
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet config = w.getSheet(0);
Sheet sheet = w.getSheet(1);
String myDay = config.getCell(1, 0).getContents();
String speed = config.getCell(1, 2).getContents();
String timeOut = config.getCell(1, 3).getContents();
String waitTime = config.getCell(1, 4).getContents();
String breakfast = sheet.getCell(1, 1).getContents();
String outfit = sheet.getCell(1, 2).getContents();
selenium.setSpeed(speed);
selenium.setTimeout(timeOut);
selenium.open(myDay);
selenium.waitForPageToLoad(waitTime);
selenium.click("link=Wake Up Ring");
selenium.waitForPageToLoad(waitTime);
proc.doCommand("watchA4jRequests", null);
selenium.click("link=Morning News");
proc.doCommand("waitForA4jRequest", idleTime);
selenium.type("whatToEat", breakfast);
selenium.click("link=Take a shower");
selenium.waitForPageToLoad(waitTime);
selenium.type("whatToWear", outfit);
selenium.click("link=Prepare to leave home");
selenium.waitForPageToLoad(waitTime);
assertElementPresent("IsEverythingOkay", "NeedToDoWhatIForgotToDo");
selenium.click("link=Leave home");
selenium.waitForPageToLoad(waitTime);
selenium.click("link=Ride to office");
selenium.waitForPageToLoad(waitTime);
assertElementPresent("IsTheRideAndTrafficOkay", "NeedToInformTeamForComingInLate");
}
catch (BiffException e) {
Reporter.log("Nothing to eat. Nothing to wear.");
e.printStackTrace();
}
catch (Exception e) {
Reporter.log("Cannot go to work. Need to inform team.");
e.printStackTrace();
}
}
@Test (alwaysRun=false, dependsOnMethods = { "myMorning" })
public void atWork() throws IOException {
setInputFile(System.getProperty("user.dir") + "\\WhatINeed.xls");
File inputWorkbook = new File(inputFile);
Workbook w;
String[] idleTime = { "30000" };
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet config = w.getSheet(0);
Sheet sheet = w.getSheet(2);
String waitTime = config.getCell(1, 4).getContents();
int hoursAtWork = 0;
String softwareTestingAndMeetings = sheet.getCell(1, 2).getContents();
String osgLunchOrSMXLunch = sheet.getCell(1, 3).getContents();
String thingsToDoWhileIdle = sheet.getCell(1, 3).getContents();
String currentTime;
String exactLunch = "12:00";
selenium.click("link=Open up all needed files and things to do");
selenium.waitForPageToLoad(waitTime);
proc.doCommand("watchA4jRequests", null);
selenium.click("link=Chat");
proc.doCommand("waitForA4jRequest", idleTime);
selenium.click("link=Eat Snacks");
proc.doCommand("waitForA4jRequest", idleTime);
while(hoursAtWork < 8){
selenium.type("workWorkWork", softwareTestingAndMeetings);
assertElementPresent("isWorkOkay", "ReportThatWorkIsNotOkay");
if(assertChecker.equals(false)){
selenium.type("WhatToDo", thingsToDoWhileIdle);
}
currentTime = getLunchTime();
if(currentTime.equals(exactLunch)){
selenium.type("Lunch", osgLunchOrSMXLunch);
}
hoursAtWork++;
}
selenium.click("link=Logout");
selenium.waitForPageToLoad(waitTime);
}
catch (BiffException e) {
Reporter.log("Running out of Tasks.");
e.printStackTrace();
}
catch (Exception e) {
Reporter.log("Something is wrong. Maybe it is a sign to.... =)");
e.printStackTrace();
}
}
@Test (alwaysRun=false, dependsOnMethods = { "atWork" })
public void afterWork() throws IOException {
setInputFile(System.getProperty("user.dir") + "\\WhatINeed.xls");
File inputWorkbook = new File(inputFile);
Workbook w;
String[] idleTime = { "30000" };
try {
w = Workbook.getWorkbook(inputWorkbook);
Sheet config = w.getSheet(0);
Sheet sheet = w.getSheet(3);
String waitTime = config.getCell(1, 4).getContents();
String gimmiks = sheet.getCell(1, 2).getContents();
selenium.click("link=Are there gimmiks for the night?");
selenium.waitForPageToLoad(waitTime);
assertElementPresent("GimmiksSet", "JustGoHome");
if(assertChecker.equals(true)){
selenium.type("nightOut", gimmiks);
}
selenium.click("link=Go home");
selenium.waitForPageToLoad(waitTime);
proc.doCommand("watchA4jRequests", null);
selenium.click("link=Read");
proc.doCommand("waitForA4jRequest", idleTime);
selenium.click("link=Midnight Snacks");
proc.doCommand("waitForA4jRequest", idleTime);
selenium.click("link=Watch TV");
proc.doCommand("waitForA4jRequest", idleTime);
selenium.click("link=Sleep");
}
catch (BiffException e) {
Reporter.log("Gimmik planning failed.");
e.printStackTrace();
}
catch (Exception e) {
Reporter.log("Unable to go home! Stranded? or BASAG from the gimmik? =)");
e.printStackTrace();
}
}
@AfterClass
public void tearDown() {
selenium.stop();
}
}
/* ---------------------------------------------------------------------------------- */
Java on!
~Lyndon Jason