wist 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -10,7 +10,7 @@ GIT
10
10
  PATH
11
11
  remote: .
12
12
  specs:
13
- wist (0.0.3)
13
+ wist (0.0.4)
14
14
  capybara
15
15
 
16
16
  GEM
data/lib/wist.rb CHANGED
@@ -16,6 +16,13 @@ module Wist
16
16
  find(selector).click
17
17
  end
18
18
 
19
+ def set_cookie(k, v)
20
+ page.execute_script <<-eos
21
+ (function (e,t,n){var r=new Date;r.setDate(r.getDate()+n);var i=escape(t)+(n==null?"":"; expires="+r.toUTCString());document.cookie=e+"="+i}
22
+ (#{k.to_json}, #{v.to_json}));
23
+ eos
24
+ end
25
+
19
26
  def set_value_and_trigger_evts(selector, val)
20
27
  find(selector).set(val)
21
28
  page.execute_script("$('#{selector}').focusout().change().trigger('input')")
@@ -58,8 +65,13 @@ module Wist
58
65
  get_js "#{jquery_selector selector}.val()"
59
66
  end
60
67
 
61
- def has_class(selector, class_name)
62
- get_js "#{jquery_selector selector}.hasClass('#{class_name}')"
68
+ def set_input_and_press_enter(selector, val)
69
+ wait_til_element_visible(selector)
70
+ page.execute_script("#{jquery_selector(selector)}.val('#{val}').trigger({type: 'keydown', which: 13})")
71
+ end
72
+
73
+ def has_class?(el, class_name)
74
+ el[:class].split(' ').include?(class_name)
63
75
  end
64
76
 
65
77
  def wait_for_new_url(element_to_click)
@@ -0,0 +1,94 @@
1
+ /*!
2
+ * jQuery Cookie Plugin v1.3.1
3
+ * https://github.com/carhartl/jquery-cookie
4
+ *
5
+ * Copyright 2013 Klaus Hartl
6
+ * Released under the MIT license
7
+ */
8
+ (function (factory) {
9
+ if (typeof define === 'function' && define.amd) {
10
+ // AMD. Register as anonymous module.
11
+ define(['jquery'], factory);
12
+ } else {
13
+ // Browser globals.
14
+ factory(jQuery);
15
+ }
16
+ }(function ($) {
17
+
18
+ var pluses = /\+/g;
19
+
20
+ function raw(s) {
21
+ return s;
22
+ }
23
+
24
+ function decoded(s) {
25
+ return decodeURIComponent(s.replace(pluses, ' '));
26
+ }
27
+
28
+ function converted(s) {
29
+ if (s.indexOf('"') === 0) {
30
+ // This is a quoted cookie as according to RFC2068, unescape
31
+ s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
32
+ }
33
+ try {
34
+ return config.json ? JSON.parse(s) : s;
35
+ } catch(er) {}
36
+ }
37
+
38
+ var config = $.cookie = function (key, value, options) {
39
+
40
+ // write
41
+ if (value !== undefined) {
42
+ options = $.extend({}, config.defaults, options);
43
+
44
+ if (typeof options.expires === 'number') {
45
+ var days = options.expires, t = options.expires = new Date();
46
+ t.setDate(t.getDate() + days);
47
+ }
48
+
49
+ value = config.json ? JSON.stringify(value) : String(value);
50
+
51
+ return (document.cookie = [
52
+ config.raw ? key : encodeURIComponent(key),
53
+ '=',
54
+ config.raw ? value : encodeURIComponent(value),
55
+ options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
56
+ options.path ? '; path=' + options.path : '',
57
+ options.domain ? '; domain=' + options.domain : '',
58
+ options.secure ? '; secure' : ''
59
+ ].join(''));
60
+ }
61
+
62
+ // read
63
+ var decode = config.raw ? raw : decoded;
64
+ var cookies = document.cookie.split('; ');
65
+ var result = key ? undefined : {};
66
+ for (var i = 0, l = cookies.length; i < l; i++) {
67
+ var parts = cookies[i].split('=');
68
+ var name = decode(parts.shift());
69
+ var cookie = decode(parts.join('='));
70
+
71
+ if (key && key === name) {
72
+ result = converted(cookie);
73
+ break;
74
+ }
75
+
76
+ if (!key) {
77
+ result[name] = converted(cookie);
78
+ }
79
+ }
80
+
81
+ return result;
82
+ };
83
+
84
+ config.defaults = {};
85
+
86
+ $.removeCookie = function (key, options) {
87
+ if ($.cookie(key) !== undefined) {
88
+ $.cookie(key, '', $.extend(options, { expires: -1 }));
89
+ return true;
90
+ }
91
+ return false;
92
+ };
93
+
94
+ }));
data/test/test.js CHANGED
@@ -29,4 +29,8 @@ $('#test_wait_til_element_visible').click(function() {
29
29
 
30
30
  $('#test_set_value_and_trigger_evts').change(function () {
31
31
  $(this).val('changed');
32
- });
32
+ });
33
+
34
+ $('#test_set_input_and_press_enter').keydown(function (e) {
35
+ if (e.which === 13) alert('enter pressed');
36
+ });
data/test/test0.html CHANGED
@@ -21,7 +21,10 @@
21
21
 
22
22
  <input id='test_set_value_and_trigger_evts' type='text' />
23
23
 
24
+ <input id='test_set_input_and_press_enter' type='text' />
25
+
24
26
  <script src="jquery.js"></script>
27
+ <script src="jquery.cookie.js"></script>
25
28
  <script src="test.js"></script>
26
29
  </body>
27
30
  </html>
@@ -1,12 +1,11 @@
1
1
  require 'test_helper'
2
2
 
3
- class TestWist < CapybaraTestCase
3
+ class WistTest < CapybaraTestCase
4
4
  def setup
5
5
  super
6
6
  visit "file://#{Dir.pwd}/test/test0.html"
7
7
  end
8
8
 
9
- # helpers
10
9
  def verify_test_page(num)
11
10
  assert current_path.match(/test#{num}.html$/)
12
11
  end
@@ -51,8 +50,9 @@ class TestWist < CapybaraTestCase
51
50
  end
52
51
 
53
52
  def test_has_class
54
- %w(foo bar).each { |c| assert has_class('#class_test', c) }
55
- assert_equal false, has_class('#class_test', 'baz')
53
+ el = find('#class_test')
54
+ %w(foo bar).each { |c| assert_equal(true, has_class?(el, c)) }
55
+ assert_equal(false, has_class?(el, 'baz'))
56
56
  end
57
57
 
58
58
  def test_alert_accept
@@ -91,4 +91,16 @@ class TestWist < CapybaraTestCase
91
91
  assert_equal('changed', get_val('#test_set_value_and_trigger_evts'))
92
92
  end
93
93
 
94
+ def test_set_cookie
95
+ return if Capybara.javascript_driver == :chrome
96
+ set_cookie('foo', 'bar')
97
+ assert_equal('bar', page.evaluate_script("$.cookie('foo')"))
98
+ end
99
+
100
+ def test_set_input_and_press_enter
101
+ alert_accept('enter pressed') do
102
+ set_input_and_press_enter('#test_set_input_and_press_enter', 'bar')
103
+ end
104
+ assert_equal('bar', get_val('#test_set_input_and_press_enter'))
105
+ end
94
106
  end
data/wist.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = 'wist'
3
- gem.version = '0.0.4'
3
+ gem.version = '0.0.5'
4
4
  gem.summary = 'capybara helpers'
5
5
  gem.author = 'Lihan Li'
6
6
  gem.email = 'frankieteardrop@gmail.com'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -40,6 +40,7 @@ files:
40
40
  - README.md
41
41
  - Rakefile
42
42
  - lib/wist.rb
43
+ - test/jquery.cookie.js
43
44
  - test/jquery.js
44
45
  - test/test.js
45
46
  - test/test0.html
@@ -72,6 +73,7 @@ signing_key:
72
73
  specification_version: 3
73
74
  summary: capybara helpers
74
75
  test_files:
76
+ - test/jquery.cookie.js
75
77
  - test/jquery.js
76
78
  - test/test.js
77
79
  - test/test0.html