howitzer 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (81) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -1
  3. data/.travis.yml +3 -2
  4. data/CHANGELOG.md +25 -5
  5. data/GETTING_STARTED.md +158 -13
  6. data/README.md +49 -32
  7. data/Rakefile +10 -1
  8. data/bin/howitzer +49 -78
  9. data/features/cli_help.feature +30 -0
  10. data/features/cli_new.feature +263 -0
  11. data/features/cli_unknown.feature +17 -0
  12. data/features/cli_version.feature +14 -0
  13. data/features/step_definitions/common_steps.rb +1 -0
  14. data/features/support/env.rb +1 -0
  15. data/features/support/transformers.rb +3 -0
  16. data/generators/base_generator.rb +2 -0
  17. data/generators/config/config_generator.rb +1 -1
  18. data/generators/config/templates/default.yml +4 -14
  19. data/generators/cucumber/cucumber_generator.rb +1 -1
  20. data/generators/cucumber/templates/cucumber.yml +1 -2
  21. data/generators/cucumber/templates/env.rb +8 -7
  22. data/generators/emails/emails_generator.rb +1 -1
  23. data/generators/pages/pages_generator.rb +1 -1
  24. data/generators/pages/templates/example_page.rb +2 -2
  25. data/generators/root/root_generator.rb +1 -1
  26. data/generators/root/templates/Gemfile +1 -1
  27. data/generators/rspec/rspec_generator.rb +1 -1
  28. data/generators/rspec/templates/example_spec.rb +2 -2
  29. data/generators/rspec/templates/rspec.rake +1 -1
  30. data/generators/rspec/templates/spec_helper.rb +6 -5
  31. data/generators/tasks/tasks_generator.rb +1 -1
  32. data/generators/tasks/templates/common.rake +1 -0
  33. data/howitzer.gemspec +8 -8
  34. data/lib/howitzer.rb +4 -1
  35. data/lib/howitzer/blank_page.rb +6 -0
  36. data/lib/howitzer/capybara/dsl_ex.rb +15 -0
  37. data/lib/howitzer/capybara/settings.rb +267 -0
  38. data/lib/howitzer/email.rb +134 -0
  39. data/lib/howitzer/exceptions.rb +18 -0
  40. data/lib/howitzer/helpers.rb +34 -23
  41. data/lib/howitzer/init.rb +1 -4
  42. data/lib/howitzer/mailgun/client.rb +48 -0
  43. data/lib/howitzer/mailgun/connector.rb +34 -0
  44. data/lib/howitzer/mailgun/response.rb +28 -0
  45. data/lib/howitzer/utils.rb +2 -2
  46. data/lib/howitzer/utils/data_generator/data_storage.rb +15 -2
  47. data/lib/howitzer/utils/data_generator/gen.rb +14 -10
  48. data/lib/howitzer/utils/locator_store.rb +14 -7
  49. data/lib/howitzer/utils/log.rb +2 -0
  50. data/lib/howitzer/utils/page_validator.rb +74 -27
  51. data/lib/howitzer/version.rb +1 -1
  52. data/lib/howitzer/web_page.rb +83 -32
  53. data/spec/config/default.yml +10 -12
  54. data/spec/spec_helper.rb +12 -0
  55. data/spec/support/mailgun_unit_client.rb +60 -0
  56. data/spec/unit/generators/generators_spec.rb +7 -7
  57. data/spec/unit/lib/capybara/dsl_ex_spec.rb +60 -0
  58. data/spec/unit/lib/{capybara_settings_spec.rb → capybara/settings_spec.rb} +16 -10
  59. data/spec/unit/lib/email_spec.rb +129 -0
  60. data/spec/unit/lib/helpers_spec.rb +160 -34
  61. data/spec/unit/lib/init_spec.rb +1 -12
  62. data/spec/unit/lib/mailgun/client_spec.rb +36 -0
  63. data/spec/unit/lib/mailgun/connector_spec.rb +70 -0
  64. data/spec/unit/lib/mailgun/response_spec.rb +29 -0
  65. data/spec/unit/lib/utils/data_generator/data_storage_spec.rb +23 -5
  66. data/spec/unit/lib/utils/data_generator/gen_spec.rb +2 -63
  67. data/spec/unit/lib/utils/locator_store_spec.rb +41 -6
  68. data/spec/unit/lib/utils/log_spec.rb +1 -1
  69. data/spec/unit/lib/utils/page_validator_spec.rb +149 -25
  70. data/spec/unit/lib/web_page_spec.rb +127 -53
  71. metadata +102 -142
  72. data/lib/howitzer/utils/capybara_patched.rb +0 -23
  73. data/lib/howitzer/utils/capybara_settings.rb +0 -247
  74. data/lib/howitzer/utils/email/email.rb +0 -85
  75. data/lib/howitzer/utils/email/mail_client.rb +0 -132
  76. data/lib/howitzer/utils/email/mailgun.rb +0 -175
  77. data/lib/howitzer/utils/email/mailgun_helper.rb +0 -61
  78. data/spec/unit/bin/howitzer_spec.rb +0 -175
  79. data/spec/unit/lib/utils/email/email_spec.rb +0 -75
  80. data/spec/unit/lib/utils/email/mail_client_spec.rb +0 -115
  81. data/spec/unit/lib/utils/email/mailgun_helper_spec.rb +0 -95
