Can we pass a value generated in a test case to another test case as a input?

Yes you can. You don’t need to ‘return’ it, just assign it with the global variable you want.

Thanks

Hi there

The correct script should be:

WebUI.setText(findTestObject('Audit Mapping/Page_Compliance Portal (2)/textarea_ctl00ContentPlaceHold'), AuditName)

‘AuditName’ will be passed as itself because it’s a string, not the variable. You should remove ’ ’

Thanks

Hi there,

Actually you can have a ‘return’ statement in test case A and then use it in test case B:

Example:

TC_A:

String testString = 'abcdef'

return testString

TC_B:

String returnString = WebUI.callTestCase('TC_A')

println returnString

The results when executing TC_B will print ‘abcdef’ for returnString.So you can ultilize the ‘return’ statement and map it in your test case B per your scenario.

Thanks

4 Likes

Hey Prithviraj,

Firs of all thanks for your response.

Sure … let me check and come back :slight_smile:

Thanks

Can we return multiple variables from one test case

1 Like

how about trying getText and then setText to pass the extracted text from getText

Hi , can u share your solution, how u set a tc1 data as global and reused in TC2 . ?
I followed ur steps but my test pass without doing the action i want

Sandeep said:

Hi , can u share your solution, how u set a tc1 data as global and reused in TC2 . ?
I followed ur steps but my test pass without doing the action i want

When I tried to set value to a global variable in TC1 and then tried to println the value of the same variable in TC2, both part of TS1, I did not get the value in TC2, got Null value. SO it is not working for me at least, then tried passing a regular variable in a call TC2 statement, it is showing up in TC2. But still is giving error saying variable not declared in TC2.

Guys,

This is a fundamental part of Katalon’s testing API – it certainly works so there must be some issue in your code if it doesn’t work.

First of all, let’s set up a global variable called THE_ANSWER with the value “42” and, let’s do this in “TestCase 1”

GlobalVariable.THE_ANSWER = "42"

Now, in “TestCase 2”, let’s print its value using a comment:

WebUI.comment("The answer is ... " + GlobalVariable.THE_ANSWER)

Now execute a suite that runs “TestCase 1” before “TestCase 2”

If that doesn’t work, check to see if the GlobalVariable component is imported correctly – you should see this line in your import section at the top of the file:

import internal.GlobalVariable as GlobalVariable

If it STILL doesn’t work, well… something terrible is wrong with your system Reinstall?

Hope this helped.

Russ

2 Likes

Russ Thomas said:

Guys,

This is a fundamental part of Katalon’s testing API – it certainly works so there must be some issue in your code if it doesn’t work.

First of all, let’s set up a global variable called THE_ANSWER with the value “42” and, let’s do this in “TestCase 1”

GlobalVariable.THE_ANSWER = "42"

Now, in “TestCase 2”, let’s print its value using a comment:

WebUI.comment("The answer is ... " + GlobalVariable.THE_ANSWER)

Now execute a suite that runs “TestCase 1” before “TestCase 2”

If that doesn’t work, check to see if the GlobalVariable component is imported correctly – you should see this line in your import section at the top of the file:

import internal.GlobalVariable as GlobalVariable

If it STILL doesn’t work, well… something terrible is wrong with your system Reinstall?

Hope this helped.

Russ

Hi Russ, first of all thanks for your response. I have created a sample project and following is the set up, I am still getting the error as shown below.

TestCase1 :

import internal.GlobalVariable as GlobalVariable

GlobalVariable.THE_ANSWER = “42”

TestCase2:

import internal.GlobalVariable as GlobalVariable

WebUI.comment("The answer is … " + GlobalVariable.THE_ANSWER)

TestSuite - SampleTest:

1. TestCase1
2. TestCase2

Following are the Console messages I am seeing.

04-18-2018 12:26:25 PM - [START] - Start Test Suite : Test Suites/SampleTest

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘hostName’ with value ‘nabhaskar - USBLRNABHASKAR1.us.deloitte.com

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘os’ with value ‘Windows 10 64bit’

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘hostAddress’ with value ‘10.31.112.166’

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘katalonVersion’ with value ‘5.4.0.1’

04-18-2018 12:26:26 PM - [START] - Start Test Case : Test Cases/TestCase1

04-18-2018 12:26:26 PM - [INFO] - Evaluating variables for test case

