"Interactive" Test with Katalon

Let me show you an example. Please make a test case with any name in Katalon Studio, paste the following code, and RUN it. This script will execute Windows’ ipconfig command, read the stdout and stderr from the process and print it.

import java.nio.charset.Charset

String[] CMD = { "cmd", "/c", "ipconfig.exe"}

String WINCHARSET = "MS932"    
// Charset used for the output of the command, depends on your Windows PC environment, 
// see https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html

// Run the Windows command
Process process = Runtime.getRuntime().exec(CMD)

// Get input streams
BufferedReader stdInput = 
    new BufferedReader(
        new InputStreamReader(
            process.getInputStream(),
            Charset.forName(WINCHARSET)
        )
    )
BufferedReader stdError = 
    new BufferedReader(
        new InputStreamReader(
            process.getErrorStream(),
            Charset.forName(WINCHARSET)
        )
    )

// Read command standard output
String s
StringBuilder sb = new StringBuilder()
sb.append("Standard output: \n")
while ((s = stdInput.readLine()) != null) {
	sb.append(s + "\n")
}

// Read command errors
sb.append("Standard error: \n")
while ((s = stdError.readLine()) != null) {
	sb.append(s+ "\n")
}

System.out.println(sb.toString())

I cheated. The origianl was https://stackoverflow.com/questions/7112259/how-to-execute-windows-commands-using-java-change-network-settings

2 Likes