Search in sources :

Example 6 with FileHandler

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();
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URL(java.net.URL) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 7 with FileHandler

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();
}
Also used : FileHandler(com.lazerycode.ebselen.handlers.FileHandler) Test(org.junit.Test)

Example 8 with FileHandler

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()))));
}
Also used : WebElement(org.openqa.selenium.WebElement) File(java.io.File) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 9 with FileHandler

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()))));
}
Also used : WebElement(org.openqa.selenium.WebElement) File(java.io.File) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 10 with FileHandler

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());
}
Also used : XMLHandler(com.lazerycode.ebselen.handlers.XMLHandler) File(java.io.File) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Aggregations

FileHandler (com.lazerycode.ebselen.handlers.FileHandler)11 File (java.io.File)4 XMLHandler (com.lazerycode.ebselen.handlers.XMLHandler)2 CleanerProperties (org.htmlcleaner.CleanerProperties)2 Test (org.junit.Test)2 WebElement (org.openqa.selenium.WebElement)2 StringWriter (java.io.StringWriter)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 VelocityEngine (org.apache.velocity.app.VelocityEngine)1 HtmlCleaner (org.htmlcleaner.HtmlCleaner)1 PrettyXmlSerializer (org.htmlcleaner.PrettyXmlSerializer)1 TagNode (org.htmlcleaner.TagNode)1