@@ -1,14 +1,3 @@
1
1
  require 'spec_helper'
2
- require "#{lib_path}/howitzer/utils/data_generator/gen.rb"
2
+ require 'howitzer/utils/data_generator/gen.rb'
3
3
  require 'capybara/dsl'
4
-
5
- describe "Helpers" do
6
- before do
7
- stub_const("Mailgun", double)
8
- expect(settings).to receive(:mailgun_api_key){ 'some_api' }
9
- end
10
- it "should init Mailgun and include modules" do
11
- expect(Mailgun).to receive(:init).with('some_api').once
12
- require "#{lib_path}/howitzer/init.rb"
13
- end
14
- end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ require 'howitzer/mailgun/client'
3
+
4
+ describe Mailgun::Client do
5
+ let(:mg_obj) { Mailgun::Client.new("Fake-API-Key") }
6
+ describe ".new" do
7
+ subject { mg_obj }
8
+ it { expect { subject }.not_to raise_error }
9
+ end
10
+
11
+ describe "#get" do
12
+ let(:query_string){ {'skip' => '10', 'limit' => '5'} }
13
+ subject { mg_obj.get("test.com/bounces", query_string) }
14
+ context "when simulation of client" do
15
+ before do
16
+ expect(RestClient::Resource).to receive(:new).once { Mailgun::UnitClient::new('Fake-API-Key', 'api.mailgun.net', 'v2') }
17
+ end
18
+ it do
19
+ expect(subject.body).to include("total_count")
20
+ expect(subject.body).to include("items")
21
+ end
22
+ end
23
+ context "when real client" do
24
+ let(:resource) { double }
25
+ before do
26
+ allow(resource).to receive('[]'){ resource }
27
+ allow(resource).to receive(:get).and_raise(StandardError, '401 Unauthorized: Forbidden')
28
+ allow(RestClient::Resource).to receive(:new) { resource }
29
+ end
30
+ it do
31
+ expect(log).to receive(:error).with(Howitzer::CommunicationError, '401 Unauthorized: Forbidden').once.and_call_original
32
+ expect { subject }.to raise_error(Howitzer::CommunicationError)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,70 @@
1
+ require 'spec_helper'
2
+ require 'howitzer/mailgun/connector'
3
+
4
+ describe Mailgun::Connector do
5
+ let(:connector) { Mailgun::Connector.instance }
6
+ let(:domain_name) { 'test@domain.com' }
7
+ describe "#client" do
8
+ subject { connector.client }
9
+ context "when api_key is default" do
10
+ context "when client is not initialized" do
11
+ it { expect(subject).to be_an_instance_of Mailgun::Client }
12
+ end
13
+ context "when client is already initialized" do
14
+ it do
15
+ object_id = connector.client.object_id
16
+ expect(subject.object_id).to eq(object_id)
17
+ end
18
+ end
19
+ end
20
+ context "when api_key is custom" do
21
+ let(:key) { "some api key" }
22
+ subject { connector.client(key) }
23
+ it { expect(subject).to be_an_instance_of Mailgun::Client }
24
+ end
25
+ context "when api_key is nil" do
26
+ let(:key) { nil }
27
+ subject { connector.client(key) }
28
+ it do
29
+ expect(log).to receive(:error).with(Howitzer::InvalidApiKeyError, "Api key can not be blank").once.and_call_original
30
+ expect { subject }.to raise_error(Howitzer::InvalidApiKeyError)
31
+ end
32
+ end
33
+ context "when api_key is blank string" do
34
+ let(:key) { "" }
35
+ subject { connector.client(key) }
36
+ it do
37
+ expect(log).to receive(:error).with(Howitzer::InvalidApiKeyError, "Api key can not be blank").once.and_call_original
38
+ expect { subject }.to raise_error(Howitzer::InvalidApiKeyError)
39
+ end
40
+ end
41
+ end
42
+ describe '#domain' do
43
+ subject { connector.domain }
44
+ context "when domain is not set" do
45
+ before { connector.instance_variable_set(:@domain, nil)}
46
+ it do
47
+ expect(connector).to receive(:change_domain).once{ domain_name }
48
+ expect(subject).to eq(domain_name)
49
+ end
50
+ end
51
+ context "when domain is already set" do
52
+ before do
53
+ expect(connector).to receive(:change_domain).never
54
+ connector.instance_variable_set(:@domain, domain_name)
55
+ end
56
+ it { expect(subject).to eql(domain_name) }
57
+ end
58
+ end
59
+
60
+ describe '#change_domain' do
61
+ context "when default" do
62
+ before { connector.change_domain }
63
+ it { expect(connector.instance_variable_get(:@domain)).to eq(settings.mailgun_domain)}
64
+ end
65
+ context "when custom" do
66
+ before { connector.change_domain(domain_name) }
67
+ it { expect(connector.instance_variable_get(:@domain)).to eq(domain_name) }
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'howitzer/mailgun/client'
3
+ require 'howitzer/exceptions'
4
+
5
+ describe Mailgun::Response do
6
+ let(:body) { {foo: 'bar'}.to_json }
7
+ let(:response) { double(:response, body: body, code: 201)}
8
+ describe "#body" do
9
+ subject { Mailgun::Response.new(response).body }
10
+ it { expect(subject).to eq("{\"foo\":\"bar\"}")}
11
+ end
12
+ describe "#code" do
13
+ subject { Mailgun::Response.new(response).code }
14
+ it { expect(subject).to eq(201)}
15
+ end
16
+ describe "#to_h" do
17
+ subject { Mailgun::Response.new(response).to_h }
18
+ context "when possible parse body" do
19
+ it { expect(subject).to eq({"foo"=>"bar"})}
20
+ end
21
+ context "when impossible parse body" do
22
+ let(:body) { '123' }
23
+ it do
24
+ expect(log).to receive(:error).with(Howitzer::ParseError, "757: unexpected token at '123'").once.and_call_original
25
+ expect { subject }.to raise_error(Howitzer::ParseError)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -1,8 +1,9 @@
1
1
  require 'spec_helper'
