This guide shows you how to capture screenshots of Hindi-language websites using Cypress, a popular end-to-end testing framework. You’ll learn how to set up a simple test that navigates to a Hindi site, waits for content to load, and saves full-page or element-specific screenshots to your project. We’ll cover the essential commands, common pitfalls like missing fonts or dynamic content, and how to troubleshoot when screenshots don’t come out as expected.
What You’ll Need
- A Node.js project (version 16 or later recommended) with npm installed.
- Cypress installed as a dev dependency:
npm install --save-dev cypress. - A code editor (like VS Code) and a terminal.
- Basic familiarity with JavaScript and Cypress test syntax.
Step-by-Step
- Open Cypress Test Runner: In your terminal, run
npx cypress open. This launches the Cypress GUI. If it’s your first time, Cypress will create a default folder structure withcypress/e2eandcypress/screenshots. - Create a new test file: Inside
cypress/e2e, create a file namedhindi-screenshot.spec.js. - Write the test skeleton: Open the file and add the following code:
describe('Hindi Website Screenshots', () => { it('captures full page screenshot', () => { cy.visit('https://www.hindustantimes.com/'); // Example Hindi news site cy.screenshot('hindi-homepage'); }); });Replace the URL with the Hindi website you want to capture. The
cy.screenshot()command takes a filename (without extension) and saves it tocypress/screenshotsby default. - Run the test: In the Cypress Test Runner, click on
hindi-screenshot.spec.js. The test will run in a browser window, and after it completes, you’ll see a screenshot saved in thecypress/screenshotsfolder. The path is typicallycypress/screenshots/hindi-screenshot.spec.js/hindi-homepage.png. - Capture a specific element: To capture only a particular part of the page (like a header or a news article), use a selector. For example:
cy.get('header').screenshot('hindi-header');This will capture only the header element. Ensure the element is visible and has loaded before taking the screenshot.
- Wait for content to load: Hindi websites often load content dynamically via JavaScript. Use
cy.intercept()orcy.wait()to ensure the page is fully rendered. For example:cy.visit('https://example.com'); cy.intercept('GET', '**/api/news').as('news'); cy.wait('@news'); // Wait for the API call to finish cy.screenshot('after-news-load');This ensures you capture the page after the dynamic content appears.
- Set viewport size: Screenshots are taken at the current viewport size. To capture a desktop-width screenshot, set the viewport before visiting:
cy.viewport(1280, 720); cy.visit('https://example.com'); cy.screenshot('desktop-view'); - Take full-page screenshots: By default,
cy.screenshot()captures only the visible viewport. To capture the entire scrollable page, usefullPage: true:cy.screenshot('full-page', { fullPage: true }); - Run headlessly for batch screenshots: To capture screenshots without opening the GUI (e.g., in CI), run
npx cypress run --spec cypress/e2e/hindi-screenshot.spec.js. Screenshots are saved automatically when a test fails, but you can also usecy.screenshot()in passing tests.
Common Mistakes
- Not waiting for fonts: Hindi text often uses custom web fonts (like Noto Sans Devanagari). If the font hasn’t loaded, the screenshot may show fallback fonts or blank boxes. Use
cy.document().then(doc => doc.fonts.ready)to wait for fonts. - Capturing before dynamic content loads: Many Hindi news sites load articles via AJAX. If you screenshot immediately after
cy.visit(), you might miss most of the content. Always wait for specific network requests or usecy.wait(1000)(though this is not recommended for production tests). - Incorrect file paths: Screenshots are saved in a subfolder named after the spec file. If you can’t find your screenshot, check
cypress/screenshots/<spec-name>/. - Not setting a viewport: Mobile vs desktop layouts differ. If you don’t set a viewport, Cypress uses the default (usually 1000×660). For a consistent desktop screenshot, always set
cy.viewport().
Tips
- Use environment variables for URLs: Instead of hardcoding the URL, use
Cypress.env('BASE_URL')to make your tests reusable across environments. - Organize screenshots by test case: Use meaningful names like
homepage-top-newsorarticle-sectionto easily identify what each screenshot shows. - Add a delay for animations: If the site has carousels or sliders, wait for them to settle:
cy.wait(500)after loading. - Use
cy.pause()for debugging: If a screenshot doesn’t look right, addcy.pause()before the screenshot to manually inspect the page in the Test Runner. - Capture screenshots on failure: Cypress automatically captures a screenshot when a test fails. This is useful for debugging, but for intentional screenshots, use
cy.screenshot()explicitly.
Troubleshooting
- Screenshot is blank or black: This often happens if the page is still loading or the browser window is minimized. Ensure you have
cy.visit()and waits before the screenshot. Also, check that the browser window is not in headless mode without proper rendering. - Hindi text appears as boxes (tofu): This is a font issue. Wait for fonts using
cy.document().then(doc => doc.fonts.ready)or usecy.wait(1000)to allow font loading. You can also preload fonts by visiting a font resource. - Screenshot is not saved: Verify the folder exists and you have write permissions. Also, ensure you are using
cy.screenshot()correctly—it should be called after the page is loaded, not inside acy.then()without a chain. - Element not found for screenshot: If you’re using
cy.get()to target an element, make sure the selector is correct and the element is visible. Usecy.contains('text')to find elements by text, which might be easier for Hindi content. - Full-page screenshot cuts off: For very long pages, full-page screenshots can be memory-intensive. Try setting
fullPage: trueand also setcapture: 'fullPage'in your Cypress configuration if needed. - Test times out: If the website is slow, increase the default timeout:
cy.visit(url, { timeout: 10000 }).
Frequently Asked Questions
Can I take screenshots of a Hindi website without opening the Cypress GUI?
Yes, run npx cypress run in the terminal. Screenshots are saved to the same location, and you can specify a spec file to limit the run.
How do I capture a screenshot of a specific article on a Hindi news site?
Use a CSS selector that uniquely identifies the article, such as cy.get('article').first().screenshot('first-article') or cy.contains('h2', 'headline text').closest('article').screenshot('article').
Why does my screenshot show only the top part of the page?
By default, cy.screenshot() captures only the viewport. Use { fullPage: true } to capture the entire scrollable page.
Can I capture screenshots in different devices (mobile, tablet) for a Hindi site?
Yes, use cy.viewport(375, 812) for iPhone X or cy.viewport(768, 1024) for iPad. Set the viewport before visiting the site.
How do I avoid capturing cookie consent popups?
You can dismiss the popup before taking the screenshot, for example: cy.get('button').contains('Accept').click() (adjust the selector to match the site’s popup). Alternatively, use cy.setCookie('consent', 'true') before visiting.
Is it possible to take screenshots of a Hindi website that requires login?
Yes, use cy.request() to perform a login API call or use cy.session() to persist the login state. Then visit the protected page and take a screenshot.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It