如何在不同Groovy类访问public static String值及Katalon全局变量配置
Hey there! Let's break down your questions one by one, since you're working with Katalon Studio and Groovy:
1. Accessing a public static String from another Groovy class
First, let's say you have a dedicated class (like a constants holder) where you've defined your static variable:
// Let's name this class GlobalConstants.groovy class GlobalConstants { // Public static String variable public static String prod_name = "MyAwesomeProduct" }
To access this variable in another Groovy class:
- If the classes are in the same package, you can directly reference it using the class name + variable name:
class TestHelper { def validateProduct() { // Use the static variable directly println("Validating product: " + GlobalConstants.prod_name) } } - If the classes are in different packages, just add an import statement at the top of the target class:
// Import the constants class import com.yourproject.constants.GlobalConstants class CheckoutFlow { def fillProductField() { String productName = GlobalConstants.prod_name // Do something with productName } }
2. Using the prod_name variable in a Katalon Test Case
You can use this variable in both Script Mode and Keyword Mode test cases:
Script Mode
Import the constants class (if needed) and use the variable directly in your test steps:
import com.yourproject.constants.GlobalConstants // Example: Enter product name into a text field WebUI.setText(findTestObject('Page_Product/input_ProductName'), GlobalConstants.prod_name) // Or just log the value WebUI.comment("Current product name: " + GlobalConstants.prod_name)
Keyword Mode
If you're using Keyword Mode, you can add a "Script" test step and use the same approach:
- Add a new test step, select "Script"
- In the script editor, paste code like:
def targetProduct = GlobalConstants.prod_name WebUI.setText(findTestObject('Page_Cart/input_ItemName'), targetProduct)
3. Defining a variable as a Global Variable in Katalon
Katalon has two common ways to set up project-wide global variables:
Option 1: Use Katalon's Built-in Global Variables
This is the official, integrated way to create global variables:
- On the left sidebar, click Profiles (next to Test Cases)
- Select the profile you want to use (e.g.,
default) - Right-click the profile > New > Variable
- Fill in the details:
- Name:
prod_name - Type:
String - Value: Your desired product name (e.g., "MyAwesomeProduct")
- Name:
- Save the profile
To access this global variable anywhere in your project (test cases, classes, keywords), use:
// In test case or Groovy class WebUI.setText(findTestObject('Page_Home/input_Product'), GlobalVariable.prod_name)
Option 2: Use a Custom Static Class (As a Global Holder)
The approach from question 1 also acts as a global variable solution—since static variables in a public class are accessible project-wide (with proper imports). This is great if you want to group related constants together for better organization.
内容的提问来源于stack exchange,提问作者tsr_qa

