Random Data Generator | Bondar Academy
Course: Playwright UI Testing with TypeScript
Module: Advanced Tricks and Techniques
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explored the use of the Faker library to generate random test data for applications. This is particularly useful for creating placeholder data like names and addresses, which helps avoid static entries in tests. Key Concepts: Faker Library: A popular JavaScript library for generating fake data. Installation: To install Faker, use the following command in the terminal: npm install faker --save-dev --force After installation, import the library into your test file using: import faker from 'faker'; Generating Random Data: To create a random full name, use: const randomFullName = faker.name.findName(); For generating random emails, combine the full name with a random number: const randomEmail = `${randomFullName.replace(' ', '').toLowerCase()}${faker.random.number({ min: 1000, max: 9999 })}@test.com`; Benefits: Using Faker allows for: Unique data entries for each test run, preventing duplication in databases. Easy customization of generated data, such as specifying gender or name formats. In summary, the Faker library simplifies the process of generating diverse test data, enhancing the reliability and effectiveness of testing procedures.
Video Transcript
In this lesson I will show you how you can use a Faker library to generate a random test data for your test. For example random names or random address or any other random information that you need as a placeholder just to fill out some applications inside of your test. This library is very helpful to use and very easy to use so I will show you it in this lesson. Let's get into it. So let's look into this particular test. Here we are filling out signing in line form with the John Smith as a first name and email john at test.com. Imagine the real-world scenario when you need to run this test again and again and your database will be filled out with John Smith's and these emails and let's say if you want to later find the created user somehow in the database to perform a different operations then it will be tricky to search this member just by this name because this is just a static name and ideally would be to have this name every time different right so different names for every test run and we can use data generator tool or data generator library that can randomly generate for us this type of test data. So the best approach to handle this is to use a data generation library that called faker. So go to the Google and type faker.js. Enter and scroll a little bit down here npm.js faker.js faker and click on this. So this is the one of the most popular libraries in JavaScript to create different fake data. So in order to use this just copy this URL, go back to application and inside of the terminal we need to paste this command and also we want to save it into the dependencies dash dash save dash dev and dash dash force to make sure that we will not have any conflict dependencies and we want to skip those. And hit enter. All right library is installed if we go back to package.json scroll down to dev dependencies. Here we go we see that faker was added inside of our framework. Now we need to import this library into the test file. Import faker from faker.js dash faker. All right and now we can simply use this library. So how to do that? Let's say we want to create first and last name instead of the John Smith. So I create a new constant and call it full random full name equals to and then call a faker library. Faker dot and then look we have a lot of different options what faker library can do. It has colors, commerce, companies, different topics, finance, git and so on. And we are looking for person over here. Here we go. Person enter and again dot. And this is what we can do for the person. Biography first and last name, full name, gender, job agenda and so on. So we are looking for full name method. This method will return us just a random full name. But additionally you can customize this method with different options. For example if you want to be returned only the different last name but the specific first name you can specify. All right I want a first name as John but the last name always will be different. Or if you want the name to be returned by particular sex. Let's say you want to be always a female or you can just provide only a male. So all the names that will be generated by faker will be only male emails and so on. So you can provide those details as attributes. For us it doesn't matter so we keep it as is. And random full name right now we can use instead of the John Smith. I am replacing this constant and we can see how it works. So let me copy the last three lines so we will not go to the date picker page and just stay here in the forms layout page to see how faker works. So run this test. And you can see that name was generated. If I close this and run it one more time now the new name is generated Tracy Swift and so on. And every time you call this faker person full name it randomly will generate whatever full name it is. So now let's replace the second argument. Second argument is email. So let's create email based on our random full name and add few numbers over there also using faker. So I create a new constant const random email equals two and we will reuse part of the data created before. So let's say we want to create email with a random full name that we just generated. Then the next argument we want to add a certain numbers and faker can help us with this. Again we call faker dot number. We are looking for a number. Here we go. Number dot and we want integer. And we want let's say a specific length. So again integer method provide different parameters. If we just call the integer it will provide a long number like that. If you put int 100 it will give us any random number within the range of 100. We can also put min or max values. That also works for us. And let's say we want to generate any number within 1000. And the last thing I add the rest of the string as test dot com because I want to keep our test domain for example as a constant. That's it. Random email and I'm replacing our email with this value. And let's run this test. Let's see the result. Okay we see the first name is printed and also we see a first name with add test dot com. But we have a little problem because our first name has separation between first and last name. We have a space and we cannot have a space and email. So we need quickly fix this. How to do that? Very easy. We can use just the JavaScript and call a JavaScript method dot replace. And we want to replace space with no space. Something like that. Let's run it one more time. All right. And right now everything looks perfect. We have first and last name and first and last name 5 at test dot com. If we run it one more time we have Gary and Gary with number 7 3 5. So we have a pretty good unique combination right now of our different emails. And if we run this test again and again most likely we never create a duplication inside of the database and we always be able to search the test member inside of the database if we later need to track this person for any other validations or something like that. So Faker library is a very powerful library. You can generate a lot of different test data for your test. Very simple and very convenient to use. All right. So let's quickly summarize what we did in this lesson. In this lesson we installed the Faker library. It's installed inside of the project in dev dependencies right here. And then we use this Faker library to create a random full name and to create that random email by using JavaScript interpolation combining full name and random number at test dot com. And we used those randomly created values as an argument for the methods to fill out the form. All right. Thank you guys and see you in the next lesson.