Search code examples
ruby-on-railsselenium-webdrivercucumbercapybarahaml

Capybara testing previous screen


In a ruby on rails application I have a 'homescreen' haml with an add button that takes the user to a 'addscreen' button. I have used Caybara and Selenium to create a test which checks to see if the h1 has changed. In the browser, when the test is run it is going to the new screen with the new header. However, the test is taking values off the old screen. Any ways to fix the issue and an explanation would be gratefully received.

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

homescreen.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

Error:

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'

Solution

  • Thanks to @engineersmnky. I'd simply missed preventing a new tab from forming. Easy answer of:

    = link_to addscreen_path, class: 'button-link', target: :_self, method: :post do
      %span Add