From 3568ad0950ea6c0fe781c2a996a185fc662fa8f8 Mon Sep 17 00:00:00 2001 From: Anthony Li Date: Thu, 6 Nov 2014 20:18:01 +0800 Subject: [PATCH 1/2] Added selenium and made it worked but it is very buggy --- Gemfile | 2 + spec/rails_helper.rb | 8 ++- spec/requests/announcement_pages_copy_spec.rb | 62 +++++++++++++++++++ spec/requests/authentication_pages_spec.rb | 2 +- spec/support/database_cleaner.rb | 24 +++++++ 5 files changed, 95 insertions(+), 3 deletions(-) create mode 100644 spec/requests/announcement_pages_copy_spec.rb create mode 100644 spec/support/database_cleaner.rb diff --git a/Gemfile b/Gemfile index 5602c2c20..ed325bc93 100644 --- a/Gemfile +++ b/Gemfile @@ -27,6 +27,8 @@ group :development, :test do gem 'factory_girl_rails' gem 'capybara' gem 'launchy' + gem 'selenium-webdriver' + gem 'database_cleaner' end # Gems used only for assets and not required diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 1fdf557f2..24100b61a 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -23,7 +23,7 @@ # If you're not using ActiveRecord, or you'd prefer not to run each of your # examples within a transaction, remove the following line or assign false # instead of true. - config.use_transactional_fixtures = true + config.use_transactional_fixtures = false # If true, the base class of anonymous controllers will be inferred # automatically. This will be the default behavior in future versions of @@ -31,5 +31,9 @@ config.infer_base_class_for_anonymous_controllers = false config.include Capybara::DSL - Capybara.default_wait_time = 30 + Capybara.default_wait_time = 3 + + Capybara.register_driver :selenium_chrome do |app| + Capybara::Selenium::Driver.new(app, :browser => :chrome) + end end diff --git a/spec/requests/announcement_pages_copy_spec.rb b/spec/requests/announcement_pages_copy_spec.rb new file mode 100644 index 000000000..d46504414 --- /dev/null +++ b/spec/requests/announcement_pages_copy_spec.rb @@ -0,0 +1,62 @@ +require 'rails_helper' + +RSpec.describe "AnnouncementPages", :type => :request, :js => true do + + let(:admin) {FactoryGirl.create(:admin)} + let(:course) { FactoryGirl.create(:course) } + before do + Capybara.current_driver = :selenium_chrome + sign_in admin + create_course course + end + + describe "Announcement Creation" do + before do + visit course_announcements_path(course) + end + + # it "shows the new button" do + # expect(page).to have_link('New', href: new_course_announcement_path(course)) + # end + + describe "create action" do + before do + click_link 'New' + end + + # it "displays the content" do + # expect(page).to have_field("announcement_title") + # expect(page).to have_selector("iframe.wysihtml5-sandbox") + # end + + describe "with valid information" do + let(:announcement) {FactoryGirl.build(:announcement)} + before do + fill_in 'announcement_title', with: announcement.title + #fill_in 'announcement_description', visible: false, with: announcement.description + #find('#announcement_description').set(announcement.description) + #find(:xpath, "iframe"); + + page.execute_script("$('#announcement_description').attr('value','abcd');") + + # within_frame "iframe.wysihtml5-sandbox" do + # #fill_in 'body', with: announcement.description + # find(:xpath, "//body").text + # end + end + + it "should create an announcement" do + expect(page).to have_selector("textarea[value='abcd']", visible: false) + expect { click_button 'Create'}.to change(Announcement, :count).by(1) + + end + end + end + + end + + after(:all) do + Capybara.use_default_driver + end + +end \ No newline at end of file diff --git a/spec/requests/authentication_pages_spec.rb b/spec/requests/authentication_pages_spec.rb index 078adcccd..cb4cc21f7 100644 --- a/spec/requests/authentication_pages_spec.rb +++ b/spec/requests/authentication_pages_spec.rb @@ -46,4 +46,4 @@ end -end +end \ No newline at end of file diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 000000000..12ee8e98b --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,24 @@ +RSpec.configure do |config| + + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, :js => true) do + DatabaseCleaner.strategy = :truncation + end + + config.before(:each) do + DatabaseCleaner.start + load Rails.root + "db/seeds.rb" + end + + config.after(:each) do + DatabaseCleaner.clean + end + +end \ No newline at end of file From bb9798cf8e0fc327ed1cc83f0dfb80b66af2d0ba Mon Sep 17 00:00:00 2001 From: Anthony Li Date: Fri, 7 Nov 2014 16:07:22 +0800 Subject: [PATCH 2/2] Made selenium driver work with the current rspec tests --- Gemfile.lock | 9 ++ spec/rails_helper.rb | 9 +- spec/requests/announcement_pages_copy_spec.rb | 105 ++++++++---------- spec/support/database_cleaner.rb | 26 +++-- 4 files changed, 74 insertions(+), 75 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 520f09d62..f83112287 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -126,6 +126,7 @@ GEM crass (0.2.1) daemons (1.1.9) dalli (2.7.2) + database_cleaner (1.3.0) delayed_job (4.0.2) activesupport (>= 3.0, < 4.2) delayed_job_active_record (4.0.1) @@ -339,6 +340,11 @@ GEM tilt (~> 1.3) scrollToFixed_rails (1.0.5) railties (>= 3.1) + selenium-webdriver (2.35.1) + childprocess (>= 0.2.5) + multi_json (~> 1.0) + rubyzip (< 1.0.0) + websocket (~> 1.0.4) slop (3.6.0) slugged (2.0.0) activerecord (>= 3.0) @@ -377,6 +383,7 @@ GEM uuidtools (2.1.5) warden (1.2.3) rack (>= 1.0) + websocket (1.0.7) win32-process (0.7.4) ffi (>= 1.0.0) win32console (1.3.2-x86-mingw32) @@ -414,6 +421,7 @@ DEPENDENCIES connection_pool daemons dalli + database_cleaner delayed_job_active_record devise (= 3.0) exception_notification @@ -451,6 +459,7 @@ DEPENDENCIES sanitize sass-rails (~> 3.2.3) scrollToFixed_rails (~> 1.0.0) + selenium-webdriver simple_form! slugged (~> 2.0) spork-rails diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 24100b61a..ae5f97050 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -9,13 +9,6 @@ Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } RSpec.configure do |config| - # ## Mock Framework - # - # If you prefer to use mocha, flexmock or RR, uncomment the appropriate line: - # - # config.mock_with :mocha - # config.mock_with :flexmock - # config.mock_with :rr # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" @@ -31,7 +24,7 @@ config.infer_base_class_for_anonymous_controllers = false config.include Capybara::DSL - Capybara.default_wait_time = 3 + Capybara.default_wait_time = 30 Capybara.register_driver :selenium_chrome do |app| Capybara::Selenium::Driver.new(app, :browser => :chrome) diff --git a/spec/requests/announcement_pages_copy_spec.rb b/spec/requests/announcement_pages_copy_spec.rb index d46504414..cc0e10eef 100644 --- a/spec/requests/announcement_pages_copy_spec.rb +++ b/spec/requests/announcement_pages_copy_spec.rb @@ -1,62 +1,47 @@ require 'rails_helper' -RSpec.describe "AnnouncementPages", :type => :request, :js => true do - - let(:admin) {FactoryGirl.create(:admin)} - let(:course) { FactoryGirl.create(:course) } - before do - Capybara.current_driver = :selenium_chrome - sign_in admin - create_course course - end - - describe "Announcement Creation" do - before do - visit course_announcements_path(course) - end - - # it "shows the new button" do - # expect(page).to have_link('New', href: new_course_announcement_path(course)) - # end - - describe "create action" do - before do - click_link 'New' - end - - # it "displays the content" do - # expect(page).to have_field("announcement_title") - # expect(page).to have_selector("iframe.wysihtml5-sandbox") - # end - - describe "with valid information" do - let(:announcement) {FactoryGirl.build(:announcement)} - before do - fill_in 'announcement_title', with: announcement.title - #fill_in 'announcement_description', visible: false, with: announcement.description - #find('#announcement_description').set(announcement.description) - #find(:xpath, "iframe"); - - page.execute_script("$('#announcement_description').attr('value','abcd');") - - # within_frame "iframe.wysihtml5-sandbox" do - # #fill_in 'body', with: announcement.description - # find(:xpath, "//body").text - # end - end - - it "should create an announcement" do - expect(page).to have_selector("textarea[value='abcd']", visible: false) - expect { click_button 'Create'}.to change(Announcement, :count).by(1) - - end - end - end - - end - - after(:all) do - Capybara.use_default_driver - end - -end \ No newline at end of file +RSpec.describe "AnnouncementPages", :type => :request do + + let(:admin) {FactoryGirl.create(:admin)} + let(:course) { FactoryGirl.create(:course) } + before do + sign_in admin + create_course course + end + + describe "Announcement Creation", :js => true do + before do + visit course_announcements_path(course) + end + + describe "Test 1" do + it "shows the new button" do + expect(page).to have_link('New', href: new_course_announcement_path(course)) + end + end + + describe "create action" do + before do + click_link 'New' + end + + it "displays the content" do + expect(page).to have_field("announcement_title") + expect(page).to have_selector("iframe.wysihtml5-sandbox") + end + + describe "with valid information" do + let(:announcement) {FactoryGirl.build(:announcement)} + before do + fill_in 'announcement_title', with: announcement.title + page.execute_script("$('#announcement_description').attr('value','abcd');") + end + + it "should create an announcement", :js => true do + expect(page).to have_selector("textarea[value='abcd']", visible: false) + expect { click_button 'Create'}.to change(Announcement, :count).by(1) + end + end + end + end +end diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb index 12ee8e98b..b1ebc5d1a 100644 --- a/spec/support/database_cleaner.rb +++ b/spec/support/database_cleaner.rb @@ -1,24 +1,36 @@ RSpec.configure do |config| config.before(:suite) do - DatabaseCleaner.clean_with(:truncation) + DatabaseCleaner.clean_with(:truncation) + load Rails.root + "db/seeds.rb" + # puts "before suite" end config.before(:each) do - DatabaseCleaner.strategy = :transaction + DatabaseCleaner.strategy = :transaction + # puts "Before Normal" end config.before(:each, :js => true) do - DatabaseCleaner.strategy = :truncation + Capybara.current_driver = :selenium_chrome + DatabaseCleaner.strategy = :truncation + # puts "Before JS" end config.before(:each) do - DatabaseCleaner.start - load Rails.root + "db/seeds.rb" + DatabaseCleaner.start + # puts "Before all" + end + + config.after(:each, :js => true) do + load Rails.root + "db/seeds.rb" + Capybara.use_default_driver + # puts "After JS" end config.after(:each) do - DatabaseCleaner.clean + DatabaseCleaner.clean + # puts "After all" end -end \ No newline at end of file +end