Search in sources :

Example 1 with RobotException

use of com.axway.ats.uiengine.exceptions.RobotException in project ats-framework by Axway.

the class RealHtmlFileBrowse method setValueUsingNativeDialog.

/**
     * This method allows you to type in dialog windows. This option is available only for Windows OS
     *
     * @param path add the location of the file, you want to type in the dialog window
     * @throws Exception
     */
@PublicAtsApi
public void setValueUsingNativeDialog(String path) throws Exception {
    if (!OperatingSystemType.getCurrentOsType().isWindows()) {
        throw new RuntimeException("This method is only available for Windows machines!");
    }
    // check if the file exist
    if (!new File(path).exists()) {
        throw new FileNotFoundException("File path \"" + path + "\" is wrong or does not exist!");
    }
    // ats_file_upload.exe location
    String uploadFileDestination = System.getProperty("user.dir") + "\\ats_file_upload.exe";
    // native window name
    String windowName;
    log.info("Using native " + path + " to work with native browser dialogs");
    // check if the ats_file_upload.exe file is already created
    if (!new File(uploadFileDestination).exists()) {
        OutputStream os = null;
        InputStream is = null;
        try {
            // get the ats_file_upload.exe file, located in the ats-uiengine.jar
            is = getClass().getClassLoader().getResourceAsStream("binaries/ats_file_upload.exe");
            if (is == null) {
                throw new FileNotFoundException("The 'ats_file_upload.exe' file is not found in ats-uiengine.jar!");
            }
            File uploadFile = new File(uploadFileDestination);
            os = new FileOutputStream(uploadFile);
            IOUtils.copy(is, os);
        } finally {
            IoUtils.closeStream(is);
            IoUtils.closeStream(os);
        }
    }
    if (webDriver instanceof FirefoxDriver) {
        windowName = " \"File Upload\" ";
    } else if (webDriver instanceof ChromeDriver) {
        windowName = " \"Open\" ";
    } else {
        throw new RobotException("Not Implemented for your browser! Currently Firefox and " + "Chrome are supported.");
    }
    // add the browse button properties
    ((AbstractRealBrowserDriver) super.getUiDriver()).getHtmlEngine().getElement(properties).click();
    // run the ats_file_upload.exe file
    IProcessExecutor proc = new LocalProcessExecutor(HostUtils.LOCAL_HOST_IPv4, uploadFileDestination + windowName + path);
    proc.execute();
    // check if there is any error, while executing the ats_file_upload.exe file
    if (proc.getExitCode() != 0) {
        log.error("AutoIT process for native browser interaction failed with exit code: " + proc.getExitCode() + ";");
        log.error("Output stream data: " + proc.getStandardOutput() + ";");
        log.error("Error stream data: " + proc.getErrorOutput());
        throw new RobotException("AutoIT process for native browser interaction failed.");
    }
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) RobotException(com.axway.ats.uiengine.exceptions.RobotException) AbstractRealBrowserDriver(com.axway.ats.uiengine.AbstractRealBrowserDriver) FirefoxDriver(org.openqa.selenium.firefox.FirefoxDriver) FileOutputStream(java.io.FileOutputStream) ChromeDriver(org.openqa.selenium.chrome.ChromeDriver) LocalProcessExecutor(com.axway.ats.core.process.LocalProcessExecutor) File(java.io.File) IProcessExecutor(com.axway.ats.core.process.model.IProcessExecutor) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 LocalProcessExecutor (com.axway.ats.core.process.LocalProcessExecutor)1 IProcessExecutor (com.axway.ats.core.process.model.IProcessExecutor)1 AbstractRealBrowserDriver (com.axway.ats.uiengine.AbstractRealBrowserDriver)1 RobotException (com.axway.ats.uiengine.exceptions.RobotException)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ChromeDriver (org.openqa.selenium.chrome.ChromeDriver)1 FirefoxDriver (org.openqa.selenium.firefox.FirefoxDriver)1