Last active
September 18, 2021 22:32
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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