Search in sources :

Example 1 with FileHandler

use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.

the class IDEToEbselen method createFileList.

/**
     * Traverse directory and create a list of all the files that need to be converted
     */
public void createFileList() throws Exception {
    FileHandler checkFileType = new FileHandler(startPoint);
    if (!checkFileType.getFile().isDirectory()) {
        LOGGER.error("The file '{}' is not a directory, will only try to convert this file...", checkFileType.getFileName());
        if (checkFileType.getFile().isFile()) {
            fileList.add(checkFileType.getFilePath() + checkFileType.getFileName());
        }
        checkFileType.close();
        return;
    }
    checkFileType.close();
    LOGGER.error("Scanning all files in the directory '{}'...", checkFileType.getFileName());
    ArrayList scanList = new ArrayList();
    scanList.add(startPoint);
    while (scanList.size() > 0) {
        FileHandler directoryToScan = new FileHandler(scanList.get(0).toString());
        File[] listOfFiles = directoryToScan.getFile().listFiles();
        for (File currentFile : listOfFiles) {
            FileHandler examineFile = new FileHandler(currentFile.getAbsolutePath());
            if (!examineFile.getExtension().equals("java")) {
                if (examineFile.getFile().isFile()) {
                    fileList.add(examineFile.getFilePath() + examineFile.getFileName());
                } else if (examineFile.getFile().isDirectory()) {
                    if (walkTree) {
                        scanList.add(currentFile);
                    }
                }
            }
            examineFile.close();
        }
        directoryToScan.close();
        scanList.remove(0);
    }
}
Also used : ArrayList(java.util.ArrayList) File(java.io.File) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 2 with FileHandler

use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.

the class IDEToEbselen method generateJavaFile.

/**
     * Writes Sky Selenium format test code into a Java file ready for tests to be run
     *
     * @param name - Name of the test
     * @throws Exception
     */
public void generateJavaFile(String name) throws Exception {
    Properties props = new Properties();
    props.setProperty("resource.loader", "class");
    props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    props.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
    VelocityEngine ve = new VelocityEngine(props);
    VelocityContext context = new VelocityContext();
    context.put("template", name);
    context.put("templateclass", name + ".class");
    context.put("testname", name);
    context.put("testdata", testCode);
    Template ebselenTemplate = ve.getTemplate(ebselenTestTemplate);
    FileHandler convertedFile = new FileHandler(conversionLocation + File.separator + name + ".java", true);
    StringWriter writer = new StringWriter();
    ebselenTemplate.merge(context, writer);
    convertedFile.write(writer.toString());
    convertedFile.close();
    LOGGER.info("Selenium IDE test converted and saved as '" + convertedFile.getFilePath() + convertedFile.getFileName() + "'");
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Properties(java.util.Properties) CleanerProperties(org.htmlcleaner.CleanerProperties) Template(org.apache.velocity.Template) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 3 with FileHandler

use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.

the class IDEToEbselen method convertToXML.

/**
     * Cleans the relevant file and generates a valid XML file ready for processing to Sel 2 java File.
     *
     * @param absoluteFilename - name of the file to convert.
     * @return String - location of the converted file.
     */
public String convertToXML(String absoluteFilename) throws Exception {
    FileHandler fromSelIDE = new FileHandler(absoluteFilename);
    FileHandler toXML = new FileHandler(System.getProperty("java.io.tmpdir") + File.separator + fromSelIDE.getFileName() + ".xml", true);
    if (fromSelIDE.getFile().isDirectory()) {
        LOGGER.error("Cannot convert directory {} into a Selenium Test!", fromSelIDE.getFileName());
        return null;
    }
    //Clean up html so that we can read it as XML properly
    HtmlCleaner cleaner = new HtmlCleaner();
    CleanerProperties XMLPrefs = cleaner.getProperties();
    XMLPrefs.setUseEmptyElementTags(true);
    XMLPrefs.setTranslateSpecialEntities(true);
    XMLPrefs.setTransResCharsToNCR(true);
    XMLPrefs.setOmitComments(true);
    XMLPrefs.setOmitComments(true);
    XMLPrefs.setOmitDoctypeDeclaration(true);
    XMLPrefs.setNamespacesAware(false);
    TagNode tagNode = new HtmlCleaner(XMLPrefs).clean(fromSelIDE.getFile());
    new PrettyXmlSerializer(XMLPrefs).writeToStream(tagNode, toXML.getWritableFileOutputStream(), "utf-8");
    toXML.close();
    return toXML.getAbsoluteFile();
}
Also used : CleanerProperties(org.htmlcleaner.CleanerProperties) PrettyXmlSerializer(org.htmlcleaner.PrettyXmlSerializer) HtmlCleaner(org.htmlcleaner.HtmlCleaner) TagNode(org.htmlcleaner.TagNode) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 4 with FileHandler

use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.

the class IDEToEbselen method generateTestCode.

/**
     * Reads in a sky.sns.selenium IDE file and creates Sky Selenium format test code
     *
     * @param filename - Selenium IDE file to convert
     * @return Name of the Selenium IDE file
     * @throws Exception
     */
public String generateTestCode(String filename) throws Exception {
    FileHandler convertFrom = new FileHandler(convertToXML(filename));
    XMLHandler seleniumXMLFile = new XMLHandler(convertFrom.getFile());
    int commandCount = seleniumXMLFile.performXPathQueryReturnInteger("count(/html/body/table/tbody/tr)");
    for (int i = 1; i <= commandCount; i++) {
        String command = "";
        String target = "";
        String value = "";
        try {
            command = seleniumXMLFile.performXPathQueryReturnString("//table/tbody/tr[" + i + "]/td[1]");
            target = seleniumXMLFile.performXPathQueryReturnString("/html/body/table/tbody/tr[" + i + "]/td[2]");
            value = seleniumXMLFile.performXPathQueryReturnString("/html/body/table/tbody/tr[" + i + "]/td[3]");
        } catch (Exception Ex) {
            LOGGER.warn("Invalid command '{}' found", command);
        }
        addToTestCode(codeGenerator.convertCommandToEbselenCode(command, target, value));
    }
    convertFrom.close();
    return convertFrom.getFileName().split("\\.")[0];
}
Also used : XMLHandler(com.lazerycode.ebselen.handlers.XMLHandler) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Example 5 with FileHandler

use of com.lazerycode.ebselen.handlers.FileHandler in project Ebselen by Ardesco.

the class CompareImages method compareImages.

public Boolean compareImages() throws Exception {
    FileDownloader downloadRemoteImage = new FileDownloader(driver);
    FileHandler downloadedImage = new FileHandler(downloadRemoteImage.imageDownloader(this.remoteImageObject));
    if (generateMessageDigest(this.localFileObject.getFile()).equals(generateMessageDigest(downloadedImage.getFile()))) {
        return true;
    }
    return false;
}
Also used : 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