2
- require "#{lib_path}/howitzer/utils/data_generator/data_storage"
2
+ require 'howitzer/utils/data_generator/data_storage'
3
3
 
4
4
  describe "DataGenerator" do
5
5
  describe "DataStorage" do
6
+ before { DataGenerator::DataStorage.data.clear }
6
7
  describe ".store" do
7
8
  subject { DataGenerator::DataStorage.store(ns, 7, :halt) }
8
9
  context "when namespace specified" do
@@ -11,8 +12,8 @@ describe "DataGenerator" do
11
12
  expect(subject).to eql(:halt)
12
13
  end
13
14
  it "should store namespace value" do
14
- adata = DataGenerator::DataStorage.instance_variable_get(:@data)
15
- expect(adata[:user]).to eql({7 => :halt})
15
+ subject
16
+ expect(DataGenerator::DataStorage.data[:user]).to eql({7 => :halt})
16
17
  end
17
18
  end
18
19
  context "when namespace empty" do
@@ -22,7 +23,7 @@ describe "DataGenerator" do
22
23
  end
23
24
  describe ".extract" do
24
25
  subject { DataGenerator::DataStorage.extract(ns, key) }
25
- before { DataGenerator::DataStorage.instance_variable_set(:@data, {user: {7 => :exit}}) }
26
+ before { DataGenerator::DataStorage.data[:user] = {7 => :exit} }
26
27
  describe "when namespace specified" do
27
28
  let(:ns) { :user }
28
29
  context "and namespace key found" do
@@ -51,12 +52,29 @@ describe "DataGenerator" do
51
52
  end
52
53
  describe ".clear_ns" do
53
54
  subject { DataGenerator::DataStorage.clear_ns(:user) }
