swamp 1.1.2 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,49 @@
1
+ module Swamp
2
+ class Setup
3
+ attr_reader :scope
4
+
5
+ COMMAND_LIST = {
6
+ ":scope" => ["source", "page"]
7
+ }
8
+
9
+ include Swamp::Assertions
10
+
11
+ def initialize
12
+ @scope = "page"
13
+ end
14
+
15
+ def handle_command(input)
16
+ @input = input
17
+ remove_white_spaces
18
+
19
+ begin
20
+ assert { COMMAND_LIST[command] && COMMAND_LIST[command].include?(value) }
21
+ rescue ArgumentError
22
+ return ["Invalid command"]
23
+ end
24
+
25
+ @scope = value
26
+
27
+ [success_message]
28
+ end
29
+
30
+ private
31
+
32
+ def success_message
33
+ ["Option", " ", command, " ", "set", " ", "to", " ", value].join
34
+ end
35
+
36
+ def command
37
+ @input.split("=")[0]
38
+ end
39
+
40
+ def value
41
+ @input.split("=")[1]
42
+ end
43
+
44
+ def remove_white_spaces
45
+ @input = @input.gsub(/\s+/, "")
46
+ end
47
+
48
+ end
49
+ end
data/lib/swamp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Swamp
2
- VERSION = '1.1.2'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/swamp/wrapper.rb CHANGED
@@ -1,12 +1,13 @@
1
1
  module Swamp
2
2
  class Wrapper < Base
3
- def initialize(meta_collection)
3
+ attr_reader :page_visited
4
+
5
+ def initialize(meta_collection, setup)
4
6
  @meta_collection = meta_collection
5
7
  @page_visited = false
8
+ @setup = setup
6
9
  end
7
10
 
8
- attr_reader :page_visited
9
-
10
11
  def explore(url)
11
12
  if !page_visited
12
13
  visit url
@@ -17,7 +18,7 @@ module Swamp
17
18
  def scan
18
19
  found_snippets = []
19
20
  @meta_collection.each do | element_collection |
20
- found_snippets += element_collection.get.map { | element | Swamp::Builder.new(element).build_snippet }
21
+ found_snippets += element_collection.get.map { | element | Swamp::Builder.new(element, @setup).build_snippet }
21
22
  end
22
23
  found_snippets
23
24
  end
data/spec/spec_helper.rb CHANGED
@@ -2,7 +2,7 @@ require 'swamp'
2
2
 
3
3
  RSpec.configure do |config|
4
4
  # Use color in STDOUT
5
- config.color_enabled = true
5
+ config.color = true
6
6
 
7
7
  # Use color not only in STDOUT but also in pagers and files
8
8
  #config.tty = true
@@ -1,35 +1,45 @@
1
1
  require 'spec_helper'
2
2
  module Swamp
3
3
  describe Builder do
4
+ let(:setup) { setup = Swamp::Setup.new }
5
+
4
6
  describe "#build_method" do
5
7
  context "when the type is field" do
6
- let(:type) { :field }
7
8
  context "when the name and the selector are present" do
8
9
  it "returns the method's snippet" do
9
10
  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"
11
+ builder = Swamp::Builder.new(element, setup)
12
+ expect(builder.build_snippet).to eq("def type_user_name(input)\n page.fill_in(\"User-Name\", with: input)\nend")
12
13
  end
13
14
  end
14
15
  end
15
16
 
16
17
  context "when the type is button" do
17
- let(:type) { :button }
18
18
  context "when the name and the selector are present" do
19
19
  it "returns the method's snippet" do
20
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"
21
+ builder = Swamp::Builder.new(element, setup)
22
+ expect(builder.build_snippet).to eq("def log_in\n page.click_button(\"Log_in\")\nend")
23
23
  end
24
24
  end
25
25
 
26
26
  context "when just the selector is present" do
27
27
  it "returns just the selector snippet" do
