If you need to find out what went wrong during an end-to-end test, a recorded video of the browser with all its interactions can be a time saver. What is true for Selenium Grid is also a good idea for Playwright. Let’s look at how we can create these videos with in NUnit and a library project.
Videos in library projects
We can copy the code from the documentation and put it into our application without any changes:
1 2 3 4 5 6 7 8 9 10 11 |
var context = await browser.NewContextAsync( new() { RecordVideoDir = "videos/" }); var Page = await context.NewPageAsync(); // your code await context.CloseAsync(); |
If we run our application, we must wait until it finishes before we find the recording in the videos folder:
Videos in NUnit
If we try to copy the code from the documentation into our NUnit tests, it will not work. We are too late to influence the options. Instead, we need to override the virtual method ContextOptions() that we get by inheriting our test class from PageTest
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public override BrowserNewContextOptions ContextOptions() { return new BrowserNewContextOptions() { RecordVideoDir = "videos/", ViewportSize = new ViewportSize { Height = 720, Width = 1280 }, RecordVideoSize = new RecordVideoSize { Height = 720, Width = 1280 } }; } |
When the test finishes, the recording shows up in the videos folder as it did in the library project.
Create screenshots
We can create screenshots on the Page object with the method ScreenshotAsync(). Since this method is on the Page object, we can use the same code in NUnit and in a library project:
1 2 3 4 5 |
await Page.ScreenshotAsync(new() { Path = "screenshot_full.png", FullPage = true, }); |
If we use the parameter FullPage, we get a screenshot of the full page, including the parts that are not visible in the Browser:
If we omit FullPage
or set it to false, we get a screenshot of only the visible part of the page:
If you should use the FullPage
option or not depends on your use case. For figuring out what went wrong I prefer to see exactly what the browser sees.
Next
Creating videos out of the box is a great feature. Unfortunately, the official documentation is not that useful if you want to create videos in NUnit. I hope this post clear things up. Next week we look at little tweaks to customise Playwright.