54
- before { DataGenerator::DataStorage.instance_variable_set(:@data, {user: {7 => :exit}}) }
55
+ before { DataGenerator::DataStorage.data[:user]= {7 => :exit}}
55
56
  it "should return empty hash" do
56
57
  subject
57
58
  adata = DataGenerator::DataStorage.instance_variable_get(:@data)
58
59
  expect(adata[:user]).to eql({})
59
60
  end
60
61
  end
62
+ describe ".clear_all_ns" do
63
+ before do
64
+ DataGenerator::DataStorage.store('sauce', :status, false)
65
+ DataGenerator::DataStorage.store(:foo, "foo", "some value1")
66
+ DataGenerator::DataStorage.store(:bar, "bar", "some value2")
67
+ DataGenerator::DataStorage.store(:baz, "baz", "some value3")
68
+ end
69
+ context "when default argument" do
70
+ before { DataGenerator::DataStorage.clear_all_ns }
71
+ it { expect(DataGenerator::DataStorage.data).to eq({"sauce"=>{:status=>false}, :foo=>{}, :bar=>{}, :baz=>{}}) }
72
+ end
73
+ context "when custom argument" do
74
+ let(:exception_list) { [:foo, :bar] }
75
+ before { DataGenerator::DataStorage.clear_all_ns(exception_list) }
76
+ it { expect(DataGenerator::DataStorage.data).to eq({"sauce"=>{}, :foo=>{"foo"=>"some value1"}, :bar=>{"bar"=>"some value2"}, :baz=>{}}) }
77
+ end
78
+ end
61
79
  end
62
80
  end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require "#{lib_path}/howitzer/utils/data_generator/gen"
2
+ require 'howitzer/utils/data_generator/gen'
3
3
 
4
4
  describe "DataGenerator" do
5
5
  describe "Gen" do
@@ -7,7 +7,7 @@ describe "DataGenerator" do
7
7
  subject { DataGenerator::Gen.user(params) }
8
8
  before do
9
9
  allow(settings).to receive(:def_test_pass) { 'test_pass' }
10
- allow(settings).to receive(:mail_pop3_domain) { 'mail.com' }
10
+ allow(settings).to receive(:mailgun_domain) { 'mail.com' }
11
11
  allow(DataGenerator::Gen).to receive(:serial) { '012345678abcde' }
12
12
  end
13
13
  context "when params specified" do
@@ -23,7 +23,6 @@ describe "DataGenerator" do
23
23
  it { expect(subject.password).to eql 'pa$$w0rd' }
24
24
  it { expect(subject.first_name).to eql 'FirstName012345678abcde' }
25
25
  it { expect(subject.last_name).to eql 'LastName012345678abcde' }
26
- it { expect(subject.mailbox).to eql 'member@test.com' }
27
26
  end
28
27
  context "with empty params" do
29
28
  let(:params) { {} }
@@ -38,7 +37,6 @@ describe "DataGenerator" do
38
37
  it { expect(subject.password).to eql 'test_pass' }
39
38
  it { expect(subject.first_name).to eql 'FirstName012345678abcde' }
40
39
  it { expect(subject.last_name).to eql 'LastName012345678abcde' }
41
- it { expect(subject.mailbox).to be_nil }
42
40
  end
43
41
  end
44
42
  describe ".given_user_by_number" do
@@ -69,20 +67,6 @@ describe "DataGenerator" do
69
67
  it { expect(subject).to_not eql ser }
70
68
  end
71
69
  end
72
- describe ".delete_all_mailboxes" do
73
- subject { DataGenerator::Gen.delete_all_mailboxes }
74
- let(:mbox1) { double }
75
- let(:mbox2) { double }
76
- before do
77
- stub_const("DataGenerator::DataStorage", double)
78
- expect(DataGenerator::DataStorage).to receive(:extract).with('user') { { 1 => mbox1, 2 => mbox2 } }
79
- end
80
- it do
81
- expect(mbox1).to receive(:delete_mailbox).once
82
- expect(mbox2).to receive(:delete_mailbox).once
83
- subject
84
- end
85
- end
86
70
  describe "User" do
87
71
  describe "#initialize" do
88
72
  subject { DataGenerator::Gen::User.new(params) }
@@ -100,51 +84,6 @@ describe "DataGenerator" do
100
84
  it { expect(subject.first_name).to eql 'Alexey' }
