use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.
the class FileDownloader method downloader.
public String downloader(WebElement element, String attribute) throws Exception {
//Assuming that getAttribute does some magic to return a fully qualified URL
String downloadLocation = element.getAttribute(attribute);
if (downloadLocation.trim().equals("")) {
throw new Exception("The element you have specified does not link to anything!");
}
URL downloadURL = new URL(downloadLocation);
HttpClient client = new HttpClient();
client.getParams().setCookiePolicy(CookiePolicy.RFC_2965);
client.setHostConfiguration(mimicHostConfiguration(downloadURL.getHost(), downloadURL.getPort()));
client.setState(mimicCookieState(driver.manage().getCookies()));
HttpMethod getRequest = new GetMethod(downloadURL.getPath());
FileHandler downloadedFile = new FileHandler(downloadPath + downloadURL.getFile().replaceFirst("/|\\\\", ""), true);
try {
int status = client.executeMethod(getRequest);
LOGGER.info("HTTP Status {} when getting '{}'", status, downloadURL.toExternalForm());
BufferedInputStream in = new BufferedInputStream(getRequest.getResponseBodyAsStream());
int offset = 0;
int len = 4096;
int bytes = 0;
byte[] block = new byte[len];
while ((bytes = in.read(block, offset, len)) > -1) {
downloadedFile.getWritableFileOutputStream().write(block, 0, bytes);
}
downloadedFile.close();
in.close();
LOGGER.info("File downloaded to '{}'", downloadedFile.getAbsoluteFile());
} catch (Exception Ex) {
LOGGER.error("Download failed: {}", Ex);
throw new Exception("Download failed!");
} finally {
getRequest.releaseConnection();
}
return downloadedFile.getAbsoluteFile();
}
use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.
the class FileHandlerTest method ifFileDoesNotExistItIsNotCreatedByDefault.
@Test(expected = IOException.class)
public void ifFileDoesNotExistItIsNotCreatedByDefault() throws Exception {
FileHandler myFile = new FileHandler(randomFilename());
myFile.getFile();
}
use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.
the class CompareImagesTest method updateLocalFileLocationUsingString.
@Test
public void updateLocalFileLocationUsingString() throws Exception {
driver.get("http://localhost:8081/downloadTest.html");
String localImage = this.localImage.toURI().getPath();
String invalidLocalImage = this.wrongLocalImage.toURI().getPath();
WebElement remoteImage = driver.findElement(By.id(locator));
CompareImages imageCompare = new CompareImages(invalidLocalImage, remoteImage, driver);
assertThat(imageCompare.getLocalImage(), is(equalTo(new FileHandler(invalidLocalImage).getFile())));
imageCompare.changeLocalFileTo(localImage);
assertThat(imageCompare.getLocalImage(), is(equalTo(new File(this.localImage.toURI()))));
}
use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.
the class CompareImagesTest method updateLocalFileLocationUsingFile.
@Test
public void updateLocalFileLocationUsingFile() throws Exception {
driver.get("http://localhost:8081/downloadTest.html");
File localImage = new File(this.localImage.toURI());
File invalidLocalImage = new File(this.wrongLocalImage.toURI());
WebElement remoteImage = driver.findElement(By.id(locator));
CompareImages imageCompare = new CompareImages(invalidLocalImage, remoteImage, driver);
assertThat(imageCompare.getLocalImage(), is(equalTo(new FileHandler(invalidLocalImage).getFile())));
imageCompare.changeLocalFileTo(localImage);
assertThat(imageCompare.getLocalImage(), is(equalTo(new File(this.localImage.toURI()))));
}
use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.
the class TestReports method createHTMLReport.
/**
* Create an HTML format report of the test run
*
* @throws Exception
*/
private void createHTMLReport() throws Exception {
String overallResult = "Pass";
XMLHandler testResults = new XMLHandler(new File(this.htmlTestTemplate));
// Populate the "test name" field
testResults.addTextToElement(this.testSuiteName, "//p[@id='name']/span");
// Populate the "Author(s)" field
testResults.addTextToElement(this.testSuiteAuthor, "//p[@id='author']/span");
// Populate "stories covered" field
String stories = "";
for (String story : this.associatedStories) {
stories = stories.concat(story).concat(", ");
}
stories = stories.trim().substring(0, stories.length() - 2);
testResults.addTextToElement(stories, "//p[@id='story']/span");
// Populate "total suite time" field
testResults.addTextToElement(formattedTime(this.suiteRunTime), "//p[@id='time']/span");
// Build the test results table
Iterator test = this.testData.entrySet().iterator();
int rowNumber = 0;
while (test.hasNext()) {
Map.Entry pairs = (Map.Entry) test.next();
testResults.addChildElement("tr", "//table[@id='testResults']/tbody");
rowNumber++;
TestData individualTestResults = (TestData) pairs.getValue();
testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]");
testResults.addTextToElement(individualTestResults.getTestName(), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[1]");
testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]");
testResults.addTextToElement(Integer.valueOf(individualTestResults.getFailures()).toString(), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[2]");
String result = "";
if (individualTestResults.getFailures() == 0) {
result = "Pass";
} else {
result = "Fail";
overallResult = "Fail";
}
testResults.addAttribute("class", result.toLowerCase(), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]");
testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]");
testResults.addTextToElement(result, "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[3]");
testResults.addChildElement("td", "//table[@id='testResults']/tbody/tr[" + rowNumber + "]");
testResults.addTextToElement(formattedTime(individualTestResults.getTimeTaken()), "//table[@id='testResults']/tbody/tr[" + rowNumber + "]/td[4]");
}
// Populate the "overall result" field
testResults.addTextToElement(overallResult, "//h2[@id='overallResult']/span");
testResults.addAttribute("class", overallResult.toLowerCase(), "//h2[@id='overallResult']/span");
// Print HTML results page location
logger.info("Test report available at {}", testResults.writeXMLFile(this.outputDirectory + this.testSuiteName.concat(".html")));
FileHandler cssStyle = new FileHandler(new File(this.htmlCSSFile));
cssStyle.copyFileTo(this.outputDirectory + cssStyle.getFileName());
}
Aggregations