<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Faker and FactoryBot &#8211; TestAutomationLabs</title>
	<atom:link href="https://testautomationlabs.com/tag/faker-and-factorybot/feed/" rel="self" type="application/rss+xml" />
	<link>https://testautomationlabs.com</link>
	<description>Quality Matters</description>
	<lastBuildDate>Thu, 15 May 2025 15:45:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://testautomationlabs.com/wp-content/uploads/2023/08/imageedit_2_5514276710-150x150.png</url>
	<title>Faker and FactoryBot &#8211; TestAutomationLabs</title>
	<link>https://testautomationlabs.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to use Faker and FactoryBot in Selenium Ruby Tests in 2025</title>
		<link>https://testautomationlabs.com/use-faker-and-factorybot-in-selenium-ruby/</link>
					<comments>https://testautomationlabs.com/use-faker-and-factorybot-in-selenium-ruby/#respond</comments>
		
		<dc:creator><![CDATA[TestAutomationLabs]]></dc:creator>
		<pubDate>Thu, 15 May 2025 15:45:36 +0000</pubDate>
				<category><![CDATA[Automation Testing]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Selenium]]></category>
		<category><![CDATA[Faker and FactoryBot]]></category>
		<category><![CDATA[How to use Faker and FactoryBot in Selenium Ruby Tests]]></category>
		<guid isPermaLink="false">https://testautomationlabs.com/?p=463</guid>

					<description><![CDATA[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 &#8220;Santosh Adhikari&#8221; and reused emails like test@example.com, can lead to duplicate entry errors. To solve such issues, Ruby developers can pair Selenium with two powerful libraries: [&#8230;]]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Why Realistic Test Data is Important in Automation</h2>



<p>One of the most overlooked aspects in automation is the quality of test data. Hardcoded test data, for example, names like &#8220;Santosh Adhikari&#8221; and reused emails like test@example.com, can lead to duplicate entry errors. To solve such issues, Ruby developers can pair Selenium with two powerful libraries: <a href="https://rubygems.org/gems/faker/versions/3.4.2?locale=en" data-type="link" data-id="https://rubygems.org/gems/faker/versions/3.4.2?locale=en" target="_blank" rel="noopener">Faker</a> and FactoryBot. While Faker helps developers to generate random but realistic data, FactoryBot offers a clean and well-structured way to manage it.</p>



<p>Interesting Right? Now let&#8217;s install the required gems in our system.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
gem &#039;selenium-webdriver&#039;
gem &#039;rspec&#039;
gem &#039;faker&#039;
gem &#039;factory_bot&#039;
</pre></div>


<p>Now to update your gem files run following command:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
bundle install
</pre></div>


<p>Now you&#8217;re all set to combine the power of dynamic test data for your test autmations.</p>



<h2 class="wp-block-heading">Generating Realistic Test Data With Faker</h2>



<p>The Faker gem generates data that mimics real-world information, such as Name, Email, Address, Phone Number, and credit card details.</p>



<p>Now, let&#8217;s examine a basic example of generating realistic test data using Faker.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
require &#039;faker&#039;

puts Faker::Name.name           # =&gt; &quot;Ava Brooks&quot;
puts Faker::Internet.email      # =&gt; &quot;ava.brooks@example.com&quot;
puts Faker::Address.city        # =&gt; &quot;New York&quot;
</pre></div>


<p>You can use different approaches to manage data, such as creating Enums, Models, et with Faker Data</p>



<p>Let&#8217;s go through following example:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
module UserRole
  ADMIN = &#039;admin&#039;
  USER = &#039;user&#039;
  GUEST = &#039;guest&#039;
  
  ALL = &#x5B;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
</pre></div>


<h2 class="wp-block-heading">Organizing Test Data with FactoryBot</h2>



<p>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.</p>



<h3 class="wp-block-heading">Defining a Factory</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
# factories/user_factory.rb
require &#039;factory_bot&#039;
require &#039;faker&#039;

FactoryBot.define do
  factory :user do
    name { Faker::Name.name }
    email { Faker::Internet.email }
    password { &#039;SecurePass123!&#039; }
    city { Faker::Address.city }
  end
end
</pre></div>


<p>To use the factory in your test:</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
include FactoryBot::Syntax::Methods

user = build(:user)
</pre></div>


<p>Now you can use the realistic values each time you run the test. user.name, user.email, etc, contain fresh data for your tests.</p>



<p>Let&#8217;s go through a real-world scenario</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
require &#039;selenium-webdriver&#039;
require &#039;factory_bot&#039;
require &#039;faker&#039;

# --- 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(&quot;^0-9&quot;)&#x5B;0..9] } # 10-digit number
    address { Faker::Address.full_address }
  end
end

# --- Test Suite ---
include FactoryBot::Syntax::Methods

describe &#039;DemoQA practice form&#039; do
  before(:all) do
    FactoryBot.find_definitions
    @driver = Selenium::WebDriver.for :chrome
    @driver.manage.timeouts.implicit_wait = 10
    @driver.navigate.to &#039;https://demoqa.com/automation-practice-form&#039;
    @driver.manage.window.maximize
  end

  it &#039;fills out form with realistic fake data&#039; do
    user = build(:user)

    @driver.find_element(id: &#039;firstName&#039;).send_keys(user.first_name)
    @driver.find_element(id: &#039;lastName&#039;).send_keys(user.last_name)
    @driver.find_element(id: &#039;userEmail&#039;).send_keys(user.email)

    # Gender radio button
    @driver.find_element(xpath: &quot;//label&#x5B;text()=&#039;Male&#039;]&quot;).click

    @driver.find_element(id: &#039;userNumber&#039;).send_keys(user.phone)

    # Date of Birth 
    @driver.find_element(id: &#039;dateOfBirthInput&#039;).click
    @driver.find_element(css: &#039;.react-datepicker__day--015&#039;).click rescue nil

    # Subjects - type and select
    subject_input = @driver.find_element(id: &#039;subjectsInput&#039;)
    subject_input.send_keys(&#039;Math&#039;)
    subject_input.send_keys(:enter)

    # Hobbies - click checkbox
    @driver.find_element(xpath: &quot;//label&#x5B;text()=&#039;Sports&#039;]&quot;).click

    # Address
    @driver.find_element(id: &#039;currentAddress&#039;).send_keys(user.address)

    @driver.execute_script(&#039;window.scrollBy(0, 300)&#039;)

    @driver.find_element(id: &#039;submit&#039;).click

    sleep 2 # View confirmation modal
  end

  after(:all) do
    @driver.quit
  end
end
</pre></div>


<p>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.</p>



<p>If you are still hardcoding test data in 2025, then it&#8217;s time to level up. Cheers.</p>



<p>Happy Testing</p>



<p>Happy Coding</p>
]]></content:encoded>
					
					<wfw:commentRss>https://testautomationlabs.com/use-faker-and-factorybot-in-selenium-ruby/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
