Custom Keyword for browser screen size

Hello,

I have a configured Custom Keyword but errors appears while I try to use it in KS.
What should I specify/activate to use this keyword?

@Keyword(keywordObject=‘WEB’)

def static enhanced_SetViewportSize(int width, int height) {

WebDriver webDriver = DriverFactory.getWebDriver()

int browserWidthGap = webDriver.manage().window().getSize().width - Integer.parseInt(WebUiBuiltInKeywords.executeJavaScript(‘return (window.innerWidth || 0)’, null).toString())

int browserHeightGap = webDriver.manage().window().getSize().height - Integer.parseInt(WebUiBuiltInKeywords.executeJavaScript(‘return (window.innerHeight || 0)’, null).toString())

float ratio = Float.parseFloat(WebUiBuiltInKeywords.executeJavaScript(‘return (window.devicePixelRatio || 1)’, null).toString())

int actualWidth = Math.round((width + browserWidthGap * ratio) / ratio)

int actualHeight = Math.round((height + browserHeightGap * ratio) / ratio)

WebUiBuiltInKeywords.setViewPortSize(actualWidth, actualHeight)

}

Thank you!

*I know about Desired Capabilities. But in Safari specified screen size (Desired Capabilities) is not applied. Chrome is ok.

You need to insert 2 lines:

import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.WebDriver

Thank you!
It helped, but did not solve errors.
Could you please take a look one more time?

Screenshot is informative, but is not enough to reproduce your problem on other’s PC. Please copy and paste your entire test case script here.

Thank you!
it is the Custon Keyword I try to use.


package screenSize

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 static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.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
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.WebDriver
import internal.GlobalVariable
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.WebDriver

public class screenSize_1 {

@Keyword(keywordObject='WEB')
def static enhanced_SetViewportSize(int width, int height) {
	WebDriver webDriver = DriverFactory.getWebDriver()
	int browserWidthGap = webDriver.manage().window().getSize().width - Integer.parseInt(WebUiBuiltInKeywords.executeJavaScript('return (window.innerWidth || 0)', null).toString())
	int browserHeightGap = webDriver.manage().window().getSize().height - Integer.parseInt(WebUiBuiltInKeywords.executeJavaScript('return (window.innerHeight || 0)', null).toString())
	float ratio = Float.parseFloat(WebUiBuiltInKeywords.executeJavaScript('return (window.devicePixelRatio || 1)', null).toString())
	int actualWidth = Math.round((width + browserWidthGap * ratio) / ratio)
	int actualHeight = Math.round((height + browserHeightGap * ratio) / ratio)
	WebUiBuiltInKeywords.setViewPortSize(actualWidth, actualHeight)
}

}

The following code compiles.
Please find the diffrence from your code:

package screenSize

import org.openqa.selenium.WebDriver

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.webui.driver.DriverFactory
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords

public class screenSize_1 {

	@Keyword(keywordObject='WEB')
	def static enhanced_SetViewportSize(int width, int height) {
		WebDriver webDriver = DriverFactory.getWebDriver()
		int browserWidthGap = webDriver.manage().window().getSize().width - Integer.parseInt(WebUiBuiltInKeywords.executeJavaScript('return (window.innerWidth || 0)', null).toString())
		int browserHeightGap = webDriver.manage().window().getSize().height - Integer.parseInt(WebUiBuiltInKeywords.executeJavaScript('return (window.innerHeight || 0)', null).toString())
		float ratio = Float.parseFloat(WebUiBuiltInKeywords.executeJavaScript('return (window.devicePixelRatio || 1)', null).toString())
		int actualWidth = Math.round((width + browserWidthGap * ratio) / ratio)
		int actualHeight = Math.round((height + browserHeightGap * ratio) / ratio)
		WebUiBuiltInKeywords.setViewPortSize(actualWidth, actualHeight)
	}
}
1 Like

It works! It’s magic!
Thank you very much!