101
85
  it { expect(subject.last_name).to eql 'Petrenko' }
102
86
  it { expect(subject.full_name).to eql 'Alexey Petrenko' }
103
- it { expect(subject.mailbox).to eql 'member@test.com' }
104
- end
105
- describe "#create_mailbox" do
106
- subject { nu_user.create_mailbox }
107
- let(:nu_user) { DataGenerator::Gen::User.new({ email: 'alex.petrenko@mail.com', mailbox: 'member@test.com' }) }
108
- before { stub_const('MailClient', double) }
109
- context "should return User object" do
110
- before do
111
- allow(settings).to receive(:mail_pop3_domain) { 'mail.com' }
112
- expect(MailClient).to receive(:create_mailbox).with('alex.petrenko').once { 'petrenko@test.com' }
113
- end
114
- it { expect(subject).to be_an_instance_of DataGenerator::Gen::User }
115
- end
116
- context "when mail_pop3_domain settings are equal to @domain" do
117
- before do
118
- allow(settings).to receive(:mail_pop3_domain) { 'mail.com' }
119
- expect(MailClient).to receive(:create_mailbox).with('alex.petrenko').once { 'petrenko@test.com' }
120
- end
121
- it do
122
- subject
123
- expect(nu_user.mailbox).to eql 'petrenko@test.com'
124
- end
125
- end
126
- context "when mail_pop3_domain settings are not equal to @domain" do
127
- before { allow(settings).to receive(:mail_pop3_domain) { 'post.com' } }
128
- it do
129
- subject
130
- expect(nu_user.mailbox).to eql 'member@test.com'
131
- end
132
- end
133
- end
134
- describe "#delete_mailbox" do
135
- subject { nu_user.delete_mailbox }
136
- let(:nu_user) { DataGenerator::Gen::User.new({ mailbox: mbox }) }
137
- before { stub_const('MailClient', double) }
138
- context "when mailbox spcified" do
139
- before { expect(MailClient).to receive(:delete_mailbox).with(mbox).once { 'mailbox deleted' } }
140
- let(:mbox) { 'member@test.com' }
141
- it { expect(subject).to eql 'mailbox deleted' }
142
- end
143
- context "when mailbox not spcified" do
144
- before { expect(MailClient).to_not receive(:delete_mailbox).with(mbox) }
145
- let(:mbox) { nil }
146
- it { expect(subject).to be_nil }
147
- end
148
87
  end
149
88
  end
150
89
  end
@@ -1,15 +1,18 @@
1
1
  require 'spec_helper'
2
- require "#{lib_path}/howitzer/utils/locator_store"
2
+ require 'howitzer/utils/locator_store'
3
3
 
4
4
  describe "Locator store" do
5
5
  let(:bad_name) { 'name' }
6
- let(:error) { LocatorStore::LocatorNotDefinedError }
6
+ let(:error) { Howitzer::LocatorNotDefinedError }
7
7
 
8
8
  shared_examples "locator methods" do |prefix, web_page|
9
9
  describe "#{prefix}locator" do
10
10
  context "when bad locator given" do
11
11
  subject { web_page.locator(bad_name) }
12
- it { expect {subject}.to raise_error(error) }
12
+ it do
13
+ expect(log).to receive(:error).with(error, bad_name).once.and_call_original
14
+ expect { subject }.to raise_error(error)
15
+ end
13
16
  end
14
17
  context "when CSS locator given" do
15
18
  before { web_page.add_locator :base_locator, 'base_locator' }
@@ -90,17 +93,49 @@ describe "Locator store" do
90
93
  end
91
94
  context "when not existing locator name" do
92
95
  subject { web_page.find_element(:unknown_locator) }