04-18-2018 12:26:26 PM - [START] - Start action : Statement - THE_ANSWER = “42”

04-18-2018 12:26:26 PM - [END] - End action : Statement - THE_ANSWER = “42”

04-18-2018 12:26:26 PM - [ERROR] - Test Cases/TestCase1 FAILED because (of) Variable ‘THE_ANSWER’ is not defined for test case.

04-18-2018 12:26:26 PM - [END] - End Test Case : Test Cases/TestCase1

04-18-2018 12:26:26 PM - [START] - Start Test Case : Test Cases/TestCase2

04-18-2018 12:26:26 PM - [INFO] - Evaluating variables for test case

04-18-2018 12:26:27 PM - [START] - Start action : comment

04-18-2018 12:26:27 PM - [END] - End action : comment

04-18-2018 12:26:27 PM - [ERROR] - Test Cases/TestCase2 FAILED because (of) Variable ‘THE_ANSWER’ is not defined for test case.

04-18-2018 12:26:27 PM - [END] - End Test Case : Test Cases/TestCase2

04-18-2018 12:26:27 PM - [END] - End Test Suite : Test Suites/SampleTest

Any help would be greatly appreciated, thanks.

Narendra Bhaskar said:

Russ Thomas said:

Guys,

This is a fundamental part of Katalon’s testing API – it certainly works so there must be some issue in your code if it doesn’t work.

First of all, let’s set up a global variable called THE_ANSWER with the value “42” and, let’s do this in “TestCase 1”

GlobalVariable.THE_ANSWER = "42"

Now, in “TestCase 2”, let’s print its value using a comment:

WebUI.comment("The answer is ... " + GlobalVariable.THE_ANSWER)

Now execute a suite that runs “TestCase 1” before “TestCase 2”

If that doesn’t work, check to see if the GlobalVariable component is imported correctly – you should see this line in your import section at the top of the file:

import internal.GlobalVariable as GlobalVariable

If it STILL doesn’t work, well… something terrible is wrong with your system Reinstall?

Hope this helped.

Russ

Hi Russ, first of all thanks for your response. I have created a sample project and following is the set up, I am still getting the error as shown below.

TestCase1 :

import internal.GlobalVariable as GlobalVariable

GlobalVariable.THE_ANSWER = “42”

TestCase2:

import internal.GlobalVariable as GlobalVariable

WebUI.comment("The answer is … " + GlobalVariable.THE_ANSWER)

TestSuite - SampleTest:

1. TestCase1
2. TestCase2

Following are the Console messages I am seeing.

04-18-2018 12:26:25 PM - [START] - Start Test Suite : Test Suites/SampleTest

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘hostName’ with value ‘nabhaskar - USBLRNABHASKAR1.us.deloitte.com

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘os’ with value ‘Windows 10 64bit’

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘hostAddress’ with value ‘10.31.112.166’

04-18-2018 12:26:25 PM - [RUN_DATA] - Logging run data ‘katalonVersion’ with value ‘5.4.0.1’

04-18-2018 12:26:26 PM - [START] - Start Test Case : Test Cases/TestCase1

04-18-2018 12:26:26 PM - [INFO] - Evaluating variables for test case

04-18-2018 12:26:26 PM - [START] - Start action : Statement - THE_ANSWER = “42”

04-18-2018 12:26:26 PM - [END] - End action : Statement - THE_ANSWER = “42”

04-18-2018 12:26:26 PM - [ERROR] - Test Cases/TestCase1 FAILED because (of) Variable ‘THE_ANSWER’ is not defined for test case.

04-18-2018 12:26:26 PM - [END] - End Test Case : Test Cases/TestCase1

04-18-2018 12:26:26 PM - [START] - Start Test Case : Test Cases/TestCase2

04-18-2018 12:26:26 PM - [INFO] - Evaluating variables for test case

04-18-2018 12:26:27 PM - [START] - Start action : comment

04-18-2018 12:26:27 PM - [END] - End action : comment

04-18-2018 12:26:27 PM - [ERROR] - Test Cases/TestCase2 FAILED because (of) Variable ‘THE_ANSWER’ is not defined for test case.

04-18-2018 12:26:27 PM - [END] - End Test Case : Test Cases/TestCase2

04-18-2018 12:26:27 PM - [END] - End Test Suite : Test Suites/SampleTest

