It can be a pain.
I use to have to automate a hospital application that required that I register a new patient in every test before the rest of the workflow could proceed.
The only issue was that each username and social security number had to be unique.
Hardcoded data would not work in this scenario and seeding the database with users beforehand than cleaning up after was messy and time-consuming.
What to do? Faker to the rescue.
What is a Data Faker?
A faker allows you to dynamically create real looking data like names, phone numbers, addresses, SSN — all kinds of stuff.
Lots of times you don’t care about what these values like name or address are so long as you know that your web application will save them correctly.
Using a faker allows you to model this data and create fresh dynamic values for these fields ever test run.
Selenium Data Faker Example
Say for example I’m testing an application as if I was a user in Italy. Using faker and passing it a locale of it_IT(Italian) it will generate Italian names and address. Here is an example using Selenium with Python with a Faker
from selenium import webdriver from selenium.webdriver.common.keys import Keys from faker import Faker fake = Faker('it_IT') full_name = (fake.name()) print(full_name) print(fake.address()) first,last = full_name.split() driver = webdriver.Chrome('C://Download/chromedriver.exe') driver.get("https://joecolantonio.com/SeleniumTestPage.html") assert "WebDriver" in driver.title first_name = driver.find_element_by_name("fname") last_name = driver.find_element_by_name("lname") first_name.clear() first_name.send_keys(first) last_name.send_keys(last) first_name.send_keys(Keys.RETURN) driver.close()
This simple code generates a dynamic name which I parse into first and last names. I then use those values as inputs in my test applications first and last name fields.
It’s as simple as that.
Of course, you can get as crazy as you want, but as you can see this is powerful even in a simple scenario.
When You Might Want to Use a Faker in Your Test Automation
As we have seen using a faker for values that need to be unique like SSN is a prime user case for utilizing a faker.
- If you are demoing a product to a customer having realistic looking data can help improve the experience. Faker can seed your application with realistic looking data.
- Faster and less maintenance than using hardcoded values
- Since you are using dynamic data, it might uncover an unanticipated bug.
- Test that requires a broad set of test data with a bulk activity.
How to Find a Faker for Your Automation Testing Language
If you’re using an automation tool that allows you to import libraries, you are in luck since there are a bunch of fakers available in most languages. Don’t waste your time creating your custom code to do what an exiting open source library can already do.
There are Faker libraries in many languages, check them out: (Let me know if I missed one of your favorites and I will add it to the list)
Don’t over Fake it
Like anything sometimes going overboard can cause more issue than its worth.
Use your judgment when using a Faker and don’t over complicate your test script with unnecessary dynamic values if you don’t have to.
Source link https://www.joecolantonio.com/faker-test-data/