require 'test_helper' class WistTest < CapybaraTestCase def setup super visit "file://#{Dir.pwd}/test/test0.html" end def verify_test_page(num) assert current_path.match(/test#{num}.html$/) end def wait_until_test(button) lambda do raise unless button.text == 'clicked' true end end # tests def test_click click('#click_test', visible: true) assert_equal('clicked', find('#click_test').text) end def test_wait_until_nested button = find('#wait_test') button.click wait_until do wait_until(&wait_until_test(button)) end end def test_wait_until button = find('#wait_test') button.click assert(button.text != 'clicked') wait_until(&wait_until_test(button)) # make sure default wait time is back to normal assert_equal(2, get_capybara_wait_time) end def test_verify_tweet_button click '#enable_tweet_button' verify_tweet_button 'check this out' end def test_switch_to_window_and_execute return if Capybara.javascript_driver == :poltergeist assert_num_windows = lambda do |num| assert_equal(num, page.driver.browser.window_handles.size) end verify_test_page(0) switch_to_window_and_execute(-> { click('#switch_window_test') }) do verify_test_page(1) assert_num_windows.(2) end assert_num_windows.(1) verify_test_page(0) end def test_get_js assert get_js('document.URL').match(/test0.html$/) end def test_get_val find('#input_test').set 'foo' assert_equal 'foo', get_val('#input_test') end def test_has_class el = find('#class_test', visible: false) %w(foo bar).each { |c| assert_equal(true, has_class?(el, c)) } assert_equal(false, has_class?(el, 'baz')) assert_equal(false, has_class?(find('#click_test'), 'foo')) end def test_alert_accept alert_accept 'alert' do click '#alert_test' end end def test_wait_for_new_url verify_test_page(0) wait_for_new_url(find('#wait_for_new_url_test')) verify_test_page(1) end def test_wait_for_new_url_with_string wait_for_new_url('#wait_for_new_url_test') verify_test_page(1) end def test_process_el_or_selector assert_equal(process_el_or_selector('body'), process_el_or_selector(find('body'))) end def test_wait_for_new_url_with_block wait_for_new_url { click('#wait_for_new_url_test') } verify_test_page(1) end def test_refresh find('#input_test').set 'foo' refresh assert_equal '', get_val('#input_test') end def test_wait_til_element_visible el = find('#test_wait_til_element_visible') el.click assert_equal false, el.visible? assert wait_til_element_visible('#test_wait_til_element_visible').visible? end def test_set_value_and_trigger_evts set_value_and_trigger_evts('#test_set_value_and_trigger_evts', 'foo') assert_equal('changed', get_val('#test_set_value_and_trigger_evts')) end def test_set_cookie return if Capybara.javascript_driver == :chrome set_cookie('foo', 'bar') assert_equal('bar', page.evaluate_script("$.cookie('foo')")) end def test_set_input_and_press_enter alert_accept('enter pressed') do set_input_and_press_enter('#test_set_input_and_press_enter', 'bar') end assert_equal('bar', get_val('#test_set_input_and_press_enter')) end def test_scroll_to old_offset = get_js('window.pageYOffset') scroll_to('#test_scroll_to') assert_equal(true, get_js('window.pageYOffset') != old_offset) end def test_finder_with_wait click('#finder_with_wait') assert_equal('finder_with_wait', first_with_wait('.done', visible: true)[:id]) refresh click('#finder_with_wait') assert_equal('finder_with_wait', all_with_wait('.done', visible: true)[0][:id]) end def test_visit_with_retries return unless Capybara.javascript_driver == :poltergeist assert_raise RuntimeError do visit_with_retries('a.bc', retries: 2, wait_time: 1) end visit_with_retries("file://#{Dir.pwd}/test/test0.html") verify_test_page(0) end def test_assert_text assert_text('foo', find('#click_test')) end def test_assert_text_include assert_text_include('fo', find('#click_test')) end def test_parent assert_text('parent', parent(find('#test_parent', visible: false))) end def test_click_by_text click_by_text('button', 'click by text') assert_text('clicked', find('#click_by_text')) end def test_assert_css assert_has_css('#click_test') assert_has_no_css('#dog123') end def test_has_css_instant? assert_equal(true, has_css_instant?('body', visible: true)) assert_equal(false, has_css_instant?('#sdklfjk')) end def test_do_instantly old_time = get_capybara_wait_time new_time = nil begin do_instantly do new_time = get_capybara_wait_time raise end rescue end assert_equal(0, new_time) assert_equal(old_time, get_capybara_wait_time) end def test_interact_with_confirm [true, false].each do |bool| interact_with_confirm(bool) click('#confirm_test') assert_equal(bool, get_js('confirmed')) end end def test_assert_el_has_link assert_el_has_link('#switch_window_test', 'test1.html') assert_raises(Minitest::Assertion) do assert_el_has_link('#switch_window_test', 'foo') end end end