93
- it { expect{ subject }.to raise_error(LocatorStore::LocatorNotDefinedError, "unknown_locator") }
96
+ it { expect{ subject }.to raise_error(Howitzer::LocatorNotDefinedError, "unknown_locator") }
97
+ end
98
+ end
99
+ describe "#{prefix}first_element" do
100
+ context "when existing locator name given" do
101
+ context "of base type" do
102
+ before { web_page.add_locator :test_locator, '.foo' }
103
+ subject { web_page.first_element(name) }
104
+ context "as string" do
105
+ let(:name) { "test_locator" }
106
+ it do
107
+ expect(web_page.is_a?(Class) ? web_page : web_page.class).to receive(:first).with('.foo')
108
+ subject
109
+ end
110
+ end
111
+ context "as symbol" do
112
+ let(:name) { :test_locator }
113
+ it do
114
+ expect(web_page.is_a?(Class) ? web_page : web_page.class).to receive(:first).with('.foo')
115
+ subject
116
+ end
117
+ end
118
+ end
119
+ context "when link locator or other" do
120
+ before { web_page.add_link_locator :test_link_locator, 'foo' }
121
+ subject { web_page.first_element(:test_link_locator) }
122
+ it do
123
+ expect(web_page.is_a?(Class) ? web_page : web_page.class).to receive(:first).with(:link, 'foo')
124
+ subject
125
+ end
126
+ end
127
+ end
128
+ context "when not existing locator name" do
129
+ subject { web_page.first_element(:unknown_locator) }
130
+ it { expect{ subject }.to raise_error(Howitzer::LocatorNotDefinedError, "unknown_locator") }
94
131
  end
95
132
  end
96
-
97
133
  describe "#{prefix}apply" do
98
134
  context "when bad locator given" do
99
135
  before { web_page.add_locator :test_locator, lambda{|test| test} }
100
136
  let(:locator) { lambda{|test| test} }
101
137
  subject { web_page.apply(locator, 'test') }
102
138
  it { expect {subject}.to raise_error(NoMethodError) }
103
-
104
139
  end
105
140
  context "when correct locator given" do
106
141
  before { web_page.add_locator :test_locator, lambda{|location_name| {xpath: ".//a[contains(.,'#{location_name}')]"}} }
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require "#{lib_path}/howitzer/utils/log"
2
+ require 'howitzer/utils/log'
3
3
  include LoggerHelper
4
4
 
5
5
  describe "Logger" do
@@ -1,5 +1,6 @@
1
1
  require 'spec_helper'
2
- require "#{lib_path}/howitzer/utils/page_validator"
2
+ require "howitzer/utils/page_validator"
3
+ require "howitzer/utils/locator_store"
3
4
 
4
5
  describe Howitzer::Utils::PageValidator do
5
6
  describe ".validations" do
@@ -8,29 +9,57 @@ describe Howitzer::Utils::PageValidator do
8
9
  end
9
10
 
10
11
  describe "PageValidator" do
11
- let(:web_page) { Class.new { include Howitzer::Utils::PageValidator }.new }
12
- describe "#check_correct_page_loaded" do
13
- subject { web_page.check_correct_page_loaded }
12
+ let(:web_page_class) do
13
+ Class.new do
14
+ include LocatorStore
15
+ include Howitzer::Utils::PageValidator
16
+ def self.name
17
+ 'TestWebPageClass'
18
+ end
19
+ end
20
+ end
21
+ let(:web_page) { web_page_class.new }
22
+ describe "#check_validations_are_defined!" do
23
+ subject { web_page.check_validations_are_defined! }
14
24
  context "when no validation specified" do
15
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::NoValidationError, "No any page validation was found") }
25
+ it do
26
+ expect(log).to receive(:error).with(Howitzer::NoValidationError, "No any page validation was found for 'TestWebPageClass' page").once.and_call_original
27
+ expect { subject }.to raise_error(Howitzer::NoValidationError)
28
+ end
29
+ end
30
+ context "when old validation style is using" do
31
+ before { web_page_class.const_set("URL_PATTERN",/Foo/) }
32
+ after { web_page_class.send :remove_const, "URL_PATTERN"}
33
+ it do
34
+ expect(web_page_class).to receive(:validates).with(:url, pattern: /Foo/).and_return{ Howitzer::Utils::PageValidator.validations['TestWebPageClass'] = {}}
35
+ expect{subject}.to_not raise_error
36
+ end
16
37
  end
17
- context "when all validation are specified" do
38
+ context "when title validation is specified" do
18
39
  before do
19
40
  web_page.class.validates :title, pattern: /Foo/
41
+ end
42
+ it { expect{subject}.to_not raise_error }
43
+ end
44
+ context "when url validation is specified" do
45
+ before do
20
46
  web_page.class.validates :url, pattern: /Foo/
21
- web_page.class.validates :element_presence, locator: :test_locator
22
47
  end
