Lessons: 16Length: 3 hours

Next lesson playing in 5 seconds

  • Overview
  • Transcript

3.3 Testing With Capybara

Hi. In this excerpt, we'll see how to create end to end tests with Capybara. So, the most difficult thing in end to end testing is to prepare, interact, and clean database between tests. Royal's infrastructure makes it quite easy, with such tools as database cleaner and [UNKNOWN]. So, it makes sense to use it for your end to end tests, especially if you work on hybrid application, which combines standard web application and only parts of it as a single page applications. So, let's set it up. Let's open Gemfile, and at the bottom here I'm gonna create new group for development and test. And inside of it let's add our spec, Rails and Gem, Capybara. Also as we go in to test JavaScript application, we'll add selenium driver. Let's save it, go to terminal, and run bundle. Okay, I've got an error so, not a simple driver, but webdriver. Let's save it, and let's run bundle again. Okay, it is installed. So now let's initialize, rspec. So we do, Rails g rspec:install. That will create spec folder and spec helper. And now let's write our first feature. So let's go back to editor. And here in spec folder, let's create new one. We call it features. And the feature we're going to create is a navigation_spec.rb. So first we need to require spec helper. And then let's describe the behavior that we want to test. So we describe navigation menu, and this is feature with JavaScript, so we need to provide javaScript true. And now let's create example. It changes active menu element depending on route. Okay, so first of all, we visit Home page. And then, we expect to find active element, and inside of it, we want to find [UNKNOWN] and its text, and we expect it to equal home. Okay. Then, I want to click on link, edges, and then I expect to find that active changed, and I find anchor with text, and this text should be equal edges. Okay, so let's save it. I also need to create folder, and let's save it. And now if you try to run this spec. We will fail, but still, let's do this. So, with spec, feature, navigation, spec. Say it uses Firefox and it can't get anywhere. And it happens because it tries to go to the local host to the port 3,000 where Rails application is, but we want to test Angular application. So, we need to have a little bit of modifications. So, let's open spec helper. And inside of it, right here, we go in to set up Capybara app host. And we specify that we want it to be port 9000. And another thing that we should specify is run server for Capybara to false, because basically, we have two different applications, we have front end application and we have back end application. When we test them together, we just run both services, and it, Capybara should not run server by itself. So now, we save it and go back to terminal. Let's try it again. And now it works, like charm. Excellent. So this is how we can test application with the help of Capybara instead of protractor. So, what's to use is up to you. In this course, we'll use more Capybara approach. Okay. Let's make our changes. Rspec and capybara setup. Okay, so this way, we can use Capybara and Rspec to test the whole stack, and that also will give us ability to manage our database quite easily. So thank you very much for your time, and see you in the next episode.

Back to the top