Skip to content
/ swamp Public

automatically generates the methods for the most common page objects based on Capybara

Notifications You must be signed in to change notification settings

Juraci/swamp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Swamp

Build Status

Automatically generates the interfaces for the most common actions that a page can provide, so you can use the generated methods to quickly create your page objects using capybara.

How to install

This gem doesn't need to be added to your Gemfile, it's supposed to be installed globally and then you can use it from the command line.

It requires Ruby 2.3.0 or later and Firefox 24.0 or later (Works fine with Iceweasel too). To install, type:

gem install swamp

How to use

  • In the terminal type:
swamp
  • It will ask you to provide an URL:
Enter the url for the page to be scanned:
  • Provide a valid one, beginning with http:// or https:// for instance:
Enter the url for the page to be scanned:
https://accounts.google.com
  • Hit enter and wait for the code snippets, like this:
Enter the url for the page to be scanned:
https://accounts.google.com

Scanning, please wait...

def type_email(input)
  page.fill_in("Email", with: input)
end
def type_passwd(input)
  page.fill_in("Passwd", with: input)
end
def sign_in
  page.find(:css, "#signIn").click
end
def select_lang_chooser(option)
  page.select(option, :from => "lang-chooser")
end
def link_forgot_passwd
  page.click_link("link-forgot-passwd")
end
def link_signup
  page.click_link("link-signup")
end

(notice that the method names are a best guess and you are always free to change them)

  • Copy the code snippets and paste inside your capybara-page-object classes like this:
module PageObjects
  class SignIn
  
    def type_email(input)
      page.fill_in("Email", with: input)
    end
  
    def type_passwd(input)
      page.fill_in("Passwd", with: input)
    end
  
    def sign_in
      page.find(:css, "#signIn").click
    end
  
    def select_lang_chooser(option)
      page.select(option, :from => "lang-chooser")
    end
  
    def link_forgot_passwd
      page.click_link("link-forgot-passwd")
    end

    def link_signup
      page.click_link("link-signup")
    end
  end
end
  • Then just call the methods passing the expected parameters when necessary. For instance:
When /^I attempt to sign in with valid credentials/ do
  sign_in_page = PageObjects::SignIn.visit
  sign_in_page.type_email "username@email.com"
  sign_in_page.type_passwd "mypassword"
  sign_in_page.sign_in
end

Generating code snippets to use with SitePrism

You can easily change the scope from "page" to "prism" by using the following command:

:scope = prism

then the code snippets will be generated in the SitePrism fromat:

element :sign_up, 'button', text: 'Sign Up'

Generating code snippets to use with capybara-page-object

You can easily change the scope from "page" to "source" by using the following command:

:scope = source

then the code snippets will be generated with the "source" scope like this:

    def link_signup
      source.click_link("link-signup")
    end

Dynamically detecting elements

You can navigate in the browser that swamp opens. Lets say a page that you want to scan requires login, then just do the following:

  • Enter the url for this page on swamp and wait for the page to load
  • Do the login procedure in the oppened browser manually
  • Wait for the new page to load
  • Then just go to the terminal and hit ENTER
  • Swamp will detect the new elements (if any) and will generate the code snippets the same way as before

How it works?

It uses capybara to fireup the browser and visit the target URL then it looks for patterns like:

  • Buttons
  • Fields
  • Select boxes
  • Input buttons
  • Links

For each pattern found it verifies if the element is visible, has some key attributes like id, name, text and etc. There is some logic to decide on how to best create the method's name and the capybara's selector. Finally the code snippets are formatted and printed in the output.

Check the .feature files to learn about the overall behavior. Check the _spec.rb files to learn about the internal behavior. Feel free to contribute.