Skip to content

Instantly share code, notes, and snippets.

@ShamaUgale
Last active September 18, 2021 22:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ShamaUgale/f3ef48dbed32ed1db8baea5ee6a2176a to your computer and use it in GitHub Desktop.
Save ShamaUgale/f3ef48dbed32ed1db8baea5ee6a2176a to your computer and use it in GitHub Desktop.
This is an example of setting the device mode with height width and scalefactor with devtools and selenium4
package com.devtools;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import java.util.HashMap;
import java.util.Map;
public class SetDeviceMode {
final static String PROJECT_PATH = System.getProperty("user.dir");
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", PROJECT_PATH + "/src/main/resources/chromedriver");
ChromeDriver driver;
driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
Map deviceMetrics = new HashMap()
{{
put("width", 600);
put("height", 1000);
put("mobile", true);
put("deviceScaleFactor", 50);
}};
driver.executeCdpCommand("Emulation.setDeviceMetricsOverride", deviceMetrics);
driver.get("https://www.google.com");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment