The Issue at Hand
There appears to be a strong desire to manipulate the values returned by window.screen.width
and window.screen.height
. Normally, this is not feasible, unless utilizing Chrome's device mode (along with the accompanying toolbar). So, how about triggering those options?
// initiating inspector and responsive design mode
driver.actions()
.keyDown(Key.CONTROL)
.keyDown(Key.SHIFT)
.sendKeys('im')
.keyUp(Key.SHIFT)
.keyUp(Key.CONTROL)
.perform();
Regrettably, this approach won't succeed in Chrome. As stated in that answer:
The Chrome driver utilizes the Chrome remote debugging protocol for browser communication. This protocol aligns with what the developer console employs as well. Unfortunately, Chrome restricts only one client connection via the protocol at any given time, implying either the developer tools or the driver, but not both concurrently. — JimEvans
What a letdown. It seems like success can be achieved in this aspect using Firefox, though. Nonetheless, executing actions using Selenium + geckodriver is currently unattainable. Furthermore, there isn't a JavaScript method to activate this mode directly. On a brighter note, we are able to send keys to an element.
A Potential Resolution
The following method has worked effectively for me.
var webdriver = require('selenium-webdriver'),
firefox = require('selenium-webdriver/firefox'),
By = webdriver.By,
Key = webdriver.Key,
driver = null;
// setting up driver
var profile = new firefox.Profile(),
devicePreset = [{
width: 640,
height: 480,
key: '640x480',
name: 'Mobile Device'
}];
profile.setPreference('devtools.responsiveUI.presets',
JSON.stringify(devicePreset));
var opts = new firefox.Options();
opts.setProfile(profile);
var builder = new webdriver.Builder().forBrowser('firefox');
builder.setFirefoxOptions(opts);
driver = builder.build();
driver.get('http://www.google.com');
// activating responsive design mode
driver.findElement(By.css('input[name="q"]'))
.sendKeys(Key.chord(Key.CONTROL, Key.SHIFT, 'm'));
driver.get('https://www.iplocation.net/find-ip-address');
Please note that I have configured a preference in Firefox indicating the desired screen size for the responsive design mode. Feel free to adjust this according to your preferential screen specifications.