Why Realistic Test Data is Important in Automation
One of the most overlooked aspects in automation is the quality of test data. Hardcoded test data, for example, names like “Santosh Adhikari” and reused emails like [email protected], can lead to duplicate entry errors. To solve such issues, Ruby developers can pair Selenium with two powerful libraries: Faker and FactoryBot. While Faker helps developers to generate random but realistic data, FactoryBot offers a clean and well-structured way to manage it.
Interesting Right? Now let’s install the required gems in our system.
gem 'selenium-webdriver'
gem 'rspec'
gem 'faker'
gem 'factory_bot'
Now to update your gem files run following command:
bundle install
Now you’re all set to combine the power of dynamic test data for your test autmations.
Generating Realistic Test Data With Faker
The Faker gem generates data that mimics real-world information, such as Name, Email, Address, Phone Number, and credit card details.
Now, let’s examine a basic example of generating realistic test data using Faker.
require 'faker'
puts Faker::Name.name # => "Ava Brooks"
puts Faker::Internet.email # => "[email protected]"
puts Faker::Address.city # => "New York"
You can use different approaches to manage data, such as creating Enums, Models, et with Faker Data
Let’s go through following example:
module UserRole
ADMIN = 'admin'
USER = 'user'
GUEST = 'guest'
ALL = [ADMIN, USER, GUEST]
end
# Usage with Faker and FactoryBot:
FactoryBot.define do
factory :user do
name { Faker::Name.name }
email { Faker::Internet.email }
role { UserRole::ALL.sample }
end
end
Organizing Test Data with FactoryBot
FactoryBot is usually tied to Rails apps, but it works just as well with plain Ruby. FactoryBot allows you to define blueprints/factories for your test data in a reusable, centralized manner.
Defining a Factory
# factories/user_factory.rb
require 'factory_bot'
require 'faker'
FactoryBot.define do
factory :user do
name { Faker::Name.name }
email { Faker::Internet.email }
password { 'SecurePass123!' }
city { Faker::Address.city }
end
end
To use the factory in your test:
include FactoryBot::Syntax::Methods
user = build(:user)
Now you can use the realistic values each time you run the test. user.name, user.email, etc, contain fresh data for your tests.
Let’s go through a real-world scenario
require 'selenium-webdriver'
require 'factory_bot'
require 'faker'
# --- Factory Definition ---
FactoryBot.define do
factory :user do
first_name { Faker::Name.first_name }
last_name { Faker::Name.last_name }
email { Faker::Internet.email }
phone { Faker::PhoneNumber.cell_phone.delete("^0-9")[0..9] } # 10-digit number
address { Faker::Address.full_address }
end
end
# --- Test Suite ---
include FactoryBot::Syntax::Methods
describe 'DemoQA practice form' do
before(:all) do
FactoryBot.find_definitions
@driver = Selenium::WebDriver.for :chrome
@driver.manage.timeouts.implicit_wait = 10
@driver.navigate.to 'https://demoqa.com/automation-practice-form'
@driver.manage.window.maximize
end
it 'fills out form with realistic fake data' do
user = build(:user)
@driver.find_element(id: 'firstName').send_keys(user.first_name)
@driver.find_element(id: 'lastName').send_keys(user.last_name)
@driver.find_element(id: 'userEmail').send_keys(user.email)
# Gender radio button
@driver.find_element(xpath: "//label[text()='Male']").click
@driver.find_element(id: 'userNumber').send_keys(user.phone)
# Date of Birth
@driver.find_element(id: 'dateOfBirthInput').click
@driver.find_element(css: '.react-datepicker__day--015').click rescue nil
# Subjects - type and select
subject_input = @driver.find_element(id: 'subjectsInput')
subject_input.send_keys('Math')
subject_input.send_keys(:enter)
# Hobbies - click checkbox
@driver.find_element(xpath: "//label[text()='Sports']").click
# Address
@driver.find_element(id: 'currentAddress').send_keys(user.address)
@driver.execute_script('window.scrollBy(0, 300)')
@driver.find_element(id: 'submit').click
sleep 2 # View confirmation modal
end
after(:all) do
@driver.quit
end
end
Using Faker and FactoryBot with Selenium in Ruby not only improves the quality of your automated tests but also boosts maintainability and test coverage. It simulates real-world data, so it helps to behave like it, helps avoid stale data issues, and reduces manual test data setup dramatically.
If you are still hardcoding test data in 2025, then it’s time to level up. Cheers.
Happy Testing
Happy Coding