Basic Context Factory

Jeff Nyman edited this page Oct 3, 2015 · 3 revisions

Here's an example of using a context factory. The reason for doing this is that it provides a nice reference as to where you are. This script is doing exactly what you did with the Basic Page Definition. What changes here is how the page definition is instantiated and referenced.

#!/usr/bin/env ruby

require 'symbiont'
include Symbiont::Factory

require 'rspec/expectations'
include RSpec::Matchers

class Stardate
  attach Symbiont

  url_is      'http://localhost:9292/stardate'
  url_matches /:\d{4}\/stardate/
  title_is    'Symbiote - Stardate Calculator'

  checkbox   :enable,        id: 'enableForm'
  radio      :tng_era,       id: 'tngEra'
  text_field :stardate,      id: 'stardateValue'
  text_field :calendar,      id: 'calendarValue'
  button     :convert,       id: 'convert'
  
  def convert_tng_date(value)
    enable.check
    tng_era.set
    stardate.set value
    convert.click
  end

  def date_should_contain(value)
    expect(calendar.value).to match value
  end

  def date_should_be(value)
    expect(calendar.value).to eq value
  end
end

Symbiont.set_browser

on_view(Stardate)
on(Stardate).convert_tng_date('56844.9')
on(Stardate).date_should_contain('2379')
on(Stardate).date_should_be('Mon, 05 Nov 2379 09:19:26 GMT')

Symbiont.browser.close

You can condense the factory as such:

on_view(Stardate) do |page|
  page.convert_tng_date('56844.9')
  page.date_should_contain('2379')
  page.date_should_be('Mon, 05 Nov 2379 09:19:26 GMT')
end

However, since the context factories automatically establish a @page instance variable, you can do this:

on_view(Stardate) do
  @page.convert_tng_date('56844.9')
  @page.date_should_contain('2379')
  @page.date_should_be('Mon, 05 Nov 2379 09:19:26 GMT')
end