Save contents of a page to a file?

Hello,

I need to automate the download of a json file either by directly downloading the file (provided by accessing an URL) or by loading the file in the browser as text and then saving the contents locally.

Is there a way to do that?

To get the whole content (source) of a page, you can use ‘getPageSource’ from Selenium, e.g:

WebDriver driver = DriverFactory.getWebDriver()
driver.getPageSource()

Using returned value of ‘getPageSource’ as an input content to write to your file:

Can Katalon studio directly execute java code? Or is there another way to save the contents of the page to a file?

@“Dobromir Manchev”
you can write any java code in keyword section section of the katalon studio and use it as keyword/method. To use java code just import classes.

Sorry, i am not really a developer… Katalon doesn’t recognize the “WebDriver” class, what do i have to import for it to be able to use it?

Here’s my script so far:



import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.checkpoint.CheckpointFactory as CheckpointFactory
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as MobileBuiltInKeywords
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testcase.TestCaseFactory as TestCaseFactory
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testdata.TestDataFactory as TestDataFactory
import com.kms.katalon.core.testobject.ObjectRepository as ObjectRepository
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSBuiltInKeywords
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUiBuiltInKeywords
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable
import org.openqa.selenium.Keys as Keys
import java.io.File

WebUI.openBrowser('https://www.cl.cam.ac.uk/~mgk25/ucs/examples/TeX.txt')
WebDriver driver = DriverFactory.getWebDriver()
String pageSource = driver.getPageSource()

Here’s some additional info:
I have an excel file that contains a list of IDs.
I have to do is access an URL that returns a json file, using an ID from the Excel file as a variable.
I then have to access a second URL with the same ID and get a 2nd file.
Finally, i have to compare the two json files and see if they are identical. The comparing part is not the priority as of now and might be done later.

The problem is Firefox and Chrome show the file contents rather than ask to save the file. I tried adding a MIME type and making it automatically download the file to a default location, but for some reason this didn’t work (don’t know why).