Could not upload file by UploadFile.uploadFileUsingRobot

I’m trying to upload a file using katalon-studio-uploadfile-plugin but unsuccessful.

The error as below was generate:
Reason: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.kms.katalon.core.webui.driver.SmartWaitWebDriver@6f6a7463' with class 'com.kms.katalon.core.webui.driver.SmartWaitWebDriver' to class 'org.openqa.selenium.remote.RemoteWebDriver' at com.kms.katalon.keyword.uploadfile.UploadFile.uploadFileUsingRobot(UploadFile.groovy:41) at com.kms.katalon.keyword.uploadfile.UploadFile.invokeMethod(UploadFile.groovy) at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50) at Draft 3.run(Draft 3:18) at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194) at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119) at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337) at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328) at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307) at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299) at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233) at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114) at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105) at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source) at TempTestCase1587639419859.run(TempTestCase1587639419859.groovy:23)
Please help advise!

The object from which we can click to choose files:
<button type="button" class="btn btn-white btn--big">choose file to upload</button>

I searched this forum and found a mysterious conversation about “SmartAWaitWebDriver” between @ThanhTo and @Russ_Thomas.

But I have no input to the original issue.

Hi @vnduc74

The code of that plugin does not seem to be compatible, please create a custom keyword with the content from this original UploadFile class.

But replacing line 41 with

Capabilities caps = ((SmartWaitWebDriver) driver).getCapabilities()

Please try to see if that works. We will update the plugin.

1 Like

I’ve separated file upalod logic using Robot into separate class, and made methods static. In method fileAttachmentUsingRobot to pass path to image you want to be uploaded, i.e full system path to image.

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

/**
 * Created by ypolshchykau on 30.01.2015.
 */
public class FileAttacherRobotImplementation {
    public final static Logger log = LoggerFactory.getLogger(FileAttacherRobotImplementation.class);

    /**
     * this method implements file attachment using Robot mechanism
     *
     * @param filePathToImage
     */

    public static void fileAttachmentUsingRobot(String filePathToImage) {
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
        setClipboardData(filePathToImage);
        robot.delay(1000);

        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
    }

    /**
     * method for copying file in order to attach file in File open window
     *
     * @param str
     */
    public static void setClipboardData(String str) {
        StringSelection stringSelection = new StringSelection(str);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
    }

}

Hope this helps you.

2 Likes

Thanks all, this issue is fixed.
But seems that it doesn’t work when minimizing windows, we could not setClipboardData in this case. Have you any solutions for it?

This might work for you…