![](/img/trans.png)
[英]RSpec, capybara, bootbox.js, Rails-3.2. Strange behavior of :selenium driver
[英]Rails Rspec Capybara Selenium JS create not showing after pressing submit
我正在建立一個網上商店,該功能已經存在,即:在一個屏幕中,有一個產品列表和一個訂購商品列表。 當按產品訂購順序時,該產品將立即顯示在此列表中。
可以猜到,當想要通過硒進行測試時,我看到Firefox正在啟動,我想我什至看到按鈕被按下,但顯然沒有任何反應。 我的訂單列表中沒有項目。
將Rails 5與更新的水豚和硒webdriver一起使用。
gem 'rspec-rails', '~> 3.5', '>= 3.5.1'
gem 'capybara', '~> 2.10', '>= 2.10.1'
gem "factory_girl_rails", "~> 4.7"
gem 'selenium-webdriver', '~> 3.0'
gem 'database_cleaner', '~> 1.5', '>= 1.5.3'
gem "email_spec", "~> 1.6.0"
creation_order_spec.rb
require 'rails_helper'
RSpec.feature 'User can fill his shopping cart', js: true do
let!(:category) { FactoryGirl.create(:category, name: 'Honey') }
let!(:cheap_product) { FactoryGirl.create(:product, name: 'Honey lolly',
price: '0,80',
category: category) }
let!(:expensive_product) { FactoryGirl.create(:product, name: 'Honeyjar 400ml',
price: '3,95',
category: category) }
before do
visit categories_path
end
scenario 'with different products' do
page.find('.product', :text => 'Honey lolly').click_button('ADD TO CART')
page.find('.product', :text => 'Honeyjar 400ml').click_button('ADD TO CART')
within('#order') do
expect(page).to have_content 'Honey lolly'
expect(page).to have_content 'Honeyjar 400ml'
expect(page).to have_content 0.80 + 3.95
end
end
end
Database_cleaning.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.use_transactional_fixtures = false
config.before(:each, :js => true) do
DatabaseCleaner.strategy = :truncation
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
end
然后我運行它並得到:
Failure/Error: expect(page).to have_content 'Honey lolly'
expected to find text "Honey lolly" in "Total € 0,00"
這意味着我的訂單沒有任何反應,或者總金額超過了€0,00。
編輯
這是僅在某種程度上在測試中不起作用的JS。
$('#order').html("<%= j render 'orders/order' %>")
編輯2
測試日志
Started POST "/bookings" for 127.0.0.1 at 2016-11-14 10:11:00 +0100 Processing by BookingsController#create as JS Parameters: {"utf8"=>"✓", "booking"=>{"product_id"=>"1", "product_quantity"=>"5"}} [1m[36mProduct Load (0.5ms)[0m [1m[34mSELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT $2[0m [["id", 1], ["LIMIT", 1]] [1m[35m (0.4ms)[0m [1m[35mBEGIN[0m [1m[36mBooking Exists (0.7ms)[0m [1m[34mSELECT 1 AS one FROM "bookings" WHERE "bookings"."order_id" IS NULL AND "bookings"."product_id" = $1 LIMIT $2[0m [["product_id", 1], ["LIMIT", 1]] [1m[35m (0.1ms)[0m [1m[31mROLLBACK[0m Rendering bookings/create.js.erb Rendered orders/_order.html.erb (0.9ms) Rendered bookings/create.js.erb (2.8ms) Completed 200 OK in 14ms (Views: 4.8ms | ActiveRecord: 2.2ms)
似乎沒有為該訂單觸發SQL語句。 通常,應該填寫總數。
bookings_controller.rb
def create
@booking = @order.bookings.find_by(product_id: params[:booking][:product_id])
if @booking
@booking.product_quantity = params[:booking][:product_quantity]
@booking.save
else
@booking = @order.bookings.new(booking_params)
@product = @booking.product
@booking.product_name = @product.name
@booking.product_price = @product.price
end
@order.save
respond_to do |format|
format.html { redirect_to categories_path }
format.js { render layout: false }
end
end
order.save
觸發一個名為.sum_all_bookings
的額外方法。 這沒有顯示在SQL中。 此處的錯誤位於視圖(#order)中順序不變的錯誤處。
最可能的原因是您的JS資產之一發生錯誤,導致您預期的行為無法正常執行。 當資產未連接時,事情可以在開發模式下正常工作(因此,一個文件中的錯誤不會阻止其他文件的處理),但是當一個文件中的錯誤可以阻止資產時,則在測試和生產環境中會失敗解析/處理后並置的JS。
另外,您會發現2個(id為=產品('#product')的元素(我假設它們會有所不同,因為您希望添加2個產品)。 這是無效的html,因為id必須是唯一的,因此可能導致奇怪的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.