mailslurp-examples - ruby-capybara-cucumber-selenium

https://github.com/mailslurp/examples

Table of Contents

ruby-capybara-cucumber-selenium/cucumber.yml

default: --publish-quiet

ruby-capybara-cucumber-selenium/README.md

# Ruby Capybara Selenium Cucumber Test
Example use of MailSlurp emails with Capybara and Cucumber.

## Install

```
gem install bundler
bundle install
```

## Run
`API_KEY=your-mailslurp-key make test`

ruby-capybara-cucumber-selenium/Makefile

-include ../.env

bundler:
	gem install bundler

vendor:
	bundle install --path ./vendor/bundle

test: 
	API_KEY=$(API_KEY) bundle exec cucumber

ruby-capybara-cucumber-selenium/Gemfile.lock

GEM
  remote: https://rubygems.org/
  specs:
    activesupport (6.1.3.1)
      concurrent-ruby (~> 1.0, >= 1.0.2)
      i18n (>= 1.6, < 2)
      minitest (>= 5.1)
      tzinfo (~> 2.0)
      zeitwerk (~> 2.3)
    addressable (2.7.0)
      public_suffix (>= 2.0.2, < 5.0)
    builder (3.2.4)
    capybara (3.35.3)
      addressable
      mini_mime (>= 0.1.3)
      nokogiri (~> 1.8)
      rack (>= 1.6.0)
      rack-test (>= 0.6.3)
      regexp_parser (>= 1.5, < 3.0)
      xpath (~> 3.2)
    childprocess (3.0.0)
    concurrent-ruby (1.1.8)
    cucumber (5.3.0)
      builder (~> 3.2, >= 3.2.4)
      cucumber-core (~> 8.0, >= 8.0.1)
      cucumber-create-meta (~> 2.0, >= 2.0.2)
      cucumber-cucumber-expressions (~> 10.3, >= 10.3.0)
      cucumber-gherkin (~> 15.0, >= 15.0.2)
      cucumber-html-formatter (~> 9.0, >= 9.0.0)
      cucumber-messages (~> 13.1, >= 13.1.0)
      cucumber-wire (~> 4.0, >= 4.0.1)
      diff-lcs (~> 1.4, >= 1.4.4)
      multi_test (~> 0.1, >= 0.1.2)
      sys-uname (~> 1.2, >= 1.2.1)
    cucumber-core (8.0.1)
      cucumber-gherkin (~> 15.0, >= 15.0.2)
      cucumber-messages (~> 13.0, >= 13.0.1)
      cucumber-tag-expressions (~> 2.0, >= 2.0.4)
    cucumber-create-meta (2.0.4)
      cucumber-messages (~> 13.1, >= 13.1.0)
      sys-uname (~> 1.2, >= 1.2.1)
    cucumber-cucumber-expressions (10.3.0)
    cucumber-gherkin (15.0.2)
      cucumber-messages (~> 13.0, >= 13.0.1)
    cucumber-html-formatter (9.0.0)
      cucumber-messages (~> 13.0, >= 13.0.1)
    cucumber-messages (13.2.1)
      protobuf-cucumber (~> 3.10, >= 3.10.8)
    cucumber-tag-expressions (2.0.4)
    cucumber-wire (4.0.1)
      cucumber-core (~> 8.0, >= 8.0.1)
      cucumber-cucumber-expressions (~> 10.3, >= 10.3.0)
      cucumber-messages (~> 13.0, >= 13.0.1)
    diff-lcs (1.4.4)
    ethon (0.12.0)
      ffi (>= 1.3.0)
    ffi (1.15.0)
    i18n (1.8.10)
      concurrent-ruby (~> 1.0)
    mailslurp_client (11.5.20)
    middleware (0.1.0)
    mini_mime (1.1.0)
    mini_portile2 (2.5.0)
    minitest (5.14.4)
    multi_test (0.1.2)
    nokogiri (1.11.2)
      mini_portile2 (~> 2.5.0)
      racc (~> 1.4)
    nokogiri (1.11.2-x86_64-linux)
      racc (~> 1.4)
    protobuf-cucumber (3.10.8)
      activesupport (>= 3.2)
      middleware
      thor
      thread_safe
    public_suffix (4.0.6)
    racc (1.5.2)
    rack (2.2.3)
    rack-test (1.1.0)
      rack (>= 1.0, < 3)
    regexp_parser (2.1.1)
    rspec (3.10.0)
      rspec-core (~> 3.10.0)
      rspec-expectations (~> 3.10.0)
      rspec-mocks (~> 3.10.0)
    rspec-core (3.10.1)
      rspec-support (~> 3.10.0)
    rspec-expectations (3.10.1)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.10.0)
    rspec-mocks (3.10.2)
      diff-lcs (>= 1.2.0, < 2.0)
      rspec-support (~> 3.10.0)
    rspec-support (3.10.2)
    rubyzip (2.3.0)
    selenium-webdriver (3.142.7)
      childprocess (>= 0.5, < 4.0)
      rubyzip (>= 1.2.2)
    sys-uname (1.2.2)
      ffi (~> 1.1)
    thor (1.1.0)
    thread_safe (0.3.6)
    typhoeus (1.4.0)
      ethon (>= 0.9.0)
    tzinfo (2.0.4)
      concurrent-ruby (~> 1.0)
    xpath (3.2.0)
      nokogiri (~> 1.8)
    zeitwerk (2.4.2)

PLATFORMS
  ruby
  x86_64-linux

DEPENDENCIES
  capybara (~> 3.35)
  cucumber (~> 5.3)
  mailslurp_client (~> 11.5)
  rspec (~> 3.10)
  selenium-webdriver (~> 3.142)
  typhoeus (~> 1.4)

BUNDLED WITH
   2.2.3

ruby-capybara-cucumber-selenium/Gemfile

# frozen_string_literal: true

source "https://rubygems.org"

git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }

# gem "rails"

gem "mailslurp_client", "~> 11.5"

gem "capybara", "~> 3.35"

gem "selenium-webdriver", "~> 3.142"

gem "cucumber", "~> 5.3"

gem "typhoeus", "~> 1.4"

gem "rspec", "~> 3.10"

ruby-capybara-cucumber-selenium/features/sign_up.feature

@selenium
Feature: User authentication with verification

Testing an application with real email addresses

  Scenario: Sign up and confirm account
    Given a user has an email address and loads the app
    When user signs up with email address and password
    Then they receive a confirmation code and can confirm their account

ruby-capybara-cucumber-selenium/features/support/env.rb

require 'capybara' 
require 'capybara/dsl' 
require 'capybara/cucumber'
require 'selenium-webdriver'

Capybara.configure do |config|
  config.default_driver = :selenium
  config.app_host   = 'https://playground.mailslurp.com'
end

World(Capybara)

ruby-capybara-cucumber-selenium/features/step_definitions/sign_up_steps.rb

require 'mailslurp_client'

# configure the mailslurp client with an API Key
MailSlurpClient.configure do |config|
	api_key = ENV['API_KEY']
	if api_key == "" or api_key == nil then
		raise "No API_KEY environment variable set for MailSlurp API KEY"
	end
  config.api_key['x-api-key'] = api_key
end

inbox = nil

Given /a user has an email address and loads the app/ do
	# create a test email account for the user
  inbox_controller = MailSlurpClient::InboxControllerApi.new
  inbox = inbox_controller.create_inbox 
	
	# load the playground application
	visit '/'
  expect(page).to have_title 'React App'
end

When /user signs up with email address and password/ do
	# click the sign up link
	find('[data-test="sign-in-create-account-link"]').click

	# fill out the form
	within('[data-test="sign-up-body-section"]') do
		fill_in 'email', with: inbox.email_address
		fill_in 'password', with: 'test-password'
	end

	# click submit and wait for confirm page to load
	find('[data-test="sign-up-create-account-button"]').click
	find('[data-test="confirm-sign-up-body-section"]').visible?
end

Then /they receive a confirmation code and can confirm their account/ do
	# wait for first unread email to arrive in user's inbox
  wait_controller = MailSlurpClient::WaitForControllerApi.new
  email = wait_controller.wait_for_latest_email({ inbox_id: inbox.id, unread_only: true, timeout: 30_000 })

	# assert the email is a confirmation 
	expect(email.subject).to include("Please confirm your email address")

	# extract a 6 digit code from the email body
	match = email.body.match(/code is ([0-9]{6})/)
	if match == nil then
		raise "Could not find match in body #{email.body}" 
	end 
	code, * = match.captures

	expect(code).to be_truthy	

	# submit confirmation code
	within('[data-test="confirm-sign-up-body-section"]') do
		fill_in 'code', with: code
	end
	find('[data-test="confirm-sign-up-confirm-button"]').click

	# load the main page again
	visit '/'	

	# login and see a welcome
	fill_in 'username', with: inbox.email_address
	fill_in 'password', with: "test-password"
	find('[data-test="sign-in-sign-in-button"]').click

	# wait for welcome to load
	expect(find('h1', wait: 30).text).to include("Welcome")
end