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
- The title contains a specific keyword;
- 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
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 !
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
How to use the Windows.switchToDesktop() custom keyword to switch window faster? → You are here
How to use the WebUI.setText() custom keyword successfully?
How to use the WebUI.dragAndDropToObject custom keyword successfully?