keepcalculator

06 Numbers and Powers

Cypress Screenshots of Hindi Websites: How-To

Card No. 06·54623 · Updated · Standard formula, shown below

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

  1. 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 with cypress/e2e and cypress/screenshots.
  2. Create a new test file: Inside cypress/e2e, create a file named hindi-screenshot.spec.js.
  3. 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 to cypress/screenshots by default.

  4. 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 the cypress/screenshots folder. The path is typically cypress/screenshots/hindi-screenshot.spec.js/hindi-homepage.png.
  5. 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.

  6. Wait for content to load: Hindi websites often load content dynamically via JavaScript. Use cy.intercept() or cy.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.

  7. 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');
  8. Take full-page screenshots: By default, cy.screenshot() captures only the visible viewport. To capture the entire scrollable page, use fullPage: true:
    cy.screenshot('full-page', { fullPage: true });
  9. 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 use cy.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 use cy.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-news or article-section to 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, add cy.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 use cy.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 a cy.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. Use cy.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: true and also set capture: '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

The whole Numbers and Powers drawer →