3
私はrspecでcapybaraを使って自分のレールアプリをテストしようとしています。CapybaraとJavascriptでテストリダイレクト
私は、リンクをクリックして、それはjavascriptの
window.location="/sports"
を経由して私をリダイレクトし、私はコンテンツ「スポーツ」を有するとカピバラの上に、このリンクをクリックしてページを探し、それ:私はこの1つの問題を単純化してきました常に失敗します。
テストサーバーでファイルを開き、[支払い方法を送信]を押すと、自分のスポーツページに移動します。セレンテストが実行されているのを見ると、リダイレクトが実行されていないことがわかります。ここで
は、ファイルは次のとおりです。
swipes_spec.rb:
は 'spec_helper'
describe "Swipes" do
describe "can view index page" do
it "has checkout in h2" do
visit swipe_index_path
page.should have_selector("h2", text: "Checkout")
end
it "should have link test js" do
visit swipe_index_path
page.should have_link("test js")
end
it "responds to js", js: true do
visit swipe_index_path
click_link "test js"
page.should have_selector('a', text: "js works")
end
it "responds to button press", js: true do
visit swipe_index_path
click_button "Submit Payment"
wait_until(10) do
page.has_content?("Select a Sport")
end
page.should have_selector('h2', text: "Select")
end
end
end
スワイプ/ index.html.erbを必要とする:
<h2>Checkout</h2>
<span class="payment-errors"></span>
<form action="" method="POST" id="payment-form">
<div class="form-row">
<label>Card Number</label>
<input type="text" size="20" autocomplete="off" id ="card-number" class="card-number"/>
</div>
<div class="form-row">
<label>CVC</label>
<input type="text" size="4" autocomplete="off" class="card-cvc"/>
</div>
<div class="form-row">
<label>Expiration (MM/YYYY)</label>
<input type="text" size="2" class="card-expiry-month"/>
<span>/</span>
<input type="text" size="4" class="card-expiry-year"/>
</div>
<button type="submit" class="submit-button">Submit Payment</button>
</form>
<%= link_to_function "test js", '$(this).html("js works")' %>
<script type="text/javascript" src="https://js.stripe.com/v1/"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_xIm00GVAKVLMWmfeR2J8GlmeHcyhL');
$(document).ready(function() {
$("#payment-form").submit(function(event) {
// disable the submit button to prevent repeated clicks
$('.submit-button').attr("disabled", "disabled");
window.location = "/sports";
return false;
});
});
function stripeResponseHandler(status, response) {
if (response.error) {
$('.submit-button').removeAttr("disabled");
//show the errors on the form
$(".payment-errors").html(response.error.message);
} else {
var form$ = $("#payment-form");
// token contains id, last4, and card type
var token = response['id'];
// insert the token into the form so it gets submitted to the server
form$.append("<input type='hidden' name='stripeToken' value='" + token + "'/>");
// and submit
form$.get(0).submit();
}
}
</script>
Capybaraのjavascriptドライバを有効にしたことを覚えましたか? https://github.com/jnicklas/capybara – MrDanA
'js:true'はそれを処理しますか?また、 'respond to js'テストではJavaScriptを使ってリンクのテキストを変更していますので、javascriptが実行されていると想定していました(少なくともいくつかのケースでは)。 –