swamp 1.1.2 → 1.2.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6ab9f667564af8b8a224893d26848f2eed6e4c7
4
- data.tar.gz: 47ba68f51f80b0a25b8fd1344c6e1faa2f30adb9
3
+ metadata.gz: 321488f9c4e58a1213185e05bce1473a6777bf6d
4
+ data.tar.gz: 929ab8a776d0303b5c211b1cd9e0787435d6ebd5
5
5
  SHA512:
6
- metadata.gz: 95d1df06d75df7a27a5a48f96e97caf2364f07f8dddfe241906e5eb7b126b04f15487ea990d2bd148288b075c83edfe02324bdb80d7c5b17b9770bc434c9b2a6
7
- data.tar.gz: 725f4c672d632660446e777a8688dfdb8a03183f45b4ceba59f5fff02e117edd972d5d99d2029181657f7e975f85519b29f1c7fd7362aabb21ff6200bd61c30a
6
+ metadata.gz: 3f9c38ba1d09493db01a42820a7befd550ba1d0b8d7fd20dc72d5423e81ea37e0a31e9415075a5a9441439c6bed341b6596ed3c3fe6b945466250c0c74caa317
7
+ data.tar.gz: 5324834c433679cf9a7409ceac75d3f9f4d886e759d1db0b8ce249e8714e4766aa4f5869b763d9d776272009b7519c560d5db135687e819fb78d7feab97018ec
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/Juraci/swamp.png?branch=master)](https://travis-ci.org/Juraci/swamp)
4
4
 
5
5
  Automatically generates the interfaces for the most common actions that a page can provide,
6
- so you can use the generated methods to quickly create your page objects using [capybara](https://github.com/jnicklas/capybara) and [capybara-page-object](https://github.com/andyw8/capybara-page-object).
6
+ so you can use the generated methods to quickly create your page objects using [capybara](https://github.com/jnicklas/capybara).
7
7
 
8
8
  ## How to install
9
9
 
@@ -42,22 +42,22 @@ https://accounts.google.com
42
42
  Scanning, please wait...
43
43
 
44
44
  def type_email(input)
45
- source.fill_in("Email", with: input)
45
+ page.fill_in("Email", with: input)
46
46
  end
47
47
  def type_passwd(input)
48
- source.fill_in("Passwd", with: input)
48
+ page.fill_in("Passwd", with: input)
49
49
  end
50
50
  def sign_in
51
- source.find(:css, "#signIn").click
51
+ page.find(:css, "#signIn").click
52
52
  end
53
53
  def select_lang_chooser(option)
54
- source.select(option, :from => "lang-chooser")
54
+ page.select(option, :from => "lang-chooser")
55
55
  end
56
56
  def link_forgot_passwd
57
- source.click_link("link-forgot-passwd")
57
+ page.click_link("link-forgot-passwd")
58
58
  end
59
59
  def link_signup
60
- source.click_link("link-signup")
60
+ page.click_link("link-signup")
61
61
  end
62
62
  ```
63
63
  (notice that the method names are a best guess and you are always free to change them)
@@ -66,32 +66,32 @@ end
66
66
 
67
67
  ```ruby
68
68
  module PageObjects
69
- class SignIn < CapybaraPageObject::Page
69
+ class SignIn
70
70
 
71
71
  path ""
72
72
 
73
73
  def type_email(input)
74
- source.fill_in("Email", with: input)
74
+ page.fill_in("Email", with: input)
75
75
  end
76
76
 
77
77
  def type_passwd(input)
78
- source.fill_in("Passwd", with: input)
78
+ page.fill_in("Passwd", with: input)
79
79
  end
80
80
 
81
81
  def sign_in
82
- source.find(:css, "#signIn").click
82
+ page.find(:css, "#signIn").click
83
83
  end
84
84
 
85
85
  def select_lang_chooser(option)
86
- source.select(option, :from => "lang-chooser")
86
+ page.select(option, :from => "lang-chooser")
87
87
  end
88
88
 
89
89
  def link_forgot_passwd
90
- source.click_link("link-forgot-passwd")
90
+ page.click_link("link-forgot-passwd")
91
91
  end
92
92
 
93
93
  def link_signup
94
- source.click_link("link-signup")
94
+ page.click_link("link-signup")
95
95
  end
96
96
  end
97
97
  end
@@ -118,6 +118,22 @@ end
118
118
  * Then just go to the terminal and hit ENTER
119
119
  * Swamp will detect the new elements (if any) and will generate the code snippets the same way as before
120
120
 
121
+ ### Generating code snippets to use with [capybara-page-object](https://github.com/andyw8/capybara-page-object)
122
+
123
+ You can easily change the scope from "page" to "source" by using the following command:
124
+
125
+ ```shell
126
+ :scope = source
127
+ ```
128
+
129
+ then the code snippets will be generated with the "source" scope like this:
130
+
131
+ ```shell
132
+ def link_signup
133
+ source.click_link("link-signup")
134
+ end
135
+ ```
136
+
121
137
  ## How it works?
122
138
 
123
139
  It uses capybara to fireup the browser and visit the target URL then it looks for patterns like:
data/bin/swamp CHANGED
@@ -2,19 +2,23 @@
2
2
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
3
3
  require 'swamp'
4
4
 
5
+ setup = Swamp::Setup.new
5
6
  fields = Swamp::Fields.new
6
7
  buttons = Swamp::Buttons.new
7
8
  input_buttons = Swamp::InputButtons.new
8
9
  select_boxes = Swamp::SelectBoxes.new
9
10
  links = Swamp::Links.new
10
11
  meta_collection = [fields, buttons, input_buttons, select_boxes, links]
11
- wrapper = Swamp::Wrapper.new(meta_collection)
12
- swamp = Swamp::Interface.new(STDOUT, wrapper)
12
+ wrapper = Swamp::Wrapper.new(meta_collection, setup)
13
+ swamp = Swamp::Interface.new(STDOUT, wrapper, setup)
13
14
  swamp.run
14
15
 
15
16
  while input = gets
16
17
  if input.chomp == 'exit'
17
18
  exit
19
+ elsif input[0] == ':'
20
+ swamp.setup_command(input)
21
+ else
22
+ swamp.scan(input)
18
23
  end
19
- swamp.scan(input)
20
24
  end
@@ -0,0 +1,34 @@
1
+ Feature: user changes the scope in the runtime
2
+
3
+ As a swamp user
4
+ I want to change the scope from "page" to "source"
5
+ So I can have snippets that are compatible with capybara-page-object gem
6
+
7
+ Scenario Outline: User receives feedback from swamp on command execution
8
+ Given that swamp is already running
9
+ When I attempt to execute the command <command>
10
+ Then I should see <expected>
11
+
12
+ Examples: Happy paths
13
+ | command | expected |
14
+ | ":scope = source" | "Option :scope set to source" |
15
+ | ":scope = page" | "Option :scope set to page" |
16
+
17
+ Examples: Corner cases
18
+ | command | expected |
19
+ | ":scope=source" | "Option :scope set to source" |
20
+ | ":scope = page" | "Option :scope set to page" |
21
+ | ":scope = " | "Invalid command" |
22
+ | ":whatever = random" | "Invalid command" |
23
+ | ": = " | "Invalid command" |
24
+
25
+ Scenario: User changes the scope from page to source
26
+ Given that swamp is already running
27
+ When I attempt to execute the command ":scope = source"
28
+ And I attempt to scan this page: "button.html"
29
+ Then swamp should output the following code snippet
30
+ """
31
+ def sign_up
32
+ source.click_button("Sign Up")
33
+ end
34
+ """
@@ -14,7 +14,7 @@ Feature: user scans buttons in a page
14
14
  And it should output the following code snippet
15
15
  """
16
16
  def sign_up
17
- source.click_button("Sign Up")
17
+ page.click_button("Sign Up")
18
18
  end
19
19
  """
20
20
 
@@ -25,7 +25,7 @@ Feature: user scans buttons in a page
25
25
  And it should output the following code snippet
26
26
  """
27
27
  def search_button
28
- source.click_button("search-button")
28
+ page.click_button("search-button")
29
29
  end
30
30
  """
31
31
 
@@ -36,7 +36,7 @@ Feature: user scans buttons in a page
36
36
  And it should output the following code snippet
37
37
  """
38
38
  def buy_now
39
- source.click_button("buy-now")
39
+ page.click_button("buy-now")
40
40
  end
41
41
  """
42
42
 
@@ -14,7 +14,7 @@ Feature: user scans fields in a page
14
14
  And it should output the following code snippet
15
15
  """
16
16
  def type_username(input)
17
- source.fill_in("id_username", with: input)
17
+ page.fill_in("id_username", with: input)
18
18
  end
19
19
  """
20
20
 
@@ -25,7 +25,7 @@ Feature: user scans fields in a page
25
25
  Then it should output the following code snippet
26
26
  """
27
27
  def type_username(input)
28
- source.fill_in("username", with: input)
28
+ page.fill_in("username", with: input)
29
29
  end
30
30
  """
31
31
 
@@ -36,7 +36,7 @@ Feature: user scans fields in a page
36
36
  Then it should output the following code snippet
37
37
  """
38
38
  def type_username(input)
39
- source.fill_in("username", with: input)
39
+ page.fill_in("username", with: input)
40
40
  end
41
41
  """
42
42
 
@@ -14,7 +14,7 @@ Feature: user scans submits in a page
14
14
  And it should output the following code snippet
15
15
  """
16
16
  def log_in
17
- source.find(:css, "#u_0_b").click
17
+ page.find(:css, "#u_0_b").click
18
18
  end
19
19
  """
20
20
 
@@ -25,6 +25,6 @@ Feature: user scans submits in a page
25
25
  And it should output the following code snippet
26
26
  """
27
27
  def continue
28
- source.find(:css, "input.button.g-button.g-button-submit[value='Continue']").click
28
+ page.find(:css, "input.button.g-button.g-button-submit[value='Continue']").click
29
29
  end
30
30
  """
@@ -14,6 +14,6 @@ Feature: user scans links in a page
14
14
  And it should output the following code snippet
15
15
  """
16
16
  def link_forgot_passwd
17
- source.click_link("link-forgot-passwd")
17
+ page.click_link("link-forgot-passwd")
18
18
  end
19
19
  """
@@ -14,7 +14,7 @@ Feature: user scans select box elements in a page
14
14
  And it should output the following code snippet
15
15
  """
16
16
  def select_month(option)
17
- source.select(option, :from => "month")
17
+ page.select(option, :from => "month")
18
18
  end
19
19
  """
20
20
 
@@ -25,7 +25,7 @@ Feature: user scans select box elements in a page
25
25
  And it should output the following code snippet
26
26
  """
27
27
  def select_region(option)
28
- source.select(option, :from => "Region")
28
+ page.select(option, :from => "Region")
29
29
  end
30
30
  """
31
31
 
@@ -36,6 +36,6 @@ Feature: user scans select box elements in a page
36
36
  And it should output the following code snippet
37
37
  """
38
38
  def provider_select(option)
39
- source.find(:css, "select.provider-select option[value='#{option}']").click
39
+ page.find(:css, "select.provider-select option[value='#{option}']").click
40
40
  end
41
41
  """
@@ -6,7 +6,7 @@ When /^(?:I start it|that swamp is already running)$/ do
6
6
  end
7
7
 
8
8
  Then /^I should see "(.*?)"$/ do |outcome|
9
- output.messages.should include(outcome)
9
+ fake_output.messages.should include(outcome)
10
10
  end
11
11
 
12
12
  Given /^I enter the url for this page: "(\w+\.html)"$/ do |page|
@@ -18,15 +18,22 @@ When /^swamp scans that page$/ do
18
18
  swamp.scan(@url)
19
19
  end
20
20
 
21
+ When /^I attempt to scan this page: "(\w+\.html)"$/ do |page|
22
+ path = File.join(File.dirname(__FILE__), '../support/page_examples/', page)
23
+ url = "file://#{path}"
24
+ swamp.scan(url)
25
+ end
26
+
21
27
  Then /^(?:swamp|it) should output the following code snippets?$/ do |string|
22
- output.messages.should include(string)
28
+ fake_output.messages.should include(string)
23
29
  end
24
30
 
25
31
  Then /^swamp should not output any snippet$/ do
26
- output.should have_at_most(3).messages
27
- output.messages.each do |m|
32
+ fake_output.messages.size.should <= 3
33
+ fake_output.messages.each do |m|
28
34
  m.should_not include("def")
29
35
  m.should_not include("source.")
36
+ m.should_not include("page.")
30
37
  end
31
38
  end
32
39
 
@@ -35,7 +42,7 @@ Given /^that swamp already have scanned a page$/ do
35
42
  path = File.join(File.dirname(__FILE__), '../support/page_examples/', "button.html")
36
43
  @url = "file://#{path}"
37
44
  swamp.scan(@url)
38
- output.messages.should include("def sign_up\n source.click_button(\"Sign Up\")\nend")
45
+ fake_output.messages.should include("def sign_up\n page.click_button(\"Sign Up\")\nend")
39
46
  end
40
47
 
41
48
  When /^I attempt to hit enter at the terminal$/ do
@@ -43,9 +50,9 @@ When /^I attempt to hit enter at the terminal$/ do
43
50
  end
44
51
 
45
52
  Then /^swamp should scan the current page$/ do
46
- output.should have_at_most(5).messages
47
- output.messages[3].should == "Scanning, please wait..."
48
- output.messages[4].should == "def sign_up\n source.click_button(\"Sign Up\")\nend"
53
+ fake_output.messages.size.should <= 5
54
+ fake_output.messages[3].should == "Scanning, please wait..."
55
+ fake_output.messages[4].should == "def sign_up\n page.click_button(\"Sign Up\")\nend"
49
56
  end
50
57
 
51
58
  Then /^swamp should highlight this (element|link): "(.+)"$/ do |mode, selector|
@@ -58,3 +65,7 @@ Then /^swamp should highlight this (element|link): "(.+)"$/ do |mode, selector|
58
65
  page.find(selector)[:style].should include("background-color: rgb(255, 0, 0)")
59
66
  end
60
67
  end
68
+
69
+ When /^I attempt to execute the command "(.*?)"$/ do |command|
70
+ swamp.setup_command(command)
71
+ end
@@ -8,8 +8,12 @@ class Output
8
8
  end
9
9
  end
10
10
 
11
- def output
12
- @output ||= Output.new
11
+ def fake_output
12
+ @fake_output ||= Output.new
13
+ end
14
+
15
+ def setup
16
+ @setup ||= Swamp::Setup.new
13
17
  end
14
18
 
15
19
  def buttons
@@ -34,9 +38,10 @@ end
34
38
 
35
39
  def wrapper
36
40
  meta_collection = [fields, buttons, input_buttons, select_boxes, links]
37
- Swamp::Wrapper.new(meta_collection)
41
+ Swamp::Wrapper.new(meta_collection, setup)
38
42
  end
39
43
 
44
+
40
45
  def swamp
41
- @swamp ||= Swamp::Interface.new(output, wrapper)
46
+ @swamp ||= Swamp::Interface.new(fake_output, wrapper, setup)
42
47
  end
data/lib/swamp.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'swamp/version'
2
+ require 'swamp/assertions'
3
+ require 'swamp/setup'
2
4
  require 'swamp/interface'
3
5
  require 'swamp/evaluator'
4
6
  require 'swamp/base'
@@ -0,0 +1,7 @@
1
+ module Swamp
2
+ module Assertions
3
+ def assert &block
4
+ raise ArgumentError unless block.call
5
+ end
6
+ end
7
+ end
data/lib/swamp/builder.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  module Swamp
2
2
  class Builder
3
- def initialize(element)
3
+
4
+ attr_reader :element
5
+
6
+ def initialize(element, setup)
4
7
  @element = element
8
+ @setup = setup
5
9
  end
6
10
 
7
11
  def build_snippet
8
- if @element.name
9
- [method_definition, @element.method_signature, line_break, identation, prefix, @element.accessor, line_break, method_end].join
12
+ if element.name
13
+ [method_definition, element.method_signature, line_break, identation, scope, element.accessor, line_break, method_end].join
10
14
  else
11
- [prefix, @element.accessor].join
15
+ [scope, element.accessor].join
12
16
  end
13
17
  end
14
18
 
@@ -20,8 +24,8 @@ module Swamp
20
24
  "end"
21
25
  end
22
26
 
23
- def prefix
24
- "source."
27
+ def scope
28
+ [@setup.scope, "."].join
25
29
  end
26
30
 
27
31
  def identation
@@ -5,9 +5,10 @@ module Swamp
5
5
  INVALID_REQUEST_MESSAGE = ['Please enter a valid url!']
6
6
  NO_ELEMENTS_MESSAGE = ['No elements were detected']
7
7
 
8
- def initialize(output, wrapper)
8
+ def initialize(output, wrapper, setup)
9
9
  @output = output
10
10
  @wrapper = wrapper
11
+ @setup = setup
11
12
  end
12
13
 
13
14
  def run
@@ -20,7 +21,10 @@ module Swamp
20
21
  present messages
21
22
  end
22
23
 
23
- private
24
+ def setup_command(input)
25
+ messages = @setup.handle_command(input)
26
+ present messages
27
+ end
24
28
 
25
29
  def present(messages)
26
30
  messages.each do |message|
@@ -28,6 +32,8 @@ module Swamp
28
32
  end
29
33
  end
30
34
 
35
+ private
36
+
31
37
  def valid_request?(input)
32
38
  evaluator(input).valid_url? or evaluator(input).refresh_command?
33
39
  end