You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不同Groovy类访问public static String值及Katalon全局变量配置

Answers to Your Katalon Studio & Groovy Questions

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:

  1. Add a new test step, select "Script"
  2. 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:

  1. On the left sidebar, click Profiles (next to Test Cases)
  2. Select the profile you want to use (e.g., default)
  3. Right-click the profile > New > Variable
  4. Fill in the details:
    • Name: prod_name
    • Type: String
    • Value: Your desired product name (e.g., "MyAwesomeProduct")
  5. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:48:53