水豚测试上一个屏幕

问题描述 投票:0回答:1

在 ruby on Rails 应用程序中,我有一个“主屏幕”haml,其中带有一个添加按钮,可将用户带到“添加屏幕”按钮。我使用 Caybara 和 Selenium 创建了一个测试,检查 h1 是否已更改。在浏览器中,运行测试时,它将进入带有新标题的新屏幕。然而,测试正在从旧屏幕上获取值。任何解决问题的方法和解释将不胜感激。

Given(/^I selected the website$/) do
  visit 'http://localhost:3000/'
end

When(/^I (?:click|have clicked) the add button$/) do
  click_link_or_button('Add')
end

Then(/^I will see the add member screen$/) do
  expect(find('body > h1')).to have_text('Add member')
end

主屏幕.haml:

%html{ lang: "en" }
  %head
    :css
      body {
        font-family: Arial, sans-serif;
      }
      table {
        width: 100%;
        border-collapse: collapse;
      }
      th, td {
        border: 1px solid #ddd;
        text-align: center;
        padding: 8px;
      }
      th {
        background-color: #f2f2f2;
      }
      .button-link {
        display: inline-block;
        padding: 5px 20px;
        background-color: #f2f2f2;
        color: black;
        border: 2px solid transparent;
        border-radius: 5px;
        text-decoration: none;
        cursor: pointer;
      }
      .button-link:hover {
        background-color: #0056b3;
      }
      .button-link:focus {
        outline: none; /* Remove focus outline */
      }
      .button-link::before,
      .button-link::after {
        content: '';
        display: block;
        position: relative;
        top: -2px;
        bottom: -2px;
        width: calc(100% + 4px);
        border: 2px solid black;
        border-radius: 5px;
        z-index: -1;
      }
      .button-link::before {
        left: -2px;
        right: -2px;
        width: calc(100% + 4px);
      }
      .button-link span {
        position: relative;
        z-index: 1;
      }

  %body
    %h1 Home
    = form_with(mode: @member, url: add_member_screen_path) do |form|
      %div
        %table#membersTable
          %tr
            %th First name
            %th Second name
          %tr
            %td= @member.first_name
            %td= @member.last_name
      %br
      %div
        = link_to addscreen_path, class: 'button-link', target: '_blank', method: :post do
          %span Add

addscreen.haml:

%html{ lang: "en" }
  %head
    :css
      body {
        font-family: Arial, sans-serif;
      }
      table {
        width: 100%;
        border-collapse: collapse;
      }
      th, td {
        border: 1px solid #ddd;
        text-align: center;
        padding: 8px;
      }
      th {
        background-color: #f2f2f2;
      }
      .button-link {
        display: inline-block;
        padding: 5px 20px;
        background-color: #f2f2f2;
        color: black;
        border: 2px solid transparent;
        border-radius: 5px;
        text-decoration: none;
        cursor: pointer;
      }
      .button-link:hover {
        background-color: #0056b3;
      }
      .button-link:focus {
        outline: none; /* Remove focus outline */
      }
      .button-link::before,
      .button-link::after {
        content: '';
        display: block;
        position: relative;
        top: -2px;
        bottom: -2px;
        width: calc(100% + 4px);
        border: 2px solid black;
        border-radius: 5px;
        z-index: -1;
      }
      .button-link::before {
        left: -2px;
        right: -2px;
        width: calc(100% + 4px);
      }
      .button-link span {
        position: relative;
        z-index: 1;
      }

  %body
    %h1 Add member
    = form_with(mode: @member, url: add_member_screen_path) do |form|
      %div
        %table#membersTable
          %tr
            %th First name
            %th Second name
          %tr
            %td= @member.first_name
            %td= @member.last_name
      %br
      %div
        = link_to home_path, class: 'button-link', target: '_blank', method: :post do
          %span Cancel

错误:

RSpec::Expectations::ExpectationNotMetError: expected to find text "Add member" in "Home"
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-support-3.13.0/lib/rspec/support.rb:110:in `block in <module:Support>'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-support-3.13.0/lib/rspec/support.rb:119:in `notify_failure'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/fail_with.rb:35:in `fail_with'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/handler.rb:40:in `handle_failure'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/handler.rb:56:in `block in handle_matcher'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/handler.rb:27:in `with_matcher'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/handler.rb:48:in `handle_matcher'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/expectation_target.rb:65:in `to'
/Users/hywelgriffiths/.rvm/gems/ruby-3.2.2/gems/rspec-expectations-3.13.0/lib/rspec/expectations/expectation_target.rb:101:in `to'
/Users/hywelgriffiths/Documents/Intellij/castell-nedd/function-tests/function-tests/features/step_definitions/add_screen/add_screen_steps.rb:10:in `/^I will see the add member screen$/'
/Users/hywelgriffiths/Documents/Intellij/castell-nedd/function-tests/function-tests/features/add_screen.feature:11:in `I will see the add member screen'
ruby-on-rails selenium-webdriver cucumber capybara haml
1个回答
0
投票

感谢@engineersmnky。我只是错过了阻止新标签形成的机会。简单回答:

= link_to addscreen_path, class: 'button-link', target: :_self, method: :post do
  %span Add
© www.soinside.com 2019 - 2024. All rights reserved.