23
- it do
24
- expect(web_page).to receive(:wait_for_url).with(/Foo/).once
25
- expect(web_page).to receive(:wait_for_title).with(/Foo/).once
26
- expect(web_page).to receive(:find_element).with(:test_locator)
27
- subject
48
+ it { expect{subject}.to_not raise_error }
49
+ end
50
+ context "when element_presence validation is specified" do
51
+ before do
52
+ web_page.class.validates :element_presence, locator: :test_locator
28
53
  end
54
+ it { expect{subject}.to_not raise_error }
29
55
  end
30
56
  end
31
57
 
32
58
  describe ".validates" do
33
- subject { web_page.class.validates(name, options)}
59
+ before do
60
+ Howitzer::Utils::PageValidator.validations[web_page.class.name] = nil
61
+ end
62
+ subject { web_page.class.validates(name, options) }
34
63
  context "when name = :url" do
35
64
  context "as string" do
36
65
  let(:name) { "url" }
@@ -39,25 +68,31 @@ describe "PageValidator" do
39
68
  let(:options) { {"pattern" => /foo/} }
40
69
  it do
41
70
  expect(subject).to be_a(Proc)
42
- expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:url]).to eql(subject)
71
+ expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:url]).to be_a Proc
43
72
  end
44
73
  end
45
74
  context "(as symbol)" do
46
75
  let(:options) { {pattern: /foo/} }
47
76
  it do
48
77
  expect(subject).to be_a(Proc)
49
- expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:url]).to eql(subject)
78
+ expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:url]).to be_a Proc
50
79
  end
51
80
  end
52
81
  end
53
82
  context "when options is incorrect" do
54
83
  context "(missing pattern)" do
55
84
  let(:options) { {} }
56
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::WrongOptionError, "Please specify ':pattern' option as Regexp object") }
85
+ it do
86
+ expect(log).to receive(:error).with(Howitzer::WrongOptionError, "Please specify ':pattern' option as Regexp object").once.and_call_original
87
+ expect { subject }.to raise_error(Howitzer::WrongOptionError)
88
+ end
57
89
  end
58
90
  context "(string pattern)" do
59
91
  let(:options) { {pattern: "foo"} }
60
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::WrongOptionError, "Please specify ':pattern' option as Regexp object") }
92
+ it do
93
+ expect(log).to receive(:error).with(Howitzer::WrongOptionError, "Please specify ':pattern' option as Regexp object").once.and_call_original
94
+ expect { subject }.to raise_error(Howitzer::WrongOptionError)
95
+ end
61
96
  end
62
97
  context "(not hash)" do
63
98
  let(:options) { "foo" }
@@ -70,7 +105,7 @@ describe "PageValidator" do
70
105
  let(:options) { {pattern: /foo/} }
71
106
  it do
72
107
  expect(subject).to be_a(Proc)
73
- expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:url]).to eql(subject)
108
+ expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:url]).to be_a Proc
74
109
  end
75
110
  end
76
111
  end
@@ -95,11 +130,17 @@ describe "PageValidator" do
95
130
  context "when options is incorrect" do
96
131
  context "(missing locator)" do
97
132
  let(:options) { {} }
98
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::WrongOptionError, "Please specify ':locator' option as one of page locator names") }
133
+ it do
134
+ expect(log).to receive(:error).with(Howitzer::WrongOptionError, "Please specify ':locator' option as one of page locator names").once.and_call_original
135
+ expect { subject }.to raise_error(Howitzer::WrongOptionError)
136
+ end
99
137
  end
100
138
  context "(blank locator name)" do
101
139
  let(:options) { {locator: ""} }
102
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::WrongOptionError, "Please specify ':locator' option as one of page locator names") }
140
+ it do
141
+ expect(log).to receive(:error).with(Howitzer::WrongOptionError, "Please specify ':locator' option as one of page locator names").once.and_call_original
142
+ expect { subject }.to raise_error(Howitzer::WrongOptionError)
143
+ end
103
144
  end
104
145
  end
105
146
  end
@@ -110,33 +151,116 @@ describe "PageValidator" do
110
151
  let(:options) { {"pattern" => /foo/} }
111
152
  it do
112
153
  expect(subject).to be_a(Proc)
113
- expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:title]).to eql(subject)
154
+ expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:title]).to be_a Proc
114
155
  end
115
156
  end
116
157
  context "(as symbol)" do
117
158
  let(:options) { {pattern: /foo/} }
118
159
  it do
119
160
  expect(subject).to be_a(Proc)
120
- expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:title]).to eql(subject)
161
+ expect(Howitzer::Utils::PageValidator.validations[web_page.class.name][:title]).to be_a Proc
121
162
  end
122
163
  end
123
164
  end
124
165
  context "when options is incorrect" do
125
166
  context "(missing pattern)" do
126
167
  let(:options) { {} }
127
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::WrongOptionError, "Please specify ':pattern' option as Regexp object") }
168
+ it do
169
+ expect(log).to receive(:error).with(Howitzer::WrongOptionError, "Please specify ':pattern' option as Regexp object").once.and_call_original
170
+ expect { subject }.to raise_error(Howitzer::WrongOptionError)
171
+ end
128
172
  end
129
173
  context "(string pattern)" do
130
174
  let(:options) { {pattern: "foo"} }
131
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::WrongOptionError, "Please specify ':pattern' option as Regexp object") }
175
+ it do
176
+ expect(log).to receive(:error).with(Howitzer::WrongOptionError, "Please specify ':pattern' option as Regexp object").once.and_call_original
177
+ expect { subject }.to raise_error(Howitzer::WrongOptionError)
178
+ end
132
179
  end
133
180
  end
134
181
  end
135
182
  context "when other name" do
136
183
  let(:name) { :unknown }
137
184
  let(:options) { {} }
138
- it { expect{subject}.to raise_error(Howitzer::Utils::PageValidator::UnknownValidationName, "unknown 'unknown' validation name") }
185
+ it do
186
+ expect(log).to receive(:error).with(Howitzer::UnknownValidationError, "unknown 'unknown' validation name").once.and_call_original
187
+ expect { subject }.to raise_error(Howitzer::UnknownValidationError)
188
+ end
189
+ end
190
+ end
191
+
192
+ describe ".pages" do
193
+ subject { Howitzer::Utils::PageValidator.pages }
194
+ it { expect(subject).to eq([]) }
195
+ it do
196
+ subject << Class
197
+ expect(subject).to eql([Class])
198
+ end
199
+ end
200
+
201
+ describe ".opened?" do
202
+ subject { web_page_class.opened? }
203
+ context "when no one validation is defined" do
204
+ it do
205
+ expect(log).to receive(:error).with(Howitzer::NoValidationError, "No any page validation was found for 'TestWebPageClass' page").once.and_call_original
206
+ expect { subject }.to raise_error(Howitzer::NoValidationError)
207
+ end
208
+ end
209
+ context "when all validations are defined" do
210
+ before do
211
+ web_page_class.class_eval do
212
+ add_locator :login, "#id"
213
+ validates :url, pattern: /foo/
214
+ validates :title, pattern: /Foo page/
215
+ validates :element_presence, locator: :login
216
+ end
217
+ end
218
+ context "when all matches" do
219
+ before do
220
+ allow(web_page_class).to receive(:url){ 'http://test.com/foo' }
221
+ allow(web_page_class).to receive(:title){ 'Foo page' }
222
+ allow(web_page_class).to receive(:first_element).with(:login){ true }
223
+ end
224
+ it { expect(subject).to be_true }
225
+ end
226
+ context "when first does not match" do
227
+ before do
228
+ expect(web_page_class).to receive(:url).once{ 'http://test.com/bar' }
229
+ expect(web_page_class).to receive(:title).never
230
+ expect(web_page_class).to receive(:first_element).never
231
+ end
232
+ it { expect(subject).to be_false }
233
+ end
234
+ end
235
+ end
236
+
237
+ describe "#matched_pages" do
238
+ let!(:web_page1_class) do
239
+ Class.new do
240
+ include Howitzer::Utils::PageValidator
241
+ def self.name
242
+ 'TestWebPage1Class'
243
+ end
244
+ def self.opened?
245
+ true
246
+ end
247
+ end
248
+ end
249
+
250
+ let!(:web_page2_class) do
251
+ Class.new do
252
+ include Howitzer::Utils::PageValidator
253
+ def self.name
254
+ 'TestWebPage2Class'
255
+ end
256
+ def self.opened?
257
+ false
258
+ end
259
+ end
139
260
  end
261
+ subject {web_page2_class.matched_pages }
262
+ before { Howitzer::Utils::PageValidator.instance_variable_set(:@pages, [web_page1_class, web_page2_class]) }
263
+ it { expect(subject).to eq([web_page1_class]) }
140
264
  end
141
265
 
142
266
  end