28
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\")"
29
+ builder = Swamp::Builder.new(element, setup)
30
+ expect(builder.build_snippet).to eq("page.click_button(\"$ 9.90 Buy\")")
31
+ end
32
+ end
33
+
34
+ context "when the scope has changed" do
35
+ it "returns the method's snippet with the changed scope" do
36
+ element = Swamp::Button.new("Log_in", "Log_in")
37
+ allow(setup).to receive(:scope).and_return("source")
38
+ builder = Swamp::Builder.new(element, setup)
39
+ expect(builder.build_snippet).to eq("def log_in\n source.click_button(\"Log_in\")\nend")
31
40
  end
32
41
  end
42
+
33
43
  end
34
44
  end
35
45
  end
@@ -5,10 +5,10 @@ module Swamp
5
5
 
6
6
  it "delegates to capybara the responsibility to get the buttons" do
7
7
  element = {'id' => "u_0_2"}
8
- element.stub(:text).and_return("Sign Up")
9
- element.stub(:visible?).and_return(true)
10
- buttons.page.stub(:execute_script).and_return(nil)
11
- buttons.page.should_receive(:all).with('button').and_return([element])
8
+ allow(element).to receive(:text).and_return("Sign Up")
9
+ allow(element).to receive(:visible?).and_return(true)
10
+ allow(buttons.page).to receive(:execute_script).and_return(nil)
11
+ expect(buttons.page).to receive(:all).with('button').and_return([element])
12
12
  buttons.get
13
13
  end
14
14
 
@@ -17,21 +17,21 @@ module Swamp
17
17
  let(:element) { {'id' => "u_0_2"} }
18
18
 
19
19
  before(:each) do
20
- element.stub(:visible?).and_return(true)
21
- element.stub(:text).and_return("Sign Up")
22
- buttons.page.stub(:all).with('button').and_return([element])
20
+ allow(element).to receive(:visible?).and_return(true)
21
+ allow(element).to receive(:text).and_return("Sign Up")
22
+ allow(buttons.page).to receive(:all).with('button').and_return([element])
23
23
  end
24
24
 
25
25
  it "highlights the element" do
26
- buttons.page.should_receive(:execute_script).twice
26
+ expect(buttons.page).to receive(:execute_script).twice
27
27
  buttons.get
28
28
  end
29
29
 
30
30
  it "returns the element in the array using the text as both the name and the selector" do
31
- buttons.page.stub(:execute_script).and_return(nil)
32
- buttons.get.should have(1).button
33
- buttons.get.each.first.name.should == "Sign Up"
34
- buttons.get.each.first.selector.should == "Sign Up"
31
+ allow(buttons.page).to receive(:execute_script).and_return(nil)
32
+ expect(buttons.get.size).to eq(1)
33
+ expect(buttons.get.each.first.name).to eq("Sign Up")
34
+ expect(buttons.get.each.first.selector).to eq("Sign Up")
35
35
  end
36
36
  end
37
37
 
@@ -40,21 +40,21 @@ module Swamp
40
40
  let(:element) { {'id' => "search-button"} }
41
41
 
42
42
  before(:each) do
43
- element.stub(:visible?).and_return(true)
44
- element.stub(:text).and_return("")
45
- buttons.page.stub(:all).with('button').and_return([element])
43
+ allow(element).to receive(:visible?).and_return(true)
44
+ allow(element).to receive(:text).and_return("")
45
+ allow(buttons.page).to receive(:all).with('button').and_return([element])
46
46
  end
47
47
 
48
48
  it "highlights the element" do
49
- buttons.page.should_receive(:execute_script).twice
49
+ expect(buttons.page).to receive(:execute_script).twice
50
50
  buttons.get
51
51
  end
52
52
 
53
53
  it "returns the element in the array using the id as both the name and the selector" do
54
- buttons.page.stub(:execute_script).and_return(nil)
55
- buttons.get.should have(1).button
56
- buttons.get.each.first.name.should == "search-button"
57
- buttons.get.each.first.selector.should == "search-button"
54
+ allow(buttons.page).to receive(:execute_script).and_return(nil)
55
+ expect(buttons.get.size).to eq(1)
56
+ expect(buttons.get.each.first.name).to eq("search-button")
57
+ expect(buttons.get.each.first.selector).to eq("search-button")
58
58
  end
59
59
  end
60
60
 
