How to handle Platform Specific Keys (Ex: Keys.Control in windows not work in Mac OS ) while doing Cross Platform Testing?

How to handle Platform Specific Keys (Ex: Keys.Control in windows not work in Mac OS ) while doing Cross Platform Testing?

Hi Deepak,

WebDriver helps to identify the browser, browser version, and platform. If it’s running in Local then the below code gives the platform name

 Capabilities mycaps=((RemoteWebDriver) driverObj).getCapabilities();
 System.out.println(mycaps.getPlatformName()); 

If it’s running any cloud platform like LambdaTest, then it depends on the Capability values passed to the RemoteWebdriver Object. For Ex:

To execute test cases in LambdaTest Platform below are the Capabilities , ( you can copy the capabilities from here )

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("browserName", "Chrome");
    capabilities.setCapability("browserVersion", "99.0");
    HashMap<String, Object> ltOptions = new HashMap<String, Object>();
    ltOptions.put("user", USER_NAME;
    ltOptions.put("accessKey", AUTH_KEY;
    ltOptions.put("build", "your build name");
    ltOptions.put("name", "platformtest");
    ltOptions.put("platformName", "Windows 11");
   capabilities.setCapability("LT:Options", ltOptions);
   driver = new RemoteWebDriver(new URL("https://" + username + ":" + authkey + hub), capabilities);

Now to retrieve the Plateform name from this driver Object,

 Map retrivedCaps=(HashMap)capabilities.getCapability("LT:Options")) 
//this Capability stored as Hasmap 
String currentRemotePlatform=retrivedCaps.get("platformName").toString(); //this statement fetch the value of current platform where selenium executes test cases.
//This if contion pass keys based on platform , 
 if (currentRemotePlatform.toLowerCase().contains("windows")) {
         element.sendKeys(Keys.chord(Keys.CONTROL, "a", Keys.DELETE));
   } else {
           element.sendKeys(Keys.chord(Keys.COMMAND, "a", Keys.DELETE));
 }

Hope this gives the idea, of how to perform actions based on the platform . To more about Selenium driver capabilities read

1 Like