Saturday, May 4, 2024
HomeJavaScriptJavaScript testing #18. E2E Playwright assessments for importing and downloading recordsdata

JavaScript testing #18. E2E Playwright assessments for importing and downloading recordsdata


Our functions generally embody options that permit customers to pick out recordsdata from their onerous drives, and a few functionalities would possibly let customers obtain recordsdata. Implementing Finish-to-Finish assessments that make sure that every thing works as anticipated may not appear simple at first.

On this article, we create a easy React software that lets customers select a JSON file from their machine and obtain a formatted model. Then, we use Playwright to check the file enter and confirm if the downloaded file is appropriate.

An software that codecs JSON recordsdata

To start out, let’s create a element with a file enter.

JsonFormatter.tsx

Underneath the hood, we extract the textual content from the chosen file and parse it as JSON. Then, we format it and ship it again to the person.

useJsonFormatter.tsx

If the person supplies a sound JSON file, they get a formatted model with the identical file title.

 

If the person selects an invalid file, we show the error message.

Getting ready the required recordsdata

To check our software, we should put together two recordsdata – one with legitimate JSON and one with invalid knowledge. Let’s put them within the listing known as
within the
folder.

assessments/assets/knowledge.json

assessments/assets/invalidData.txt

Interacting with the file enter

To work together with the file enter by way of our Finish-to-Finish assessments, we should first find it on the web page. One of many simple methods of doing that’s to search out it by way of the label textual content.

Now, we should present the trail to our recordsdata with the
operate. A method is to offer a relative path to the file. The trail can be resolved relative to the present working listing containing our
file.

We will use the above to check what occurs when the person supplies each legitimate and invalid recordsdata.

JsonFormatter.check.tsx

To make use of
with out offering the complete URL we have to have the
 configuration arrange. If you wish to know extra, try JavaScript testing #17. Introduction to Finish-to-Finish testing with Playwright

Testing the title of the downloaded file

Each time the browser downloads a file, Playwright emits an occasion. To start out listening for it, we have to name the
operate, which returns a promise.

The essential facet is to begin ready for the occasion earlier than the browser downloads the file, however don’t use the
key phrase till it’s downloaded.

Now, we will use the
operate to check if the downloaded file’s title is appropriate.

JsonFormatter.check.tsx

Checking the contents of the file

We additionally need to verify if our software accurately formatted the JSON file. We have to use the
operate that returns a learn stream to try this.

If you wish to know extra about streams in Node.js, try Node.js TypeScript #4. Paused and flowing modes of a readable stream

Playwright assessments run in a Node.js atmosphere. Because of that, we will use numerous APIs constructed into Node.js. Essentially the most simple means of studying the stream is to make use of the
operate from
.

Abstract

On this article, we’ve realized find out how to use Playwright to put in writing Finish-to-Finish assessments to confirm options that embody coping with recordsdata. This included studying find out how to simulate selecting a file from the onerous drive. In addition to that, we additionally realized find out how to confirm downloaded recordsdata by checking their names and content material to make sure they include the right knowledge. Fortuitously, Playwright makes working with recordsdata very simple.

Collection Navigation

<< JavaScript testing #17. Introduction to Finish-to-Finish testing with Playwright

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments