Is there any way to setup a driver from a keyword in Katalon?

I’m trying to follow a basic Page Object Model tutorial (which I know works when using IntelliJ) here: https://www.pluralsight.com/guides/getting-started-with-page-object-pattern-for-your-selenium-tests

but I have no idea how to translate the setup code for Katalon, the code I want to reuse across all test case scripts e.g. via the Extends keyword on the test case class - it involves this code (currently I have this at the start of every test case):

	ChromeOptions options = new ChromeOptions();
	options.addArguments("--start-maximized");
	System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
	WebDriver driver = new ChromeDriver(options)
	driver.get(URL)

Any help/explanations are appreciated

There are many ways you can do this. I’ll just give you what we do, and maybe it will give some food for thought.

1.) Handle all of your desired capabilities in the project settings (Project > Settings > Desired Capabilities > WebUI > Chrome). In your case, you would add the “–start-maximized” arg there. For more info on configuring Desired Capabilities this way, see this.

2.) Instead of using driver.get(URL), at the start of every test case you would instead use WebUI.openBrowser(URL). This tells Katalon to instantiate a driver instance, with any capabilities you’ve defined in Step 1.

3.) In your page object hierarchy, you have one abstract class (ours is just called “Page”), which (among other things) asks Katalon for the driver it’s currently using:

public abstract class Page {

    protected WebDriver driver = DriverFactory.getWebDriver();

    .
    .
    .
}

4.) Any and all page object classes you write would be subclasses of the generic “Page” class, and thus have access to the current driver by default. For example, a “LoginPage” class might look something like:

public class LoginPage extends Page {

    public void setUsernameField(String value) {
        WebElement element = driver.findElement(...);
        element.sendKeys(value);
    }
    
    .
    .
    .
}

5.) Your scripts end up looking something like:

WebUI.openBrowser(URL);
LoginPage loginPage = new LoginPage();
loginPage.setUsernameField("Billy Babaganoosh");

(or the groovy equivalent, mine is just pure java).

Note: This approach is thread-safe, if you were planning on running scripts in parallel at any point.
Note: This allows you to run any browser type you want, not just chrome.

2 Likes