casper 0.0.4 → 0.0.5

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 (3) hide show
  1. data/lib/casper.rb +45 -15
  2. data/test/casper_test.rb +29 -1
  3. metadata +3 -3
@@ -2,7 +2,25 @@ require "libxdo"
2
2
 
3
3
  module Casper
4
4
  class Mouse
5
+ @@delay = 0
6
+
5
7
  class << self
8
+ # Sets a time (in seconds, pass floats for fractions of a second) to
9
+ # delay after every operation. Useful for situations where the commands
10
+ # are going to a window and want to give the window a little time to
11
+ # react before sending more commands (i.e. letting the browser fire
12
+ # events).
13
+ #
14
+ # Defaults to 0 (no delay).
15
+ def delay=(delay)
16
+ @@delay = delay
17
+ end
18
+
19
+ # Returns the current delay time.
20
+ def delay
21
+ @@delay
22
+ end
23
+
6
24
  # Move the mouse directly to the specified x/y coordinates.
7
25
  #
8
26
  # If relative = true the x/y coordinates used are relative from the
@@ -59,16 +77,26 @@ module Casper
59
77
  #
60
78
  # i.e.
61
79
  #
62
- # drag :to => [ 300, 350 ]
63
- # drag :from => [ 200, 300 ], :to => [ 400, 800 ]
64
- # drag :from => [ 200, 300 ], :distance => [ 200, 500 ]
65
- # drag :from => [ 200, 300 ], :distance => [ 220, 340 ], :increments => 20
66
- # drag :from => [ 200, 300 ], :to => [ 300, 400 ] do
67
- # sleep 0.5
68
- # end
69
- # drag :distance => [ 20, 0 ] do
70
- # drag :distance => [ 0, 30 ]
71
- # end
80
+ # drag :to => [ 300, 350 ]
81
+ # drag :from => [ 200, 300 ], :to => [ 400, 800 ]
82
+ # drag :from => [ 200, 300 ], :distance => [ 200, 500 ]
83
+ # drag :from => [ 200, 300 ], :distance => [ 220, 340 ], :increments => 20
84
+ # drag :from => [ 200, 300 ], :to => [ 300, 400 ] do
85
+ # sleep 0.5
86
+ # end
87
+ # drag :distance => [ 20, 0 ] do
88
+ # drag :distance => [ 0, 30 ]
89
+ # end
90
+ #
91
+ # NOTE: that if :increments is not evenly divisble by the total distance
92
+ # the mouse will move in either direction then the distance of each
93
+ # iteration will not be consistent. The iterations are individually
94
+ # rounded to try to provide a smooth movement. For instance, take:
95
+ #
96
+ # drag :from => [ 0, 0 ], :distance => [ 20, 20 ], :increments => 8
97
+ #
98
+ # will still occur in 8 iterations, however each 3rd iteration will move
99
+ # 3px instead of 2px.
72
100
  def drag(options={}, &block)
73
101
  raise ArgumentError.new(":to or :distance is required to provide ending location") unless options.has_key?(:to) || options.has_key?(:distance)
74
102
  raise ArgumentError.new(":increments must be > 0") if options.has_key?(:increments) && options[:increments] <= 0
@@ -76,13 +104,14 @@ module Casper
76
104
  from ||= options[:from] || location
77
105
  increments = options[:increments] || 10
78
106
  distance = options[:distance] || [ options[:to][0] - from[0], options[:to][1] - from[1] ]
79
-
80
- shift_x = distance[0] / increments
81
- shift_y = distance[1] / increments
82
107
 
83
108
  move from[0], from[1]
84
109
  down
85
- increments.times{ |i| move(shift_x, shift_y, true) }
110
+ increments.times do |i|
111
+ x = (from[0] + (distance[0].to_f / increments) * (i + 1)).round
112
+ y = (from[1] + (distance[1].to_f / increments) * (i + 1)).round
113
+ move(x, y)
114
+ end
86
115
  yield if block_given?
87
116
  up
88
117
  end
@@ -93,8 +122,9 @@ module Casper
93
122
  def xdo(&block)
94
123
  xdo = Libxdo.xdo_new(nil)
95
124
  yield(xdo)
125
+ sleep delay
96
126
  Libxdo.xdo_free(xdo)
97
- end
127
+ end
98
128
 
99
129
  # Performs an absolute mouse move
100
130
  def absolute(x, y)
@@ -17,6 +17,7 @@ $('<div class="target" id="#{id}"></div>').
17
17
  end
18
18
 
19
19
  setup do
20
+ Casper::Mouse.delay = 0.010
20
21
  Casper::Mouse.move 0, 0
21
22
  # Gets rid of the menu if it was left open (i.e. from leaving the mouse
22
23
  # button down and moving over it).
@@ -110,6 +111,33 @@ $('<div class="target" id="#{id}"></div>').
110
111
  assert_raise ArgumentError do
111
112
  Casper::Mouse.drag :from => [ 1, 1 ]
112
113
  end
113
- end
114
+ end
115
+
116
+ should "end in the correct location when the distance is not divisible by the number of increments" do
117
+ Casper::Mouse.drag :from => [ 0, 0 ], :to => [ 20, 20 ], :increments => 8
118
+ assert_equal [ 20, 20 ], Casper::Mouse.location
119
+ end
120
+
121
+ should "end in the correct location when one axis is not divisible by the number of increments" do
122
+ Casper::Mouse.drag :from => [ 0, 0 ], :to => [ 16, 20 ], :increments => 8
123
+ assert_equal [ 16, 20 ], Casper::Mouse.location
124
+ end
125
+ end
126
+
127
+ describe "with delay" do
128
+ should "move with no delay by default" do
129
+ start = Time.now
130
+ Casper::Mouse.move 10, 10
131
+ Casper::Mouse.move 20, 20
132
+ assert (Time.now.to_i - start.to_i) < 0.5
133
+ end
134
+
135
+ should "delay after moving" do
136
+ start = Time.now
137
+ Casper::Mouse.delay = 0.5
138
+ Casper::Mouse.move 10, 10
139
+ Casper::Mouse.move 20, 20
140
+ assert (Time.now.to_f - start.to_f) > 0.5
141
+ end
114
142
  end
115
143
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: casper
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 4
10
- version: 0.0.4
9
+ - 5
10
+ version: 0.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Alavi