27.02.2013 Views

Rails%203%20In%20Action

Rails%203%20In%20Action

Rails%203%20In%20Action

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

232 CHAPTER 9 File uploading<br />

You could keep adding file fields until all the users are satisfied, or you could be<br />

sensible about it and switch back to using one field and, directly underneath it, providing<br />

a link that, when clicked, adds another file field. Using this solution, you also<br />

clean up your UI a bit by removing possible extra file fields yet still allowing users to<br />

attach as many files as they like. This is where JavaScript comes in.<br />

When you introduce JavaScript into your application, you have to run any scenarios<br />

that rely on it through another piece of software called WebDriver. WebDriver is a<br />

browser driver, which was installed when the Capybara gem was installed, so you don’t<br />

have to do anything to set it up. Capybara without WebDriver won’t run JavaScript<br />

because it doesn’t support it by itself. By running these JavaScript-reliant scenarios<br />

through WebDriver, you ensure the JavaScript will be executed. One of the great<br />

things with this WebDriver and Capybara partnership is that you can use the same old,<br />

familiar Capybara steps to test JavaScript behavior.<br />

9.4.1 JavaScript testing<br />

Capybara provides an easy way to trigger WebDriver testing. You tag a scenario (or feature)<br />

with the @javascript tag, and it launches a new web browser window and tests<br />

your code by using the same steps as standard Capybara testing. Isn’t that neat? To tag<br />

a scenario, place @javascript above it. Let’s write a new scenario in the Creating<br />

Tickets feature for multifile uploading, as shown in the following listing.<br />

Listing 9.13 features/creating_tickets.feature<br />

@javascript<br />

Scenario: Creating a ticket with an attachment<br />

When I fill in "Title" with "Add documentation for blink tag"<br />

And I fill in "Description" with "The blank tag has an undocumented<br />

➥speed attribute"<br />

And I attach the file "spec/fixtures/speed.txt" to "File #1"<br />

And I follow "Add another file"<br />

And I attach the file "spec/fixtures/spin.txt" to "File #2"<br />

And I press "Create Ticket"<br />

Then I should see "Ticket has been created."<br />

And I should see "speed.txt" within "#ticket .assets"<br />

And I should see "spin.txt" within "#ticket .assets"<br />

When I follow "speed.txt"<br />

File<br />

field<br />

The @javascript tag at the top of this scenario tells Cucumber that the scenario uses<br />

JavaScript, so it should be run using WebDriver. Also in this scenario, you’ve filled in<br />

only one file field B because, as stated before, you’re going to reduce the number of<br />

initial file fields to one. After filling in this field, you follow the Add Another File link<br />

that triggers a JavaScript event, which renders the second file field that you can then<br />

fill in. The rest of this scenario remains the same: ensuring that the ticket is created C<br />

and that you can see the files inside the element with the class assets.<br />

When you run this scenario with bin/cucumber features/creating_tickets<br />

.feature:36, it fails because the Add Another File link doesn’t yet exist:<br />

C<br />

B<br />

Create<br />

ticket

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!