watir_api 0.2.0 → 0.2.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +137 -5
  3. data/lib/watir_api.rb +7 -4
  4. data/watir_api.gemspec +1 -1
  5. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2866494791af1c0785b52cb294240c8f0a2c16a9
4
- data.tar.gz: 0a7dd46130691e620357d03fcd66113372ee2ff2
3
+ metadata.gz: a5774f09a5ef7ee12f3a8726d05ddb0dd1da0ef6
4
+ data.tar.gz: 10f86cada217f8547693b7d5a257a02e74036ebb
5
5
  SHA512:
6
- metadata.gz: 9abe2898a5c711ec90c37889123516c985e51165670040576b747983503c742f3134a12b25ce5c37b6b6508ab5a18edb0f8969a7ac49977226855e65d5b7e23d
7
- data.tar.gz: f9a64cc277053f4c4530d978efa808bf2d22a34b15e777e9f466ea94156e909ac931edde90a2949980e070c5c0b8a7a7ae5dec79ca31c068fbba5bad397d059f
6
+ metadata.gz: a85d99f5328327fbd77d0e671d9524d6fc7fa04066d994674aa2cb6b6b7e7a8e7888203f5607f8484a65a744d36fb2c4931371464f0b7d6663f845b7f969fb16
7
+ data.tar.gz: 6e2ca0725ac604af8b222b49ec835eaeba33bb5607d85c16f0e679103ce47acc53e68d60f001542ee12d194db38a93928042b250c065a6abd44713d88d741a1e
data/README.md CHANGED
@@ -1,7 +1,13 @@
1
- # WatirApi
1
+ # Watir API
2
2
 
3
- Send and receive data via a Web App's API, ideally using WatirModel objects. The goal is to
4
- compare test data with what is input and displayed via UI.
3
+ As more web applications make use of an interface to interact with their service layer, people now have
4
+ more flexibility to set up and verify parts of their UI tests without needing to use a browser.
5
+
6
+ This simple gem makes it easy to subclass `WatirApi::Base` and provide all of the information necessary
7
+ to interact with the different REST endpoints available in your application.
8
+
9
+ This code is designed to be used with the [watir_model gem](https://github.com/titusfortner/watir_model). The Model stores the canonical data and is used
10
+ to make it easy to compare the input and output from both the API and the UI.
5
11
 
6
12
  ## Installation
7
13
 
@@ -21,8 +27,134 @@ Or install it yourself as:
21
27
 
22
28
  ## Usage
23
29
 
24
- This gem is still in beta; if you have suggestions to improve it, please write some code or
25
- raise an issue on Github
30
+ 1. Set the base url
31
+ ```ruby
32
+ WatirApi::Base.base_url = '"https://restful-booker.herokuapp.com"'
33
+ ```
34
+ 2. Create a subclass with an endpoint:
35
+ ```ruby
36
+ module API
37
+ class Booking < WatirApi::Base
38
+ def self.endpoint
39
+ 'booking'
40
+ end
41
+ end
42
+ end
43
+ ```
44
+
45
+ 3. Make API calls
46
+ ```ruby
47
+ booking = {firstname: "Trey",
48
+ lastname: "Ruecker",
49
+ totalprice: 83,
50
+ depositpaid: true,
51
+ bookingdates: {checkin: '2019-02-23',
52
+ checkout: '2019-02-27'}}
53
+
54
+ API::Booking.create(booking)
55
+ ```
56
+
57
+ 4. The Array or Hash of results is accessed with `#data`
58
+ ```ruby
59
+ booking = {firstname: "David",
60
+ lastname: "Jones",
61
+ totalprice: 183,
62
+ depositpaid: true,
63
+ bookingdates: {checkin: '2019-03-23',
64
+ checkout: '2019-03-27'}}
65
+
66
+ created_booking = API::Booking.create(booking)
67
+ booking_id = created_booking.data[:bookingid]
68
+
69
+ stored_booking = API::Booking.show(id: booking_id).data
70
+
71
+ expect(stored_booking).to eq booking
72
+ ```
73
+
74
+ 5. Use [Watir Model](https://github.com/titusfortner/watir_model)
75
+
76
+ Note that the code in the previous example will actually fail.
77
+ This is because we are storing dates as `String` values and the input `String`
78
+ does not match the output `String`
79
+
80
+ Hashes are hard to compare, which is why we have `WatirModel`.
81
+ WatirModel is designed to store the canonical representation of related data in the appropriate data type,
82
+ specifically so that data can be correctly compared.
83
+
84
+ ```ruby
85
+ module Model
86
+ class BookingDates < WatirModel
87
+ key(:checkin, data_type: Date) { Faker::Date.forward }
88
+ key(:checkout, data_type: Date) { checkin + 4 }
89
+ end
90
+
91
+ class Booking < WatirModel
92
+ key(:firstname) { Faker::Name.first_name }
93
+ key(:lastname) { Faker::Name.last_name }
94
+ key(:totalprice, data_type: Integer) { Faker::Commerce.price.round }
95
+ key(:depositpaid) { true }
96
+ key(:bookingdates, data_type: BookingDates) { BookingDates.new }
97
+ key(:additionalneeds)
98
+ end
99
+ end
100
+ ```
101
+
102
+ Because we have a model class defined that is named the same as the API class, `WatirApi` will
103
+ automatically attempt to create an instance of the model from the return value of the API call.
104
+ It is accessible from a method based on the name of the API/Model classes, so in this case `#booking`:
105
+
106
+ ```ruby
107
+ booking = Model::Booking.new
108
+
109
+ created_booking = API::Booking.create(booking)
110
+ booking_id = created_booking.data[:bookingid]
111
+
112
+ stored_booking = API::Booking.show(id: booking_id).booking
113
+
114
+ expect(stored_booking).to eq booking
115
+ ```
116
+
117
+ 6. Customize
118
+
119
+ You have a subclass, so if you need to add or change things before or after a call, just override the `WatirApi` method
120
+ in your subclass:
121
+
122
+ ```ruby
123
+ module API
124
+ class Booking < WatirApi::Base
125
+
126
+ attr_reader :id
127
+
128
+ def initialize(*)
129
+ super
130
+ return if @data.is_a?(Array)
131
+ @id = @data[:bookingid]
132
+ end
133
+ end
134
+ end
135
+ ```
136
+ Now we can use this like so:
137
+ ```ruby
138
+ booking = Model::Booking.new
139
+
140
+ created_booking = API::Booking.create(booking)
141
+
142
+ expect(created_booking.id).to eq created_booking.data[:bookingid]
143
+ ```
144
+
145
+ Because this pattern comes in very handy, you can use `#define_attribute` to do the same thing:
146
+
147
+ ```ruby
148
+ module API
149
+ class Booking < WatirApi::Base
150
+ def initialize(*)
151
+ super
152
+ return if @data.is_a?(Array)
153
+ define_attribute(:id, @data[:bookingid])
154
+ end
155
+ end
156
+ end
157
+ ```
26
158
 
27
159
  ## Contributing
28
160
 
@@ -98,10 +98,8 @@ module WatirApi
98
98
  model = convert_to_model(@data) unless @data.nil?
99
99
  var = model_object.to_s[/[^:]*$/].underscore
100
100
  var = var.pluralize if @data.is_a? Array
101
- instance_variable_set "@#{var}", model
102
- singleton_class.class_eval { attr_accessor var }
103
- @id = @data[:id] if @data.is_a? Hash
104
- singleton_class.class_eval { attr_accessor :id } unless @id.nil?
101
+ define_attribute(var, model)
102
+ define_attribute(:id, @data[:id]) if @data.is_a? Hash
105
103
  end
106
104
 
107
105
  def convert_to_model(data)
@@ -123,5 +121,10 @@ module WatirApi
123
121
  def model_object
124
122
  self.class.model_object
125
123
  end
124
+
125
+ def define_attribute(key, value)
126
+ instance_variable_set("@#{key}", value)
127
+ singleton_class.class_eval { attr_reader key }
128
+ end
126
129
  end
127
130
  end
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "watir_api"
6
- spec.version = "0.2.0"
6
+ spec.version = "0.2.1"
7
7
  spec.authors = ["Titus Fortner"]
8
8
  spec.email = ["titusfortner@gmail.com"]
9
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watir_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Titus Fortner