Turn geturl into a variable

Is there anyone that could clearly with examples, let me know how to turn a geturl into a global variable for use within my project?

thanks

Here are some links to assist you with getUrl and creating a GlobalVariable. The getUrl returns a string so the GlobalVariable should be a String type. Then, use the getUrl into the GlobalVariable.

and some samples:
GlobalVariable.gSiteUrl = WebUI.getUrl();

GlobalVariable.gImportFileName = ‘G:\Katalon Test Cases\Data Files\Big Fight.txt’;

finally, make sure you have both of these in your import list at the top of the Test Case:

import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

Thanks for your reply:

Question: is this GlobalVariable.gSiteUrl = WebUI.getUrl(); actually defined in the profile? or the script?

thanks

Larger Context:

Objective: I have a setup script where I am trying to getUrl - and Define it as a global variable for use in other scripts.

Process: This is defined inside my setup script
GlobalVariable.simURL = WebUI.getUrl()

Next, in my default profile I define simURL:

This is NOT working: in a test suite when I try and navigate to Url GlobalVariable.simURL it fails with: Step failed exception Unable to navigate to "
I also have the necessary import installed:
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

Next

I

Can I define the getUrl as a global variable in a script?
Is this possible?

Would gSiteUrl need to be defined in the profile?

@Paul_Smith1, you define GlobalVariables in the Profile. After defining the GV, you can use it in all TestCase pages of a TestSuite after you collect/fill. However, to use the GV in more than the “original” TestSuite where you collect/fill the variable, then you have to SAVE the variable quantity (it’s contents) to an outside Data source, such as MS Excel. Then, when you run the other TestSuites, you “read” the contents of the “original” from the Data source.
As an example, save an MS Excel file within the Data folder of Katalon Studio. (you need the path and the MS Excel file name in your query; see below) Now, after you have the GV filled, save it to MS Excel (you can find similar examples in other KS questions) :

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;

FileInputStream fis = new FileInputStream (“G:\Katalon Studio\Data Files\TestId 23.xlsx”);

XSSFWorkbook workbook = new XSSFWorkbook (fis);

XSSFSheet sheet = workbook.getSheet(“Sheet1”);
// set data in cell A2
Row row = sheet.createRow(1);
Cell cell = row.createCell(0);

cell.setCellType(cell.CELL_TYPE_STRING);
cell.setCellValue(GlobalVariable.gLastName);
FileOutputStream fos = new FileOutputStream(“G:\Katalon Studio\Data Files\TestId 23.xlsx”);
workbook.write(fos);
fos.close();

Now, in a second TestSuite, read the GV back from the MS Excel sheet into the TestCase (you need the same import statements as above) :
FileInputStream fis = new FileInputStream (“G:\Katalon Studio\Data Files\TestId 23.xlsx”);

XSSFWorkbook workbook = new XSSFWorkbook (fis);

XSSFSheet sheet = workbook.getSheet(“Sheet1”);

// read data from cell A2
Row row = sheet.getRow(1);
Cell cell = row.getCell(0);
// read in the user’s last name
GlobalVariable.gLastName = cell.getStringCellValue();

Hope I have addressed all your concerns.

This worked magnificently well, thanks very much for your very clear response and instructions. I hope this thread can remain somewhere where Katalon users may easily find it for reference purposes.

thanks again!

hello,

excel, ugh, ok if it works, but as in testengineer should use better way e.g postgress db :slight_smile:

Timo, instead of merely passing comment, please write a Tips&Tricks article that explains how to install setup Postgres to use for testing and how to integrate with Katalon.

hi, Russ,

Google is our friend :slight_smile:

There’s very little that google does that I would consider friendly. :confused:

Please. Write it up. Be our hero.

hello,

If I’ll create a document, then I will do it to way that document reader not need to be engineer to get it everything work as it should be. That takes time and my current company will pay me for that, Katalon community is not the payer :slight_smile: sorry