@@ -63,21 +63,21 @@ module Swamp
63
63
  let(:element) { {'value' => "search-button", 'id' => ""} }
64
64
 
65
65
  before(:each) do
66
- element.stub(:visible?).and_return(true)
67
- element.stub(:text).and_return("")
68
- buttons.page.stub(:all).with('button').and_return([element])
66
+ allow(element).to receive(:visible?).and_return(true)
67
+ allow(element).to receive(:text).and_return("")
68
+ allow(buttons.page).to receive(:all).with('button').and_return([element])
69
69
  end
70
70
 
71
71
  it "highlights the element" do
72
- buttons.page.should_receive(:execute_script).twice
72
+ expect(buttons.page).to receive(:execute_script).twice
73
73
  buttons.get
74
74
  end
75
75
 
76
76
  it "returns the element in the array using the value as both the name and the selector" do
77
- buttons.page.stub(:execute_script).and_return(nil)
78
- buttons.get.should have(1).button
79
- buttons.get.each.first.name.should == "search-button"
80
- buttons.get.each.first.selector.should == "search-button"
77
+ allow(buttons.page).to receive(:execute_script).and_return(nil)
78
+ expect(buttons.get.size).to eq(1)
79
+ expect(buttons.get.each.first.name).to eq("search-button")
80
+ expect(buttons.get.each.first.selector).to eq("search-button")
81
81
  end
82
82
  end
83
83
 
@@ -85,14 +85,14 @@ module Swamp
85
85
  let(:element) { {'value' => "", 'id' => ""} }
86
86
 
87
87
  before(:each) do
88
- element.stub(:visible?).and_return(true)
89
- element.stub(:text).and_return("")
90
- buttons.page.stub(:all).with('button').and_return([element])
88
+ allow(element).to receive(:visible?).and_return(true)
89
+ allow(element).to receive(:text).and_return("")
90
+ allow(buttons.page).to receive(:all).with('button').and_return([element])
91
91
  end
92
92
 
93
93
  it "returns an empty array" do
94
- buttons.stub(:all).with('button').and_return([element])
95
- buttons.get.should == []
94
+ allow(buttons).to receive(:all).with('button').and_return([element])
95
+ expect(buttons.get).to eq([])
96
96
  end
97
97
  end
98
98
  end
@@ -102,10 +102,10 @@ module Swamp
102
102
  context "when the button element is not visible" do
103
103
  it "returns an empty array" do
104
104
  element = {'id' => "u_0_2"}
105
- element.stub(:text).and_return("Sign Up")
106
- element.stub(:visible?).and_return(false)
107
- buttons.page.stub(:all).with('button').and_return([element])
108
- buttons.get.should == []
105
+ allow(element).to receive(:text).and_return("Sign Up")
106
+ allow(element).to receive(:visible?).and_return(false)
107
+ allow(buttons.page).to receive(:all).with('button').and_return([element])
108
+ expect(buttons.get).to eq([])
109
109
  end
110
110
  end
111
111
  end
@@ -9,7 +9,7 @@ module Swamp
9
9
  it "returns true" do
10
10
  input = "http://fakepage.com"
11
11
  evaluator = Swamp::Evaluator.new(input, wrapper)
12
- evaluator.should be_valid_url
12
+ expect(evaluator).to be_valid_url
13
13
  end
14
14
  end
15
15
 
@@ -17,7 +17,7 @@ module Swamp
17
17
  it "returns true" do
18
18
  input = "https://fakepage.com"
19
19
  evaluator = Swamp::Evaluator.new(input, wrapper)
20
- evaluator.should be_valid_url
20
+ expect(evaluator).to be_valid_url
21
21
  end
22
22
  end
23
23
 
@@ -25,7 +25,7 @@ module Swamp
25
25
  it "returns true" do
26
26
  input = "file://fakepage.html"
27
27
  evaluator = Swamp::Evaluator.new(input, wrapper)
28
- evaluator.should be_valid_url
28
+ expect(evaluator).to be_valid_url
29
29
  end
30
30
  end
31
31
  end
@@ -35,7 +35,7 @@ module Swamp
35
35
  it "returns false" do
36
36
  input = "www.fakepage.com"
