点击使用Capybara / Poltergeist的活动

时间:2016-08-22 19:55:18

标签: javascript php ruby capybara poltergeist

我试图使用Capybara和Poltergeist发起点击事件。

我的目标是:

<div id="sticker-reserva">
    <button type="button" id="reservebtn" onclick="makeReservation()" class="btn btn-primary-outline btn-block m-b-2 disabled">Reservar</a>
</div>

我已实施的步骤:

When(/^user makes a reservation (.*)-(.*)$/) do |block,room|
    visit $baseUrl+'/calendar/day/02/01/2016'
    find(:xpath,'//div[@data="'+block+'/'+room+'"]').click

    expect(page).to have_xpath("//div[@data='"+block+"/"+room+"' and @class='col-xs-2 calendario-diario-celda  press']")
    page.execute_script("$('#reservebtn').click()")
    step "I fill the form data"
    page.execute_script('$("#saveReservation").click()')
    expect(page).to have_xpath("//div[contains(@class,'modal-body')]/p[2]", :text=>'Se ha guardado correctamente la reservación.')
    find(:xpath,"//div[contains(@class,'modal-footer')]/button[@class='btn btn-border btn-default']").click
end

当我使用Selenium而不是Poltergeist进行测试时,一切都可以正常运行。

错误产生于:page.execute_script("$('#reservebtn').click()")

我使用了page.expect断言来验证按钮没有被点击。

我尝试更改此选项中提到的行:

find(:xpath,'//button[@id="reservebtn"]').click
click_button 'reservebtn'
find(:xpath,'//div[@id="sticker-reserva"]').click
page.execute_script('makeReservation()');
page.execute_script("$('#reservebtn').click()")

更新

1)方案的执行会抛出此输出:

Scenario: Reservation                       # features/reservation.feature:13
    Given I am logged in as <USER_ROLE_SALES> # features/step_definitions/login_steps.rb:39
    When user makes a reservation 1-C         # features/step_definitions/reservation_steps.rb:33
      expected to find xpath "//div[@id=\"central-zone\"]/h1[@class=\"page-header\"]" with text "Nueva Reserva" but there were no matches. Also found "Calendario Diario Descargar Reporte", which matched the selector but not all filters. (RSpec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/reservation_steps.rb:46:in `block (2 levels) in <top (required)>'
      ./features/step_definitions/reservation_steps.rb:45:in `/^user makes a reservation (.*)-(.*)$/'
      features/reservation.feature:15:in `When user makes a reservation 1-C'
    Then the reservation in 1-C is logged     # features/step_definitions/reservation_steps.rb:66

&#34; Calendario Diario Descargar Reporte&#34;是预订按钮所在的原始页面的标题。

2)点击&#39; reservebtn&#39;应该更改页面并显示预订表格,页面标题是&#34; Nueva Reserva&#34;。但现在发生的是系统停留在具有按钮的视图中。

3)makeReservation()从当前视图中获取信息,并在php中调用显示预订表单的方法,并填写一些数据,如所选日期和为事件选择的房间。

makeReservation(){
   if(!$(this).hasClass('disabled')) {
        $('#reservebtn').prop('disabled',true);
        var reservationInfo = [];
        $(".press").each(function (index) {
            reservationInfo.push($(this).attr("data"));
            console.log(index + ": " + $(this).attr("data"));
        });

        console.log(reservationInfo);

        $.ajax({
            url: '{{ url('reservation') }}',
            data: {reservationInfo: reservationInfo, date: $('#dateNow').val()},
            type: "POST",
            beforeSend: function () {
                $('#modalLoading').modal('show');
            }
        }).done(function (a) {
            removeModal();
            $('.main').html(a);
        }).error(function (a) {
            removeModal();
            modalDefault('', 'Ha ocurrido un error. Vuelva a intentarlo', '', '', [0, 0, 1],[0], 'Entendido', 'error.svg');
        });
    }
}

4)我使用的版本是:PhantomJs 2.1.1.0和Poltergeist 1.10.0 5)这是我测试的配置文件:

require 'rspec/expectations'
require 'capybara'
require 'capybara/mechanize'
require 'capybara/poltergeist'
require 'capybara/cucumber'
require 'capybara/rspec'
require 'test/unit/assertions'
require 'mechanize'
require_relative 'browser_config'

World(Test::Unit::Assertions)

Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.register_driver :poltergeist do |app|
    options = {
        :js_errors => false,
        :inspector => true,
        :debug => false
    }
    Capybara::Poltergeist::Driver.new(app, options)
end
Capybara.ignore_hidden_elements = false

Capybara.app_host = "http://localhost"
Capybara.run_server = false
Capybara.app = "make sure this isn't nil"
Capybara.default_max_wait_time = 5

World(Capybara)

0 个答案:

没有答案