Install Selenium with Capybara in the Ruby On Rails application

Tram Ho

Original article:

Sometimes you want to know how your capybara test cases interact with your site, sometimes viewing log on console to find out why test cases are not working is not enough, that’s why you Want to make it show the steps in the browser in real time. You can do this really easily with Selenium, which provides us with the functionality needed to interact with the interface of all popular browsers.

Let’s start by looking at what we will use in this article, you can use your own versions but it is better if they are:

Ok, now we can start creating new projects:

We use the -T flag to not initialize the project and test files because we will create these files manually. After the command has finished running, we need to go into the project folder:

I always check if the server can run or not to make sure everything works correctly, if any error should be present on the console then I can tell.

If all goes well, we can continue.

To comply with TDD, we first need to create test cases with RSpec and Capybara, so let’s start by adding the required Gem in the development group and testing in Gemfile:

Note, the 3 dots above are meant to indicate the gems already available in your project. Now we can install the gems above (you can also use the bundle install command, the two commands are the same):

If everything has installed successfully, we need to install, and set up for the gem above, To install RSpec, we just need to run the following command:

Our small rodent friend, Capybara will need a bit more work. First, we need to put the following code in the first line of /spec/rspec_helper.rb file:

In the file /spec/rails_helper.rb :

We also need to change the use_transactional_fixtures parameter to false :

This is the code that shows what the rails_helper.rb file will look like after all the changes, and delete the comments:

In addition, we also need to install chromium chrome driver, If you have not already installed it. For Ubuntu, you just need to run the following command:

If you use another operating system, you can visit the chromedriver homepage and follow the instructions .

Okay, since we have done so many things that we haven’t even written a test case, we can finally write it. We need to create a folder called features inside the /spec directory, and inside it, we create the posts_spec.rb file including our test case.

/spec/features/posts_spec.rb :

The first line requires the rails_helper file and loads the code in it, then, as described in the RSpec documentation, we use RSpec.describe to encapsulate your code according to the test case, it is important that Indicates which specific driver use, in this case, is :selenium_chrome driver. In addition, we need to enable JavaScript with js: true because selenium sometimes has problems without it.

And our test case is complete!

However, it will fail, as expected.

Because we still haven’t implemented this feature in our project. But don’t worry, we will do it now. To be able to achieve our goals directly, I will ignore the usual constructs, for example, regarding posts will be a user. In our application, a post will be created anonymously, that is, it is not owned by any user. And to save time, I will use the scaffold to create a simple CRUD module, just use the following command:

Then run the migrate database:

For now, we can access the localhost:3000/posts in the browser, which lists all the posts created in my application. We still have no posts yet, but we can proceed to create one with the capybara test , just run the following command:

Your browser will automatically open and fill out the form.

Now we can celebrate because the test case has been a success!

One final note: make sure this works only with your ruby ​​on rails application, otherwise, not Capybara running on your site.  And that’s all for this article. I hope it will be helpful to you.

Share the news now

Source : Viblo