[Tip] Here's how I select a file to upload in Windows


#1

Last night, I spent quite a bit of time trying to upload a .zip file in Chrome once the Windows Native Dialog (Open) is displayed. Fortunately, I was able to find a solution that I hope is simple to follow. Here’s the code:

import java.awt.Robot as Robot
import java.awt.Toolkit as Toolkit
import java.awt.datatransfer.StringSelection as StringSelection
import java.awt.event.KeyEvent as KeyEvent

//this is the button you click to launch the upload window (ie: native Open window)

WebUI.click(findTestObject('Object Repository/Page_Add New Post  Sandbox  WordPress/button_Choose your zip file'))

String text = 'C:\\Users\\myuser\\Documents\\Dropbox\\some files\\another sub folder\\myzip.zip'

StringSelection ss = new StringSelection(text)

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null)

Robot robot = new Robot()

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)

You should only need to change these 2 lines:

WebUI.click(findTestObject('Object Repository/Page_Add New Post  Sandbox  WordPress/button_Choose your zip file'))

String text = 'C:\\Users\\myuser\\Documents\\Dropbox\\some files\\another sub folder\\myzip.zip'

I hope that helps!


#2

Thanks Brian. I’ll move this to the Tips category.


#3

To make it easier to use, I created a folder in root of project called “TestFiles”. You would have to move your test files into that directory. Then the following code replaces your text variable.

def testFiles = RunConfiguration.getProjectDir().replaceAll(’/’, ‘\\’) + ‘\TestFiles’
String text = testFiles + ‘\myzip.zip’

Benefit is that these files become part of your project.


#4

Thanks danpoleary! Here’s where I ended up after making your change:

import java.awt.Robot as Robot
import java.awt.Toolkit as Toolkit
import java.awt.datatransfer.StringSelection as StringSelection
import java.awt.event.KeyEvent as KeyEvent
import com.kms.katalon.core.configuration.RunConfiguration

WebUI.click(findTestObject('Object Repository/Page_Add New Post  Sandbox  WordPress/button_Choose your zip file'))

String text =  RunConfiguration.getProjectDir() + '/TestFiles/myfile.zip'

if(RunConfiguration.getOS().indexOf("Windows")!= -1)
{
 text = text.replace('/', '\\')
}

StringSelection ss = new StringSelection(text)

Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null)

Robot robot = new Robot()

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)