Created
May 18, 2021 23:50
-
-
Save serg987/c422c54874863f686abc0b79ee25d066 to your computer and use it in GitHub Desktop.
Emulate (not exactly though) mobile screen in Firefox + Headless mode + save screenshot (JS)
This file contains 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
const webdriver = require('selenium-webdriver-3'); | |
const firefox = require('selenium-webdriver-3/firefox'); | |
const Profile = firefox.Profile; | |
const { DriverService } = require('selenium-webdriver-3/remote'); | |
const capabilities = webdriver.Capabilities.firefox(); | |
capabilities.setLoggingPrefs({ performance: 'ALL' }); | |
const binary = new firefox.Binary(); | |
binary.addArguments('-headless'); | |
binary.addArguments('-width=1000'); | |
binary.addArguments('-height=950'); | |
const options = new firefox.Options(capabilities); | |
const profile = new Profile(); | |
options.setBinary(binary); | |
profile.setPreference('general.useragent.override', 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19'); | |
profile.setPreference('layout.css.devPixelsPerPx', '2'); | |
options.setProfile(profile); | |
var srv = new DriverService.Builder("C:/Webdrivers/geckodriver.exe").setLoopback(true).build(); | |
let driver = firefox.Driver.createSession(options, srv); | |
try { | |
driver.manage().window().setSize(360, 640); | |
driver.get('http://www.yahoo.com'); | |
driver.sleep(3000); | |
driver.takeScreenshot().then( | |
function (image, err) { | |
require('fs').writeFile('out.png', image, 'base64', function (err) { | |
if (err) console.log(err); | |
}); | |
} | |
); | |
} finally { | |
driver.quit(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment