Justin Watts
Justin Watts

Reputation: 13

Can page-object be used outside of Cucumber?

I am trying to understand what exactly ties the page-object gem to Cucumber.

The page factory is exposed to Cucumbers world which allows the use of on / visit in steps, but this seems to be something that can be exposed without Cucumber.

The @browser variable which page-object relies on is created in a before_suite or before_scenario context in Cucumber but this can also be created without Cucumber.

Is there anything actually tying this gem to Cucumber?

Upvotes: 0

Views: 218

Answers (2)

Sid
Sid

Reputation: 408

As has been mentioned on rubygems.org PageObject gem has only development dependency on Cucumber. It doesnt need cucumber for implementation. A precise explanation of difference between runtime and development dependencies is given here.

For execution purposes Cucumber looks for step definitions. Passing Page Factory to World adds the PageObject capabilities to Cucumber. The only thing that I can think of tying PageObject to Cucumber are its unit tests. Evmorov has already given a great solution for it to work with Rspec alone.

Upvotes: 0

Evmorov
Evmorov

Reputation: 1219

Yes. Page-object gem can be used outside of Cucumber.

Here is an example how you can use it with RSpec.

$ tree

├── Gemfile
├── spec
│   ├── my_app
│   │   └── search_spec.rb
│   ├── pages
│   │   ├── article_page.rb
│   │   └── index_page.rb
│   └── spec_helper.rb

Gemfile

source "https://rubygems.org"

gem 'rspec'
gem 'page-object'

spec_helper.rb

require 'page-object'

Dir["#{File.dirname(__FILE__)}/pages/*_page.rb"].each { |file| require file }

RSpec.configure do |config|
  config.include PageObject::PageFactory

  config.before do
    @browser = Watir::Browser.new :firefox
  end

  config.after do
    @browser.close
  end
end

search_spec.rb

require 'spec_helper'

describe 'Search field' do
  it 'searches an article' do
    visit(IndexPage) do |page|
      page.search = 'Ruby'
      page.submit_search
    end

    expect(on(ArticlePage).main_header).to eq('Ruby')
  end
end

article_page.rb

class ArticlePage
  include PageObject

  h1(:main_header)
end

index_page.rb

class IndexPage
  include PageObject

  page_url 'https://www.wikipedia.org'

  text_field(:search, id: 'searchInput')
  button(:submit_search)
end

Upvotes: 2

Related Questions