8000 [Feature] How to use setMobileEmulation for correct window resize · Issue #310 · google/webdriver.dart · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Feature] How to use setMobileEmulation for correct window resize #310

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
munrocket opened this issue Dec 20, 2024 · 1 comment
Closed

Comments

@munrocket
Copy link

I wanted to fix flutter/issues/136109 in Flutter for proper window size and E2E screenshot comparison. I realised that the same resize behaviour in ordinary Selenium WebDriver script, but if I adding setMobileEmulation all become correct. But I can't find the same feature here. Can you validate my thought, maybe this feature already supported in Dart version.

import { Builder } from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome.js';
import { existsSync, mkdirSync, writeFileSync } from 'fs';

const options = new chrome.Options();
options.addArguments('--no-sandbox');

// BUG FIX >>> 
options.setMobileEmulation({
    deviceMetrics: {
        width: 393,
        height: 852,
        pixelRatio: 3.0
    },
    userAgent: 'Mozilla/5.0 (Linux; Android 15) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.6778.200 Mobile Safari/537.36'
});
// <<<

const driver = new Builder()
    .forBrowser('chrome')
    .usingServer('http://localhost:4444') // Ensure this server is running
    .setChromeOptions(options)
    .build();

(async function takeScreenshot() {
    try {
        // Set the window size to match the emulated device
        await driver.manage().window().setRect({ width: 393, height: 852 });

        // Create the screenshots directory if it doesn't exist
        if (!existsSync('screenshots')) {
            mkdirSync('screenshots');
        }

        // Take a screenshot and save it
        const screenshot = await driver.takeScreenshot();
        writeFileSync('screenshots/screenshot.png', screenshot, 'base64');
    } catch (error) {
        console.error('An error occurred:', error);
    } finally {
        await driver.quit();
    }
})();
@munrocket
Copy link
Author

Mobile emulation works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0