Skip to content

Instantly share code, notes, and snippets.

@johana-star
Created April 2, 2012 06:28
Show Gist options
  • Save johana-star/2281234 to your computer and use it in GitHub Desktop.
Save johana-star/2281234 to your computer and use it in GitHub Desktop.
How to create a basic language selector with jQuery that's testable in Cucumber
<!-- in lib/views/index.erb -->
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Strand Beer</title>
</head>
<body>
<a href="" id="en">EN</a> | <a href="" id="zh">ZH</a>
<p class='en' lang='en'>This is English content.</p>
<p class='zh' lang='zh'>这是中国的内容。</p>
<script>
$("a#en").click(function(event) {
event.preventDefault();
$('.zh').hide();
$('.en').show();
});
$("a#zh").click(function(event) {
event.preventDefault();
$('.en').hide();
$('.zh').show();
});
</script>
</body>
</html>
# in features/language_selector.feature
Feature: language selector
As a customer
I want to be able to view the site in Chinese or English
So I can read about beer in the language which I am most fluent.
@javascript
Scenario: Selecting English language content
Given I am on the homepage
When I follow "EN"
Then I should be on the homepage
And I should see "This is English content."
And I should see "这是中国的内容。" within ".hidden"
And I should see "EN | ZH"
@javascript
Scenario: Selecting Chinese language content
Given I am on the homepage
When I follow "ZH"
Then I should be on the homepage
And I should see "这是中国的内容。"
And I should see "This is English content." within ".hidden"
And I should see "EN | ZH"

I am working on a bilingual (Chinese/English) site in Sinatra and using Cucumber to test features as they are implemented.

A key feature for the site is that content should be displayed in English or Chinese when a selector is used. Initially I used a step beginning with "I should see..." however, when I hide content cucumber can still see it, because the content is still in the source, it is just not displayed in the browser. How do I write a step which confirms that content is visible or not visible?

Solved! I didn't have to write a new web step, though I did change the jQuery from $('.zh').show(); $('.en').hide(); (and its inverse) to $('.zh').show().removeClass("hidden"); $('.en').hide().addClass("hidden"); and used the changing class to detect that the content was hidden.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment