Rayrun
← Back to Discord Forum

How to get device data like screen width within a test (in Node.js)?

I have a few projects with different device data (screen sizes), and I want to take screenshots and save them to files with names depending on the screen size. How to do that?

This thread is trying to answer question "How to get device data like screen width within a test in Node.js?"

2 replies

you just need to input testInfo and then you can use testInfo.viewport or timeout .....

thanks!

Answer

Capture Screenshots with Playwright

First, let's install Playwright in your Node.js project. Run the following command in your project directory:

npm install playwright

Next, import the necessary functions from Playwright. If you're using Chromium browser, import it like this:

const { chromium } = require('playwright');

Now, launch a browser instance. This will create a new browser window for capturing screenshots.

const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();

Navigate to the webpage you want to capture a screenshot from using the page.goto() method.

await page.goto('https://ray.run');

To capture a screenshot and save it as a file, use the page.screenshot() method with appropriate options.

await page.screenshot({ path: 'screenshot.png' });

The screenshot will be saved as "screenshot.png" in your current working directory. Repeat these steps for different projects or screen sizes.

Finally, close the browser instance using browser.close() method to free up system resources.

await browser.close();

That's it! You've captured screenshots and saved them to files with Playwright. Remember to customize the code according to your specific requirements and project structure.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.