Any help would be greatly appreciated, thanks.

Hi, I have got this figured out, when I used default profile, global variables set in one test case was not available in another test case, but when created a new execution profile and tagged my test cases and test suite to the same profile, every thing started working fine. Thanks for your suggestion.

Bis thanks for providing the solution for this usage of variable from one test case in another test case issue. I was struggling for a long time to get this done.

Chandrasekaran Ganapathy said:

Can we return multiple variables from one test case

I have the same issue. Have u deal with it?

Phan Ky Giang said:

Chandrasekaran Ganapathy said:

Can we return multiple variables from one test case

I have the same issue. Have u deal with it?

I think the easiest way to do that is a variable of type array, or map or similar. You can about arrays et al here:

http://docs.groovy-lang.org/latest/html/documentation/#_arrays

Phan Ky Giang said:

Chandrasekaran Ganapathy said:

Can we return multiple variables from one test case

I have the same issue. Have u deal with it?

Yes… You can create a HashMap and return the map…

Test Case :1
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.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
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.testdata.TestData as TestData
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 internal.GlobalVariable as GlobalVariable

WebUI.openBrowser(’’)

WebUI.navigateToUrl(GlobalVariable.URL)

WebUI.click(findTestObject(‘Reset Wishlist/Page_Your store/a_Log in’))

WebUI.setText(findTestObject(‘Reset Wishlist/Page_Your store. Login/input_Email_Email’), ‘johndeo@gmail.com’)

WebUI.setEncryptedText(findTestObject(‘Reset Wishlist/Page_Your store. Login/input_Password_Password’), ‘aeHFOx8jV/A=’)

WebUI.click(findTestObject(‘Reset Wishlist/Page_Your store. Login/input_Forgot password_button-1’))

WebUI.click(findTestObject(‘Reset Wishlist/Page_Your store/span_Wishlist’))

if (WebUI.verifyElementPresent(findTestObject(‘Reset Wishlist/Page_Your store. Wishlist/input_245.00_updatecart’),
5, FailureHandling.OPTIONAL) == true) {
while (WebUI.verifyElementPresent(findTestObject(‘Reset Wishlist/Page_Your store. Wishlist/input_Remove_removefromcart’),
5, FailureHandling.OPTIONAL) == true) {
WebUI.click(findTestObject(‘Reset Wishlist/Page_Your store. Wishlist/input_Remove_removefromcart’))

    WebUI.click(findTestObject('Reset Wishlist/Page\_Your store. Wishlist/input\_245.00_updatecart'))  
}  
 
WebUI.verifyTextPresent('The wishlist is empty!', false)  

WebUI.navigateToUrl(GlobalVariable.URL)  

} else {
WebUI.navigateToUrl(GlobalVariable.URL)
}

WebUI.closeBrowser()

Test case :2
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.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
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.testdata.TestData as TestData
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 internal.GlobalVariable as GlobalVariable

WebUI.callTestCase(findTestCase(‘Reset Wishlist’), [(GlobalVariable.Password) : GlobalVariable.Password, (GlobalVariable.Username) : GlobalVariable.Username],
FailureHandling.STOP_ON_FAILURE)

WebUI.click(findTestObject(‘Reset Wishlist/Page_Your store/input_245.00_button-2 add-to-w’))

WebUI.click(findTestObject(‘Reset Wishlist/Page_Your store/a_wishlist’))

WebUI.verifyElementText(findTestObject(‘Reset Wishlist/Page_Your store. Wishlist/a_HTC One M8 Android L 5.0 Lol’), ‘HTC One M8 Android L 5.0 Lollipop’)

can anyone help me out how can i map with this methods of test case 1 into the test case-2 , so i can call test case 1 in test case 2

Nope, this is not working for me
I initialize my global variable like this:


Change its value in TC 1 and use it in TC 2 but its still blank

That’s how they work. Your initial value is re-assigned at the start of each test case. If you think about it, how else could it work? Re-assign the value you used last week?

I handle this by reading my globals from an external file. If I need to share the same value between tests, I use callTestCase - usually that means I don’t need a global since a Test Case can return a value (which might be a Map of values).

https://docs.katalon.com/katalon-studio/docs/call-test-case.html

1 Like

Thanks a lot for the response, I could make my TC work.