37
37
  evaluator = Swamp::Evaluator.new(input, wrapper)
38
- evaluator.should_not be_valid_url
38
+ expect(evaluator).not_to be_valid_url
39
39
  end
40
40
  end
41
41
  end
@@ -45,29 +45,29 @@ module Swamp
45
45
  context "when the input is an enter keystroke" do
46
46
  context "when a page was already visited" do
47
47
  it "returns true" do
48
- wrapper.stub(:page_visited).and_return(true)
48
+ allow(wrapper).to receive(:page_visited).and_return(true)
49
49
  input = "\n"
50
50
  evaluator = Swamp::Evaluator.new(input, wrapper)
51
- evaluator.should be_refresh_command
51
+ expect(evaluator).to be_refresh_command
52
52
  end
53
53
  end
54
54
 
55
55
  context "when no page was yet visited" do
56
56
  it "returns false" do
57
- wrapper.stub(:page_visited).and_return(false)
57
+ allow(wrapper).to receive(:page_visited).and_return(false)
58
58
  input = "\n"
59
59
  evaluator = Swamp::Evaluator.new(input, wrapper)
60
- evaluator.should_not be_refresh_command
60
+ expect(evaluator).not_to be_refresh_command
61
61
  end
62
62
  end
63
63
  end
64
64
 
65
65
  context "when the input is not an enter keystroke" do
66
66
  it "returns false" do
67
- wrapper.stub(:page_visited).and_return(true)
67
+ allow(wrapper).to receive(:page_visited).and_return(true)
68
68
  input = " "
69
69
  evaluator = Swamp::Evaluator.new(input, wrapper)
70
- evaluator.should_not be_refresh_command
70
+ expect(evaluator).not_to be_refresh_command
71
71
  end
72
72
  end
73
73
  end
@@ -5,9 +5,9 @@ module Swamp
5
5
 
6
6
  it "delegates to capybara the responsibility to get the fields" do
7
7
  element = {'name' => "username", 'type' => "text"}
8
- element.stub(:visible?).and_return(true)
9
- fields.page.stub(:execute_script).and_return(nil)
10
- fields.page.should_receive(:all).with('input').and_return([element])
8
+ allow(element).to receive(:visible?).and_return(true)
9
+ allow(fields.page).to receive(:execute_script).and_return(nil)
10
+ expect(fields.page).to receive(:all).with('input').and_return([element])
11
11
  fields.get
12
12
  end
13
13
 
@@ -19,25 +19,25 @@ module Swamp
19
19
  let (:element) { {'name' => "username", 'type' => type, 'id' => "u_0_b"} }
20
20
 
21
21
  before(:each) do
22
- element.stub(:visible?).and_return(true)
23
- fields.page.stub(:all).with('input').and_return([element])
22
+ allow(element).to receive(:visible?).and_return(true)
23
+ allow(fields.page).to receive(:all).with('input').and_return([element])
24
24
  end
25
25
 
26
26
  it "highlights the element" do
27
- fields.page.should_receive(:execute_script).twice
27
+ expect(fields.page).to receive(:execute_script).twice
28
28
  fields.get
29
29
  end
30
30
 
31
31
  it "returns the element in the array using the name as the method's name" do
32
- fields.page.stub(:execute_script).and_return(nil)
33
- fields.get.should have(1).field
34
- fields.get.first.name.should == "username"
32
+ allow(fields.page).to receive(:execute_script).and_return(nil)
33
+ expect(fields.get.size).to eq(1)
34
+ expect(fields.get.first.name).to eq("username")
35
35
  end
36
36
 
37
37
  it "returns the element in the array using the id as the selector" do
38
- fields.page.stub(:execute_script).and_return(nil)
39
- fields.get.should have(1).field
40
- fields.get.first.selector.should == "u_0_b"
38
+ allow(fields.page).to receive(:execute_script).and_return(nil)
39
+ expect(fields.get.size).to eq(1)
40
+ expect(fields.get.first.selector).to eq("u_0_b")
41
41
  end
42
42
  end
43
43
 
@@ -45,25 +45,25 @@ module Swamp
45
45
  let (:element) { {'type' => type, 'id' => "username"} }
