Upload File From A URL

Hi all,

I recently started playing around with WebUI.uploadFile and WebUI.uploadFileWithDragAndDrop and was curious if there was a way to pass it a public URL to retrieve a image from the web to upload instead of it having to be in a directory on a local machine.

For example:

Instead of this: WebUI.uploadFileWithDragAndDrop(findTestObject(myObject), C:\myfile.txt)

I want to this WebUI.uploadFileWithDragAndDrop(findTestObject(myObject), https://openthread.google.cn/images/ot-contrib-google.png)

Is there currently a way to this? I did give it a try. and Katalon didn’t know what to do with this.

Simply, no.

You can develop a solution for yourself. You can download a image from a URL to save it into your local disk. How to? See the following post:

Then you can upload the image file from disk to the target web site using WebUI.uploadFile().

1 Like

Thanks for the information, I did give that a try but it looks like I’m getting a nullpointerexception when trying that provided code. The error happens on the ImageIO.write step.

=============== ROOT CAUSE =====================


For trouble shooting, please visit: https://docs.katalon.com/katalon-studio/docs/troubleshoot-common-execution-exceptions-web-test.html
================================================

08-19-2020 11:14:06 AM Test Cases/Scripts/file download test

Elapsed time: 5.125s

Test Cases/Scripts/file download test FAILED.
Reason:
java.lang.NullPointerException
	at file download test.run(file download test:33)
	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:339)
	at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:330)
	at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:309)
	at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:301)
	at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:235)
	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 TempTestCase1597853643448.run(TempTestCase1597853643448.groovy:25)

The stacktrace indicates something in your code is null unexpectedly, but it does not tell precisely which. You should try to find what is null in your code for yourself.

I understand that piece that I should try to find what is null in the code myself, but from my knowledge nothing is null at the moment, nor should it be null because the only code running in that script is from the forum post you provided:

This is the step that it is failing on.

ImageIO.write(saveImage, "png", new File("C:\\test\\test-image.png"))

It should be providing all parameters for that from what I understand, but I’m still getting an NPE

Do you use Windows? Mac? Linux?

This path is valid only for Windows, you know.

Yes, this a Windows 10 machine.

Ok I just figured it out, the directory “test” didn’t exist. This brings me to an additional question though then, can I create the directory mid-execution if the directory doesn’t already exist on the machine? I presume I’ll need an additional line or two of code to do so.

Edit: I’m answering my own questions apparently, here’s a solution I found for making a directory.

File directory = new File("C:/test");
directory.mkdirs();

I will have to make sure this works on linux too but for windows at least this appears to be a solution for the time being.

Ok so, here is where I’m at. The solution above does work for local runs, but ultimately this functionality is needed to work also on a remote server run (using the Katalon RE to run on a remote server like Zalenium). Here is the implementation as gathered from @kazurayam 's help

import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import org.openqa.selenium.By
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

def dirPath = "C:\\test" // specify directory path

def filePath = dirPath + "\\test-image.png" // specify file path

WebUI.openBrowser("https://d1h3p5fzmizjvp.cloudfront.net/themes/katalon_4/images/katalon_template_1809/logo@2x.png") // open katalon browser with the image

WebElement image = DriverFactory.getWebDriver().findElement(By.xpath("//img")) // find the image

String imgSrc = image.getAttribute("src") // get source attribute

URL imageURL = new URL(imgSrc) // make src url the image url

BufferedImage saveImage = ImageIO.read(imageURL)  // read the image and prep for saving

File directory = new File(dirPath); //specify directory

directory.mkdirs(); //create the directory

ImageIO.write(saveImage, "png", new File(dirPath )) // Save image to proper directory

WebUI.uploadFileWithDragAndDrop(findTestObject('myFileUploadObject'), filePath) // upload file from the filepath

As stated this does work locally, but when running remote the file get saved to the machine running the Katalon RE and the Zalenium node doesn’t know where the file is because of this. Any ideas?

You should make it clear where you want to save the image on the machine running the KRE.

The following test case script will save a png file in a directory named tmp under the project’s root directory. I tested it on Mac. It should work on Windows and Linux as well without any code change.

//import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import java.awt.image.BufferedImage

import javax.imageio.ImageIO

import org.openqa.selenium.By
import org.openqa.selenium.WebElement

import com.kms.katalon.core.configuration.RunConfiguration
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path dirPath = projectDir.resolve("tmp")
Files.createDirectories(dirPath)
Path filePath = dirPath.resolve("test-image.png")

WebUI.openBrowser("https://d1h3p5fzmizjvp.cloudfront.net/themes/katalon_4/images/katalon_template_1809/logo@2x.png") // open katalon browser with the image
WebElement image = DriverFactory.getWebDriver().findElement(By.xpath("//img")) // find the image
String imgSrc = image.getAttribute("src") // get source attribute
URL imageURL = new URL(imgSrc) // make src url the image url
WebUI.closeBrowser()

BufferedImage saveImage = ImageIO.read(imageURL)  // read the image and prep for saving
ImageIO.write(saveImage, "png", filePath.toFile()) // Save image to proper directory

//WebUI.uploadFileWithDragAndDrop(findTestObject('myFileUploadObject'), filePath) // upload file from the filepath

You should learn how to use Java NIO Path API https://www.baeldung.com/java-nio-2-path to make your code platform-independent.

1 Like

Ultimately I want to save it to the Zalenium node because I need it for the test run. The test steps look like this:

Step 1: Find the image and download it on the Node in Zalenium
Step 2: Run the test that interacts with our website to get to the file upload location
Step 3: Upload the image from Step 1

The solutions provided so far work locally, I.E they’ll save a file, image, or whatever to the KRE but I need it for the remote execution on Zalenium because when it goes to find the file to upload its not on the Node’s drive.

I have no Zalenium configuration so I have not more words to say.

Not a problem, in the original scope of how I asked this question you did answer it. I marked your Java NIO path as the solution.

To others coming: For those that need this function on a remote run, check out Local File Detector to handle file uploads on remote execution for a solution. @kazurayam 's solution along with the linked forum post will get you where you need to be.

1 Like