<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: tallerguy</title>
    <description>The latest articles on DEV Community by tallerguy (@tallerguy).</description>
    <link>https://dev.to/tallerguy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F179715%2Ff4b31e28-2cff-4fa3-8816-953572f115d9.png</url>
      <title>DEV Community: tallerguy</title>
      <link>https://dev.to/tallerguy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tallerguy"/>
    <language>en</language>
    <item>
      <title>Test Setup rails 5.2</title>
      <dc:creator>tallerguy</dc:creator>
      <pubDate>Sun, 25 Aug 2019 04:25:17 +0000</pubDate>
      <link>https://dev.to/tallerguy/test-setup-rails-5-2-i5m</link>
      <guid>https://dev.to/tallerguy/test-setup-rails-5-2-i5m</guid>
      <description>&lt;h1&gt;
  
  
  Capybara, Selenium and Devise
&lt;/h1&gt;

&lt;p&gt;So have been looking at these three for quite some time. Mostly in older versions of rails and as an after effect at the older version of these gems. &lt;/p&gt;

&lt;p&gt;Started working in a hobby project with rails 5.2 (why not rails 6?). This is what I had to do to make it all work.&lt;/p&gt;

&lt;p&gt;First things first, capybara and selenium come preinstalled.&lt;/p&gt;

&lt;p&gt;What else do you need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;capybara-screenshot: Find the details &lt;a href="https://github.com/mattheworiordan/capybara-screenshot"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;database_cleaner: Find the details &lt;a href="https://github.com/DatabaseCleaner/database_cleaner"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Capybara screenshot takes screenshots every time there is an error in your test and will help you when you scratch(pull out?) your head (hair?).&lt;/p&gt;

&lt;p&gt;database_cleaner is used to clean out test DB to start each test with a clean DB.&lt;/p&gt;

&lt;p&gt;Ok quickly moving on, where do you add the settings and what are the settings&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Assumption&lt;/em&gt;: the Testing framework is RSpec&lt;/p&gt;

&lt;h2&gt;
  
  
  Capybara
&lt;/h2&gt;

&lt;p&gt;In spec helper (rspec/spec_helper.rb) add the following at the very top&lt;/p&gt;

&lt;p&gt;&lt;code&gt;require 'capybara/rspec'&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Capybara Screenshot
&lt;/h2&gt;

&lt;p&gt;In spec helper (rspec/spec_helper.rb) add the following right below require capybara&lt;/p&gt;

&lt;p&gt;&lt;code&gt;require 'capybara-screenshot/rspec'&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Selenium
&lt;/h2&gt;

&lt;p&gt;In spec helper (rspec/spec_helper.rb) add the following below capybara-screeshot&lt;br&gt;
&lt;code&gt;require 'selenium-webdriver'&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Database cleaner
&lt;/h2&gt;

&lt;p&gt;Add a file inside rspec/support called database_cleaner.rb. Add the following inside the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RSpec.configure do |config|

  config.before(:suite) do
    DatabaseCleaner.clean_with(:truncation)
  end

  config.before(:each) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each, js: true) do
    DatabaseCleaner.strategy = :truncation
  end

  config.before(:each) do
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Pay special attention to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;config.before(:each) do
  DatabaseCleaner.strategy = :truncation
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This setting becomes important when we use selenium with devise to login by visiting the login page in a headless chrome(&lt;em&gt;More about this in another post&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;Ok, now the main setting to make selenium work with capybara, inside spec helper, after the require statements add the following.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Capybara.default_driver = :selenium

options = Selenium::WebDriver::Chrome::Options.new(
  args: %w[headless disable-gpu no-sandbox disable-dev-shm-usage window-size=1600,1200 verbose]
)

Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome, options: options)
end

Capybara.javascript_driver = :selenium
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;one by one now,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we set the default driver for running tests on a headless browser as selenium. There are others, like &lt;a href="https://github.com/teampoltergeist/poltergeist"&gt;poltergeist&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Options that we choose for the browser that will run our test suite, &lt;em&gt;headless&lt;/em&gt; part is important, you don't want the browser popping up for every test.&lt;/li&gt;
&lt;li&gt;Setting the javascript_driver to selenium to make capybara pay heed JS (the ajax and async goodness).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now that all the setting is done, when you actually write a test for logging in, how will it run?&lt;/p&gt;

&lt;p&gt;In order for all this to work, inside rspec/rails_helper.rb there is a setting&lt;/p&gt;

&lt;p&gt;&lt;code&gt;config.use_transactional_fixtures&lt;/code&gt; turn it off (= false). Selenium has no idea what happens in the transaction (details &lt;a href="https://stackoverflow.com/questions/12326096/capybara-selenium-fault-and-redirect-example-com-when-without-everything-is-gre/12330557#12330557"&gt;here&lt;/a&gt; and &lt;a href="https://stackoverflow.com/questions/6154687/rails-integration-test-with-selenium-as-webdriver-cant-sign-in"&gt;here&lt;/a&gt;) and because of this, all you get are errors for invalid logins.&lt;/p&gt;

&lt;p&gt;Remember the &lt;strong&gt;pay attention to this&lt;/strong&gt; part in database settings, ya well if you don't make it truncation all this won't work. &lt;/p&gt;

&lt;p&gt;Why is that? Haven't dug deep enough to write about it yet, but expect an article soon.&lt;/p&gt;

&lt;p&gt;Alright, that's it for now. Later.&lt;/p&gt;

&lt;p&gt;Keep coding.&lt;/p&gt;

</description>
      <category>devise</category>
      <category>testing</category>
      <category>capybara</category>
      <category>selenium</category>
    </item>
  </channel>
</rss>
