Show List
Taking a screenshot
Here's a sample TestCafe test script that demonstrates taking a screenshot:
import { Selector } from 'testcafe';
fixture('Screenshot Test')
.page('https://www.example.com');
test('Take Screenshot', async t => {
// Take a screenshot of the entire page
await t
.takeScreenshot();
// Selector for an element to capture in the screenshot
const elementToCapture = Selector('#element-to-capture');
// Take a screenshot of a specific element
await t
.takeElementScreenshot(elementToCapture, 'screenshot.png');
});
In this test:
- We import the
Selector
function from TestCafe to select elements on the web page. - We define a fixture named 'Screenshot Test' and set the page to 'https://www.example.com'.
- Inside the test function:
- We use TestCafe's
takeScreenshot
action to capture a screenshot of the entire page. - We define a selector for an element (
elementToCapture
) that we want to capture in the screenshot. - We use TestCafe's
takeElementScreenshot
action to capture a screenshot of the specified element. The second argument specifies the file name for the screenshot (e.g.,'screenshot.png'
).
- We use TestCafe's
The screenshots will be saved in the directory from which you run the TestCafe test command. Make sure to replace 'https://www.example.com'
with the URL of the website you want to test, and adjust the selector accordingly based on the structure of the webpage you are testing. Additionally, replace '#element-to-capture'
with an appropriate selector for the element you want to capture.
Leave a Comment