Manipulate Browser User Agent, Emulate Device Dimensions in Selenium 4
In this article we will learn how to fake and manipulate a browser user agent plus how to emulate device dimensions (mobile etc).
To understand this, let us go to the website
https://www.whatismybrowser.com/detect/what-is-my-user-agent
On this website, you would see your ‘User Agent’ that comprises of your system related information
The same user agent information will also be seen when you inspect any website and go to ‘Network’, see below
The above highlighted information of user agent matches the user agent information that we saw on the website https://www.whatismybrowser.com/detect/what-is-my-user-agent
Now, we can fake this user agent information, so that the other person cannot understand from which user agent the request is coming.
So, we can manipulate the browser information and send the fake browser information as if the user who accessed this app is having ubuntu machine, edge browser etc.
We can achieve this by writing the following peace of code.
Line#22 will load our current user agent information as it is. In line#25, we are creating a fake user agent string. In line#27 we are overriding our current user agent settings. Finally, in line#29, we are loading the manipulated user agent info
Save and run. Notice below that the manipulated user agent info gets loaded. It is showing as if we are using Ubuntu linux system
So this is how we can send a manipulated user agent info.
Let us now see how to emulate device dimensions (mobile etc).
Let us say you want to see how your test run would look like when you execute test on a mobile (from your laptop). So to that, we have to simulate the mobile device dimensions as shown in lines 26,27 below. Line#25 sets the device as mobile. In Line#30, we are overriding our current device metrics
Run this test, see below, we see the output as if we executed the test on a mobile
You can go to the site and study the various parameter details if you want to
https://chromedevtools.github.io/devtools-protocol/tot/Emulation/
You can use another approach as well to achieve the same results (without using executeCdpCommand), see below
Run the script
The only difference in this approach is that the browser comes back to default mode after the test execution as shown below. This is an expected behaviour with this approach
Code snippet (user agent)
package sel4scripts;
import java.util.Optional;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v85.network.Network;
import io.github.bonigarcia.wdm.WebDriverManager;
public class UserAgent {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
driver.get("https://www.whatismybrowser.com/detect/what-is-my-user-agent");
Thread.sleep(2000);
String customUserAgent = "Mozilla/5.0 (Ubuntu LST 15) AppleWebKit/533.33 (KHTML, like Gecko) Chrome/96.0.4664.11 Safari/537.33";
devTools.send(Network.setUserAgentOverride(customUserAgent, Optional.empty(), Optional.empty(), Optional.empty()));
driver.get("https://www.whatismybrowser.com/detect/what-is-my-user-agent");
}
}
Code snippet (emulate device dimension, method 1)
package sel4scripts;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import io.github.bonigarcia.wdm.WebDriverManager;
public class EmulateDeviceScreenDimensions {
public static void main(String[] args) {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
DevTools devTools = ((ChromeDriver) driver).getDevTools();
devTools.createSession();
//Map<String, Object> deviceMetrics = new HashMap<String, Object>() {
Map deviceMetrics = new HashMap();
deviceMetrics.put("mobile", true);
deviceMetrics.put("width", 300);
deviceMetrics.put("height", 900);
deviceMetrics.put("deviceScaleFactor", 50);
((ChromeDriver) driver).executeCdpCommand("Emulation.setDeviceMetricsOverride", deviceMetrics);
driver.get("https://www.selenium-tutorial.com/p/devops-training");
}
}
Code snippet (method 2, device simulation)
package sel4scripts;
import java.util.Optional;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.devtools.DevTools;
import org.openqa.selenium.devtools.v95.emulation.Emulation;
import io.github.bonigarcia.wdm.WebDriverManager;
public class EmulateDeviceScreenMethod2 {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeDriver driver = new ChromeDriver();
DevTools devTools = driver.getDevTools();
devTools.createSession();
devTools.send(Emulation.setDeviceMetricsOverride(300, 900, 50, true, Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty()));
driver.get("https://www.selenium-tutorial.com/p/selenium-4-0-latest-all-new-features-docker-integration");
}
}
Thank you for reading!