swamp 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/.travis.yml +8 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +79 -0
- data/README.md +5 -0
- data/Rakefile +8 -0
- data/bin/swamp +20 -0
- data/features/specifications/user_scans_a_page_interactively.feature +10 -0
- data/features/specifications/user_scans_buttons_in_a_page.feature +43 -0
- data/features/specifications/user_scans_fields_in_a_page.feature +38 -0
- data/features/specifications/user_scans_input_buttons_in_a_page.feature +28 -0
- data/features/specifications/user_scans_links_in_a_page.feature +18 -0
- data/features/specifications/user_scans_select_box_elements_in_a_page.feature +28 -0
- data/features/specifications/user_starts_swamp.feature +10 -0
- data/features/step_definitions/swamp_steps.rb +46 -0
- data/features/support/env.rb +2 -0
- data/features/support/page_examples/button.html +1 -0
- data/features/support/page_examples/button.html~ +0 -0
- data/features/support/page_examples/button_without_text.html +1 -0
- data/features/support/page_examples/button_without_text_id_and_value.html +1 -0
- data/features/support/page_examples/button_without_text_without_id_with_value.html +1 -0
- data/features/support/page_examples/checkbox.html +1 -0
- data/features/support/page_examples/field.html +1 -0
- data/features/support/page_examples/field_without_id.html +1 -0
- data/features/support/page_examples/input_submit.html +1 -0
- data/features/support/page_examples/input_submit_without_id.html +1 -0
- data/features/support/page_examples/link.html +1 -0
- data/features/support/page_examples/link.html~ +0 -0
- data/features/support/page_examples/link_with_id.html +5 -0
- data/features/support/page_examples/radio.html +1 -0
- data/features/support/page_examples/select_box_with_id.html +1 -0
- data/features/support/page_examples/select_box_with_name_only.html +1 -0
- data/features/support/setup.rb +42 -0
- data/lib/swamp/base.rb +29 -0
- data/lib/swamp/builder.rb +35 -0
- data/lib/swamp/button.rb +11 -0
- data/lib/swamp/buttons.rb +19 -0
- data/lib/swamp/element.rb +23 -0
- data/lib/swamp/elements.rb +39 -0
- data/lib/swamp/evaluator.rb +20 -0
- data/lib/swamp/field.rb +11 -0
- data/lib/swamp/fields.rb +21 -0
- data/lib/swamp/formatter.rb +51 -0
- data/lib/swamp/input_button.rb +11 -0
- data/lib/swamp/input_buttons.rb +19 -0
- data/lib/swamp/interface.rb +33 -0
- data/lib/swamp/link.rb +11 -0
- data/lib/swamp/links.rb +15 -0
- data/lib/swamp/select_box.rb +11 -0
- data/lib/swamp/select_boxes.rb +17 -0
- data/lib/swamp/version.rb +3 -0
- data/lib/swamp/wrapper.rb +25 -0
- data/lib/swamp.rb +26 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/swamp/builder_spec.rb +36 -0
- data/spec/swamp/buttons_spec.rb +93 -0
- data/spec/swamp/evaluator_spec.rb +75 -0
- data/spec/swamp/fields_spec.rb +88 -0
- data/spec/swamp/formatter_spec.rb +59 -0
- data/spec/swamp/input_buttons_spec.rb +62 -0
- data/spec/swamp/interface_spec.rb +62 -0
- data/spec/swamp/links_spec.rb +34 -0
- data/spec/swamp/select_boxes_spec.rb +53 -0
- data/spec/swamp/wrapper_spec.rb +104 -0
- data/swamp.gemspec +31 -0
- metadata +274 -0
@@ -0,0 +1,20 @@
|
|
1
|
+
module Swamp
|
2
|
+
class Evaluator
|
3
|
+
def initialize(input, wrapper)
|
4
|
+
@input = input
|
5
|
+
@wrapper = wrapper
|
6
|
+
end
|
7
|
+
|
8
|
+
def valid_url?
|
9
|
+
@input.match(/^(http|https|file):\/\/.*$/i) ? true : false
|
10
|
+
end
|
11
|
+
|
12
|
+
def refresh_command?
|
13
|
+
enter_keystroke? and @wrapper.page_visited
|
14
|
+
end
|
15
|
+
|
16
|
+
def enter_keystroke?
|
17
|
+
@input == "\n"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/swamp/field.rb
ADDED
data/lib/swamp/fields.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Swamp
|
2
|
+
class Fields < Elements
|
3
|
+
def get
|
4
|
+
elements = []
|
5
|
+
page.all('input').map do |element|
|
6
|
+
if element.visible? and has_name?(element) and valid_type?(element)
|
7
|
+
if has_id?(element)
|
8
|
+
elements << Swamp::Field.new(element['name'], element['id'])
|
9
|
+
else
|
10
|
+
elements << Swamp::Field.new(element['name'], element['name'])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
elements
|
15
|
+
end
|
16
|
+
|
17
|
+
def valid_type?(element)
|
18
|
+
element['type'] != "radio" and element['type'] != "checkbox" and element['type'] != "submit" ? true : false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Swamp
|
2
|
+
class Formatter
|
3
|
+
|
4
|
+
def format(name)
|
5
|
+
@name = name
|
6
|
+
@name = replace_dashes
|
7
|
+
@name = remove_white_spaces
|
8
|
+
@name = replace_parentheses
|
9
|
+
@name = replace_brackets
|
10
|
+
@name = remove_suffix_symbols
|
11
|
+
@name = downcase_name
|
12
|
+
end
|
13
|
+
|
14
|
+
def format_class(name)
|
15
|
+
@name = name
|
16
|
+
@name = replace_white_spaces_with_dots
|
17
|
+
end
|
18
|
+
|
19
|
+
def replace_white_spaces_with_dots
|
20
|
+
@name.gsub(" ",".")
|
21
|
+
end
|
22
|
+
|
23
|
+
def replace_dashes
|
24
|
+
@name.gsub("-","_")
|
25
|
+
end
|
26
|
+
|
27
|
+
def remove_white_spaces
|
28
|
+
@name.gsub(" ", "_")
|
29
|
+
end
|
30
|
+
|
31
|
+
def replace_parentheses
|
32
|
+
@name.gsub("(", "_").gsub(")", "_")
|
33
|
+
end
|
34
|
+
|
35
|
+
def replace_brackets
|
36
|
+
@name.gsub("[", "_").gsub("]", "_")
|
37
|
+
end
|
38
|
+
|
39
|
+
def remove_suffix_symbols
|
40
|
+
result = @name.match(/\w+?(?<symbols>[_]+\Z)/)
|
41
|
+
if result != nil
|
42
|
+
@name = @name.chomp(result[:symbols])
|
43
|
+
end
|
44
|
+
@name
|
45
|
+
end
|
46
|
+
|
47
|
+
def downcase_name
|
48
|
+
@name.downcase
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Swamp
|
2
|
+
class InputButtons < Elements
|
3
|
+
def get
|
4
|
+
elements = []
|
5
|
+
page.all('input[type="submit"]').map do | element |
|
6
|
+
if element.visible?
|
7
|
+
if has_value?(element)
|
8
|
+
if has_id?(element)
|
9
|
+
elements << Swamp::InputButton.new(element["value"], "##{element["id"]}")
|
10
|
+
elsif has_class?(element)
|
11
|
+
elements << Swamp::InputButton.new(element["value"], "input.#{formatter.format_class(element["class"])}[value='#{element["value"]}']")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
elements
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Swamp
|
2
|
+
class Interface
|
3
|
+
|
4
|
+
WELCOME_MESSAGE = ['Enter the url for the page to be scanned:']
|
5
|
+
INVALID_REQUEST_MESSAGE = ['Please enter a valid url!']
|
6
|
+
|
7
|
+
def initialize(output, wrapper)
|
8
|
+
@output = output
|
9
|
+
@wrapper = wrapper
|
10
|
+
end
|
11
|
+
|
12
|
+
def run
|
13
|
+
present(WELCOME_MESSAGE)
|
14
|
+
end
|
15
|
+
|
16
|
+
def scan(input)
|
17
|
+
evaluator = Swamp::Evaluator.new(input, @wrapper)
|
18
|
+
messages = (evaluator.valid_url? or evaluator.refresh_command?) ? request(input) : INVALID_REQUEST_MESSAGE
|
19
|
+
present messages
|
20
|
+
end
|
21
|
+
|
22
|
+
def request(input)
|
23
|
+
@wrapper.explore(input)
|
24
|
+
@wrapper.scan
|
25
|
+
end
|
26
|
+
|
27
|
+
def present(messages)
|
28
|
+
messages.each do |message|
|
29
|
+
@output.puts(message)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/swamp/link.rb
ADDED
data/lib/swamp/links.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
module Swamp
|
2
|
+
class Links < Elements
|
3
|
+
def get
|
4
|
+
elements = []
|
5
|
+
page.all('a[href]').map do | element |
|
6
|
+
if element.visible?
|
7
|
+
if has_id?(element)
|
8
|
+
elements << Swamp::Link.new(element["id"], element["id"])
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
elements
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Swamp
|
2
|
+
class SelectBoxes < Elements
|
3
|
+
def get
|
4
|
+
elements = []
|
5
|
+
page.all('select').map do | element |
|
6
|
+
if element.visible?
|
7
|
+
if has_id?(element)
|
8
|
+
elements << Swamp::SelectBox.new(element["id"], element["id"])
|
9
|
+
elsif has_name?(element)
|
10
|
+
elements << Swamp::SelectBox.new(element["name"], element["name"])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
elements
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Swamp
|
2
|
+
class Wrapper < Base
|
3
|
+
def initialize(meta_collection)
|
4
|
+
@meta_collection = meta_collection
|
5
|
+
@page_visited = false
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :page_visited
|
9
|
+
|
10
|
+
def explore(url)
|
11
|
+
if !page_visited
|
12
|
+
visit url
|
13
|
+
@page_visited = true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def scan
|
18
|
+
found_snippets = []
|
19
|
+
@meta_collection.each do | element_collection |
|
20
|
+
found_snippets += element_collection.get.map { | element | Swamp::Builder.new(element).build_snippet }
|
21
|
+
end
|
22
|
+
found_snippets
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/swamp.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
Bundler.setup(:default)
|
4
|
+
|
5
|
+
require 'swamp/version'
|
6
|
+
require 'swamp/interface'
|
7
|
+
require 'swamp/evaluator'
|
8
|
+
require 'swamp/base'
|
9
|
+
require 'swamp/elements'
|
10
|
+
require 'swamp/wrapper'
|
11
|
+
require 'swamp/element'
|
12
|
+
require 'swamp/fields'
|
13
|
+
require 'swamp/field'
|
14
|
+
require 'swamp/buttons'
|
15
|
+
require 'swamp/button'
|
16
|
+
require 'swamp/builder'
|
17
|
+
require 'swamp/formatter'
|
18
|
+
require 'swamp/input_buttons'
|
19
|
+
require 'swamp/input_button'
|
20
|
+
require 'swamp/select_boxes'
|
21
|
+
require 'swamp/select_box'
|
22
|
+
require 'swamp/links'
|
23
|
+
require 'swamp/link'
|
24
|
+
|
25
|
+
module Swamp
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'swamp'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
# Use color in STDOUT
|
5
|
+
config.color_enabled = true
|
6
|
+
|
7
|
+
# Use color not only in STDOUT but also in pagers and files
|
8
|
+
#config.tty = true
|
9
|
+
|
10
|
+
# Use the specified formatter
|
11
|
+
#config.formatter = :documentation # :progress, :html, :textmate
|
12
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Swamp
|
3
|
+
describe Builder do
|
4
|
+
describe "#build_method" do
|
5
|
+
context "when the type is field" do
|
6
|
+
let(:type) { :field }
|
7
|
+
context "when the name and the selector are present" do
|
8
|
+
it "returns the method's snippet" do
|
9
|
+
element = Swamp::Field.new("User-Name", "User-Name")
|
10
|
+
builder = Swamp::Builder.new(element)
|
11
|
+
builder.build_snippet.should == "def type_user_name(input)\n source.fill_in(\"User-Name\", with: input)\nend"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when the type is button" do
|
17
|
+
let(:type) { :button }
|
18
|
+
context "when the name and the selector are present" do
|
19
|
+
it "returns the method's snippet" do
|
20
|
+
element = Swamp::Button.new("Log_in", "Log_in")
|
21
|
+
builder = Swamp::Builder.new(element)
|
22
|
+
builder.build_snippet.should == "def log_in\n source.click_button(\"Log_in\")\nend"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when just the selector is present" do
|
27
|
+
it "returns just the selector snippet" do
|
28
|
+
element = Swamp::Button.new(nil, "$ 9.90 Buy")
|
29
|
+
builder = Swamp::Builder.new(element)
|
30
|
+
builder.build_snippet.should == "source.click_button(\"$ 9.90 Buy\")"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Swamp
|
3
|
+
describe Buttons do
|
4
|
+
let(:buttons) { buttons = Swamp::Buttons.new }
|
5
|
+
|
6
|
+
it "delegates to capybara the responsibility to get the buttons" do
|
7
|
+
element = {'id' => "u_0_2"}
|
8
|
+
element.stub(:text).and_return("Sign Up")
|
9
|
+
element.stub(:visible?).and_return(true)
|
10
|
+
buttons.page.should_receive(:all).with('button').and_return([element])
|
11
|
+
buttons.get
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when the element is visible" do
|
15
|
+
context "when the button element has text" do
|
16
|
+
let(:element) { {'id' => "u_0_2"} }
|
17
|
+
|
18
|
+
before(:each) do
|
19
|
+
element.stub(:visible?).and_return(true)
|
20
|
+
element.stub(:text).and_return("Sign Up")
|
21
|
+
buttons.page.stub(:all).with('button').and_return([element])
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns the element in the array using the text as both the name and the selector" do
|
25
|
+
buttons.get.should have(1).button
|
26
|
+
buttons.get.each.first.name.should == "Sign Up"
|
27
|
+
buttons.get.each.first.selector.should == "Sign Up"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "when the button element has no text" do
|
32
|
+
context "when the button element has id" do
|
33
|
+
let(:element) { {'id' => "search-button"} }
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
element.stub(:visible?).and_return(true)
|
37
|
+
element.stub(:text).and_return("")
|
38
|
+
buttons.page.stub(:all).with('button').and_return([element])
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns the element in the array using the id as both the name and the selector" do
|
42
|
+
buttons.get.should have(1).button
|
43
|
+
buttons.get.each.first.name.should == "search-button"
|
44
|
+
buttons.get.each.first.selector.should == "search-button"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when the button element has no id" do
|
49
|
+
context "when the button element has value" do
|
50
|
+
let(:element) { {'value' => "search-button", 'id' => ""} }
|
51
|
+
|
52
|
+
before(:each) do
|
53
|
+
element.stub(:visible?).and_return(true)
|
54
|
+
element.stub(:text).and_return("")
|
55
|
+
buttons.page.stub(:all).with('button').and_return([element])
|
56
|
+
end
|
57
|
+
|
58
|
+
it "returns the element in the array using the value as both the name and the selector" do
|
59
|
+
buttons.get.should have(1).button
|
60
|
+
buttons.get.each.first.name.should == "search-button"
|
61
|
+
buttons.get.each.first.selector.should == "search-button"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when the button element has no value" do
|
66
|
+
let(:element) { {'value' => "", 'id' => ""} }
|
67
|
+
|
68
|
+
before(:each) do
|
69
|
+
element.stub(:visible?).and_return(true)
|
70
|
+
element.stub(:text).and_return("")
|
71
|
+
buttons.page.stub(:all).with('button').and_return([element])
|
72
|
+
end
|
73
|
+
|
74
|
+
it "returns an empty array" do
|
75
|
+
buttons.stub(:all).with('button').and_return([element])
|
76
|
+
buttons.get.should == []
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context "when the button element is not visible" do
|
84
|
+
it "returns an empty array" do
|
85
|
+
element = {'id' => "u_0_2"}
|
86
|
+
element.stub(:text).and_return("Sign Up")
|
87
|
+
element.stub(:visible?).and_return(false)
|
88
|
+
buttons.page.stub(:all).with('button').and_return([element])
|
89
|
+
buttons.get.should == []
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Swamp
|
3
|
+
describe Evaluator do
|
4
|
+
let(:wrapper) { wrapper = double('wrapper').as_null_object }
|
5
|
+
|
6
|
+
describe "#valid_url?" do
|
7
|
+
context "when the url is valid" do
|
8
|
+
context "when it starts with 'http://'" do
|
9
|
+
it "returns true" do
|
10
|
+
input = "http://fakepage.com"
|
11
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
12
|
+
evaluator.should be_valid_url
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when it starts with 'https://'" do
|
17
|
+
it "returns true" do
|
18
|
+
input = "https://fakepage.com"
|
19
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
20
|
+
evaluator.should be_valid_url
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when it starts with 'file://'" do
|
25
|
+
it "returns true" do
|
26
|
+
input = "file://fakepage.html"
|
27
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
28
|
+
evaluator.should be_valid_url
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context "when the url is invalid" do
|
34
|
+
context "when the protocol prefix is missing" do
|
35
|
+
it "returns false" do
|
36
|
+
input = "www.fakepage.com"
|
37
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
38
|
+
evaluator.should_not be_valid_url
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#refresh_command?" do
|
45
|
+
context "when the input is an enter keystroke" do
|
46
|
+
context "when a page was already visited" do
|
47
|
+
it "returns true" do
|
48
|
+
wrapper.stub(:page_visited).and_return(true)
|
49
|
+
input = "\n"
|
50
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
51
|
+
evaluator.should be_refresh_command
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "when no page was yet visited" do
|
56
|
+
it "returns false" do
|
57
|
+
wrapper.stub(:page_visited).and_return(false)
|
58
|
+
input = "\n"
|
59
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
60
|
+
evaluator.should_not be_refresh_command
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context "when the input is not an enter keystroke" do
|
66
|
+
it "returns false" do
|
67
|
+
wrapper.stub(:page_visited).and_return(true)
|
68
|
+
input = " "
|
69
|
+
evaluator = Swamp::Evaluator.new(input, wrapper)
|
70
|
+
evaluator.should_not be_refresh_command
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Swamp
|
3
|
+
describe Fields do
|
4
|
+
let(:fields) { Swamp::Fields.new }
|
5
|
+
|
6
|
+
it "delegates to capybara the responsibility to get the fields" do
|
7
|
+
element = {'name' => "username", 'type' => "text"}
|
8
|
+
element.stub(:visible?).and_return(true)
|
9
|
+
fields.page.should_receive(:all).with('input').and_return([element])
|
10
|
+
fields.get
|
11
|
+
end
|
12
|
+
|
13
|
+
context "when the input element is visible" do
|
14
|
+
context "when the name attribute is present" do
|
15
|
+
context "when it has the id attribute" do
|
16
|
+
it "returns the element in the array using the name as the name and the id as the selector" do
|
17
|
+
element = {'name' => "username", 'type' => "text", 'id' => "u_0_b"}
|
18
|
+
element.stub(:visible?).and_return(true)
|
19
|
+
fields.page.stub(:all).with('input').and_return([element])
|
20
|
+
fields.get.should have(1).field
|
21
|
+
fields.get.first.name.should == "username"
|
22
|
+
fields.get.first.selector.should == "u_0_b"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when its type is text" do
|
27
|
+
it "returns the element in the array" do
|
28
|
+
element = {'name' => "username", 'type' => "text"}
|
29
|
+
element.stub(:visible?).and_return(true)
|
30
|
+
fields.page.stub(:all).with('input').and_return([element])
|
31
|
+
fields.get.should have(1).field
|
32
|
+
fields.get.first.name.should == "username"
|
33
|
+
fields.get.first.selector.should == "username"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when its type is email" do
|
38
|
+
it "returns the element in the array" do
|
39
|
+
element = {'name' => "useremail", 'type' => "email"}
|
40
|
+
element.stub(:visible?).and_return(true)
|
41
|
+
fields.page.stub(:all).with('input').and_return([element])
|
42
|
+
fields.get.should have(1).field
|
43
|
+
fields.get.first.name.should == "useremail"
|
44
|
+
fields.get.first.selector.should == "useremail"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "when its type is password" do
|
49
|
+
it "returns the element in the array" do
|
50
|
+
element = {'name' => "password", 'type' => "password"}
|
51
|
+
element.stub(:visible?).and_return(true)
|
52
|
+
fields.page.stub(:all).with('input').and_return([element])
|
53
|
+
fields.get.should have(1).field
|
54
|
+
fields.get.first.name.should == "password"
|
55
|
+
fields.get.first.selector.should == "password"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when the input element type is invalid" do
|
60
|
+
it "returns an empty array" do
|
61
|
+
element = {'name' => "username", 'type' => "radio"}
|
62
|
+
element.stub(:visible?).and_return(true)
|
63
|
+
fields.page.stub(:all).with('input').and_return([element])
|
64
|
+
fields.get.should == []
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "when the input element has no name attribute" do
|
70
|
+
it "returns an empty array" do
|
71
|
+
element = {'name' => "", 'type' => "text"}
|
72
|
+
element.stub(:visible?).and_return(true)
|
73
|
+
fields.page.stub(:all).with('input').and_return([element])
|
74
|
+
fields.get.should == []
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when the input element is not visible" do
|
80
|
+
it "returns an empty array" do
|
81
|
+
element = {'name' => "username", 'type' => "text"}
|
82
|
+
element.stub(:visible?).and_return(false)
|
83
|
+
fields.page.stub(:all).with('input').and_return([element])
|
84
|
+
fields.get.should == []
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
module Swamp
|
3
|
+
describe Formatter do
|
4
|
+
let(:formatter) { formatter = Swamp::Formatter.new }
|
5
|
+
|
6
|
+
describe "#format" do
|
7
|
+
context "when the name has dashes" do
|
8
|
+
it "replaces the dashes with unsderscores" do
|
9
|
+
name = "user-name"
|
10
|
+
formatter.format(name).should == "user_name"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "when the name has white spaces" do
|
15
|
+
it "replaces the white spaces with underscores" do
|
16
|
+
name = "user name"
|
17
|
+
formatter.format(name).should == "user_name"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "when the name has underscore in the end" do
|
22
|
+
it "removes the underscores from the end" do
|
23
|
+
name = "user_name_"
|
24
|
+
formatter.format(name).should == "user_name"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when the name has uppercase characters" do
|
29
|
+
it "converts the characters to lowercase" do
|
30
|
+
name = "Sign up"
|
31
|
+
formatter.format(name).should == "sign_up"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when the name has parentheses" do
|
36
|
+
it "replace the parentheses with underscores" do
|
37
|
+
name = "user_name(title)"
|
38
|
+
formatter.format(name).should == "user_name_title"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when the name has brackets" do
|
43
|
+
it "replace the brackets with underscores" do
|
44
|
+
name = "user_name[title]"
|
45
|
+
formatter.format(name).should == "user_name_title"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#format_class" do
|
51
|
+
context "when the class has white spaces" do
|
52
|
+
it "replaces the white spaces with dots" do
|
53
|
+
class_name = "button g-button g-button-submit"
|
54
|
+
formatter.format_class(class_name).should == "button.g-button.g-button-submit"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|