0
votes

Issue:

I am not able to login a web application using Capybara, rspec and selenium webdriver. I am able to fill the username and password to the respective fields, but when i tried to click on login button, application is not logging in. Instead the application returns 'Unprocessable entity' (tried enabling and disabling the cookies). Using Headless chromium 64 Linux.

lib/abcd.rb

def click_login user, password
  visit "https://www.******.com/users/sign_in"
  fill_in 'user[email]', :with => user
  fill_in 'user[password]', :with => password
  click_button 'Login'
end

Test case:

require_relative 'lib/*****.rb'

describe 'Visit Websites', type: :feature, driver: :selenium_chrome_headless do

    it "TC001_Test case 1" do
      click_login "[email protected]", "password123"
      expect(page).to have_title "Welcome to home page"
    end
       output:// application stays in the same page

    it "TC002_Test case 2" do
      find(:xpath,".account menu").click
      expect(page).to have_title "Account details page"
    end

driver setup - Approach 1

        def setup_driver
     Capybara.register_driver :selenium_chrome_headless do |app|

      Capybara::Selenium::Driver.new(app, browser: :chrome, options: driver_options, :driver_path => 'bin/chromedriver')

    end

  Capybara.configure do |config|

    config.run_server = false
    config.default_driver = :selenium_chrome_headless
   end
 end

 def driver_options

  options = Selenium::WebDriver::Chrome::Options.new(binary: 'bin/headless-chromium')

    arguments = %w[--headless --disable-gpu --window-size=1280x1696
                 --disable-application-cache --disable-infobars --no-sandbox
                 --hide-scrollbars --enable-logging --log-level=0
                 --single-process --ignore-certificate-errors --homedir=/tmp]
  arguments.each do |argument|

    options.add_argument(argument)
  end

  options

end

driver setup - Approach 2

        def setup_driver
     Capybara.register_driver :selenium_chrome_headless do |app|

      Capybara::Selenium::Driver.new(app, browser: :chrome, options: driver_options, :driver_path => 'bin/chromedriver')

    end

  Capybara.configure do |config|

    config.run_server = false
    config.default_driver = :selenium_chrome_headless
   end
 end

 def driver_options

  options = Selenium::WebDriver::Chrome::Options.new(binary: 'bin/headless-chromium')

    arguments = %w[--headless --disable-gpu --window-size=1280x1696
                 --disable-application-cache --disable-infobars --no-sandbox
                 --hide-scrollbars --enable-logging --log-level=0
                 --single-process --ignore-certificate-errors --homedir=/tmp]
  arguments.each do |argument|

    options.add_argument(argument)
  end

  options

end
1
Show the full error message with stack trace - Thomas Walpole

1 Answers

0
votes

You haven't explained exactly what you're trying to do here (test a local rails app, test a remote app, etc) but overall everything sounds like it is working exactly as expected in a default setup. By default every RSpec test is expected to be fully independent of every other test (you should be able to run any test singly and/or run them all in a random order), so everything gets reset between each test (it block). This means you would need to log in for every test (before blocks can help DRY that up). Also feature tests are different from normal unit tests, you should be testing whole user flows/behaviors rather than just single expectations - otherwise your tests will end up taking hours to run.

For the login issue it sounds like you haven't actually created the account in the system you're testing. If this is a local app you're testing, where you have direct connectivity to the DB you should look into fixtures or factories to help with setting up the correct state for a test. Check your logs to confirm but my guess is you'll see the login is actually failing due to invalid username/password combo.