Project

swamp

0.02
No commit activity in last 3 years
No release in over 3 years
Automatically generates the methods and selectors to help on faster page-object creation using capybara
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.6
~> 1.3
~> 2.6
~> 1.8
~> 0.9
~> 0.2
~> 10.3
~> 3

Runtime

 Project Readme

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.