46
46
 
47
47
  before(:each) do
48
- element.stub(:visible?).and_return(true)
49
- fields.page.stub(:all).with('input').and_return([element])
48
+ allow(element).to receive(:visible?).and_return(true)
49
+ allow(fields.page).to receive(:all).with('input').and_return([element])
50
50
  end
51
51
 
52
52
  it "highlights the element" do
53
- fields.page.should_receive(:execute_script).twice
53
+ expect(fields.page).to receive(:execute_script).twice
54
54
  fields.get
55
55
  end
56
56
 
57
57
  it "returns the element in the array using the id as the method's name" do
58
- fields.page.stub(:execute_script).and_return(nil)
59
- fields.get.should have(1).field
60
- fields.get.first.name.should == "username"
58
+ allow(fields.page).to receive(:execute_script).and_return(nil)
59
+ expect(fields.get.size).to eq(1)
60
+ expect(fields.get.first.name).to eq("username")
61
61
  end
62
62
 
63
63
  it "returns the element in the array using the id as the selector" do
64
- fields.page.stub(:execute_script).and_return(nil)
65
- fields.get.should have(1).field
66
- fields.get.first.selector.should == "username"
64
+ allow(fields.page).to receive(:execute_script).and_return(nil)
65
+ expect(fields.get.size).to eq(1)
66
+ expect(fields.get.first.selector).to eq("username")
67
67
  end
68
68
  end
69
69
 
@@ -71,25 +71,25 @@ module Swamp
71
71
  let (:element) { {'name' => "username", 'type' => type} }
72
72
 
73
73
  before(:each) do
74
- element.stub(:visible?).and_return(true)
75
- fields.page.stub(:all).with('input').and_return([element])
74
+ allow(element).to receive(:visible?).and_return(true)
75
+ allow(fields.page).to receive(:all).with('input').and_return([element])
76
76
  end
77
77
 
78
78
  it "highlights the element" do
79
- fields.page.should_receive(:execute_script).twice
79
+ expect(fields.page).to receive(:execute_script).twice
80
80
  fields.get
81
81
  end
82
82
 
83
83
  it "returns the element in the array using the name as the method's name" do
84
- fields.page.stub(:execute_script).and_return(nil)
85
- fields.get.should have(1).field
86
- fields.get.first.name.should == "username"
84
+ allow(fields.page).to receive(:execute_script).and_return(nil)
85
+ expect(fields.get.size).to eq(1)
86
+ expect(fields.get.first.name).to eq("username")
87
87
  end
88
88
 
89
89
  it "returns the element in the array using the name as the selector" do
90
- fields.page.stub(:execute_script).and_return(nil)
91
- fields.get.should have(1).field
92
- fields.get.first.selector.should == "username"
90
+ allow(fields.page).to receive(:execute_script).and_return(nil)
91
+ expect(fields.get.size).to eq(1)
92
+ expect(fields.get.first.selector).to eq("username")
93
93
  end
94
94
  end
95
95
  end
@@ -99,9 +99,9 @@ module Swamp
99
99
 
100
100
  it "returns an empty array" do
101
101
  element = {'name' => "username", 'type' => type}
102
- element.stub(:visible?).and_return(true)
103
- fields.page.stub(:all).with('input').and_return([element])
104
- fields.get.should == []
102
+ allow(element).to receive(:visible?).and_return(true)
103
+ allow(fields.page).to receive(:all).with('input').and_return([element])
104
+ expect(fields.get).to eq([])
105
105
  end
106
106
  end
107
107
  end
@@ -109,9 +109,9 @@ module Swamp
109
109
  context "when the input element is not visible" do
110
110
  it "returns an empty array" do
111
111
  element = {'name' => "username", 'type' => "text"}
112
- element.stub(:visible?).and_return(false)
113
- fields.page.stub(:all).with('input').and_return([element])
114
- fields.get.should == []
112
+ allow(element).to receive(:visible?).and_return(false)
113
+ allow(fields.page).to receive(:all).with('input').and_return([element])
114
+ expect(fields.get).to eq([])
115
115
  end
116
116
  end
117
117
  end