[KShare] How to use the Windows.switchToDesktop() custom keyword to switch window faster?

How to use the Windows.switchToDesktop() custom keyword to switch window faster?

Enhancing the efficiency of window switching involves two key elements:

  • Firstly, ensuring that the system is able to identify the current expected windows;
  • Secondly, identifying the distinctive features of the windows so that they are easily identifiable.

To guarantee that the system is able to identify the current expected windows, we can use the keyword Windows.switchToDesktop() before switching to a specific window. The system can manage all existing windows before switching.

We mostly prefer to find windows by title, and in order to identify the title quickly, we can employ techniques like

  1. The title contains a specific keyword;
  2. Use the regular expression (abbreviated as regex or regexp) to find the matched pattern of text.

The Katalon Product Support team is happy to share with you the below list of custom keywords :point_down:

import java.util.regex.Pattern

import org.openqa.selenium.WebElement

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.logging.KeywordLogger
import com.kms.katalon.core.testobject.WindowsTestObject
import com.kms.katalon.core.testobject.WindowsTestObject.LocatorStrategy
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows

public class WindowsImprovedKeywords {
	private static KeywordLogger logger = KeywordLogger.getInstance(WindowsImprovedKeywords.class);

	@Keyword
	def startApplicationWithTitle(String application, String title) {
		Windows.startApplicationWithTitle(application, '')
		logger.logInfo("Application has been started")
		Windows.delay(5) // This is optional: Waiting for the main window to show up
		this.switchToWindowTitle(title);
	}

	@Keyword
	def switchToWindowTitle(String title) {
		logger.logInfo("Start switchToWindowTitle")
		WindowsTestObject windowObject = new WindowsTestObject()
		windowObject.setLocator(title)
		windowObject.setLocatorStrategy(LocatorStrategy.NAME)
		Windows.switchToDesktop()
		Windows.switchToWindow(windowObject)
		Windows.switchToApplication()
		logger.logInfo("Done switchToWindowTitle")
	}

	@Keyword
	def startApplicationWithTitlePattern(String application, String titleRegex) {
		Windows.startApplicationWithTitle(application, '')
		logger.logInfo("Application has been started")
		Windows.delay(5) // This is optional: Waiting for the main window to show up
		this.switchToWindowTitlePattern(titleRegex);
	}

	@Keyword
	def switchToWindowTitlePattern(String titleRegex) {
		logger.logInfo("Start switchToWindowTitlePattern")
		Windows.switchToDesktop()

		def allWindows = Windows.getDriver().findElementsByXPath("/*/*[name() = 'Window' or name() = 'Pane']")
		def targetWindow = findWindowWithTitle(allWindows, titleRegex)
		if (targetWindow == null) {
			return;
		}

		WindowsTestObject windowObject = new WindowsTestObject()
		windowObject.setLocator(targetWindow.getAttribute("Name"))
		windowObject.setLocatorStrategy(LocatorStrategy.NAME)
		Windows.switchToWindow(windowObject)

		Windows.switchToApplication()
		logger.logInfo("Done switchToWindowTitlePattern")
	}

	@Keyword
	def startApplicationWithTitleContains(String application, String aPartOfTitle) {
		Windows.startApplicationWithTitle(application, '')
		logger.logInfo("Application has been started")
		Windows.delay(5) // This is optional: Waiting for the main window to show up
		this.switchToWindowWithTitleContains(aPartOfTitle);
	}

	@Keyword
	def switchToWindowWithTitleContains(String aPartOfTitle) {
		logger.logInfo("Start switchToWindowTitlePattern")
		Windows.switchToDesktop()

		def targetWindow = Windows.getDriver().findElementByXPath("/*/*[(name() = 'Window' or name() = 'Pane') and contains(@Name, '${aPartOfTitle}')]")
		if (targetWindow == null) {
			return;
		}

		WindowsTestObject windowObject = new WindowsTestObject()
		windowObject.setLocator(targetWindow.getAttribute("Name"))
		windowObject.setLocatorStrategy(LocatorStrategy.NAME)
		Windows.switchToWindow(windowObject)

		Windows.switchToApplication()
		logger.logInfo("Done switchToWindowTitlePattern")
	}

	def WebElement findWindowWithTitle(List<WebElement> windows, String titlePattern) {
		for (def windowI : windows) {
			if (Pattern.matches(titlePattern, windowI.getAttribute("Name"))) {
				return windowI;
			}
		}
		return null;
	}
}

Also, you can refer to the sample project below for more details:
Windows Improved Keywords.zip (237.7 KB)

Let us know if the custom keywords that we have provided works for you. And if they do, then don’t forget to show us some love by clicking on the like button below :heart:!

And feel free to leave us your feedback so that we could improve our articles in the future!


We also have other topics related to working with custom keywords in Katalon Studio, check them out below :point_down:

:pushpin: How to use the Windows.switchToDesktop() custom keyword to switch window faster? → You are here

:pushpin: How to use the WebUI.setText() custom keyword successfully?

:pushpin: How to use the WebUI.dragAndDropToObject custom keyword successfully?

:pushpin: How to work better with Browser Alerts?

1 Like

Thank you Product Support team (@support.squad) as always for this insightful topic. And also a big shout-out to the two individuals below for your contribution to this topic:

Linh Nguyen Thong Tran
Linh Nguyen (@linh.nguyen) - Product Support Manager at Katalon Thong Tran (@thong.tran) - Senior Software Engineer at Katalon
Linh is the Product Support team Manager at Katalon. She spent many years working as an Automation Testing QA before joining Katalon Product Support as a technical support expert. Based on her experiences, she usually provides customers with highly applicable solutions. She now manages her team with a user-centric approach to guarantee customers greater success with Katalon Products. A passionate Katalon developer with a wealth of programming and testing expertise. Thong has been dedicated to providing exceptional enterprise support for the past five years, helping Katalon’s customers achieve their testing goals with ease.

Do you have an idea for a topic that you would like the Product Supoprt team to cover in their next KShare article? Then share them with us by simply fill in the form below :point_down: