webkit_remote 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,109 @@
1
+ require File.expand_path('../../helper.rb', File.dirname(__FILE__))
2
+
3
+ describe WebkitRemote::Client::Runtime do
4
+ before :all do
5
+ @client = WebkitRemote.local port: 9669
6
+ @client.page_events = true
7
+ @client.navigate_to fixture_url(:runtime)
8
+ @client.wait_for type: WebkitRemote::Event::PageLoaded
9
+ end
10
+ after :all do
11
+ @client.close
12
+ end
13
+
14
+ describe 'remote_eval' do
15
+ describe 'for a number' do
16
+ before :each do
17
+ @number = @client.remote_eval '11 + 31', group: 'no'
18
+ end
19
+ it 'returns a Ruby number' do
20
+ @number.must_equal 42
21
+ end
22
+ it 'does not create an object group' do
23
+ @client.object_group('no').must_equal nil
24
+ end
25
+ end
26
+
27
+ describe 'for a boolean' do
28
+ before :each do
29
+ @true = @client.remote_eval '!!1', group: 'no'
30
+ @false = @client.remote_eval '!!0', group: 'no'
31
+ end
32
+ it 'returns a Ruby boolean' do
33
+ @true.must_equal true
34
+ @false.must_equal false
35
+ end
36
+ it 'does not create an object group' do
37
+ @client.object_group('no').must_equal nil
38
+ end
39
+ end
40
+
41
+ describe 'for a string' do
42
+ before :each do
43
+ @string = @client.remote_eval '"hello Ruby"', group: 'no'
44
+ end
45
+ it 'returns a Ruby string' do
46
+ @string.must_equal 'hello Ruby'
47
+ end
48
+ it 'does not create an object group' do
49
+ @client.object_group('no').must_equal nil
50
+ end
51
+ end
52
+
53
+ describe 'for null' do
54
+ before :each do
55
+ @null = @client.remote_eval 'null', group: 'no'
56
+ end
57
+ it 'returns nil' do
58
+ @string.must_equal nil
59
+ end
60
+ it 'does not create an object group' do
61
+ @client.object_group('no').must_equal nil
62
+ end
63
+ end
64
+
65
+ describe 'for an object created via new' do
66
+ before :each do
67
+ @object = @client.remote_eval 'new TestClass("hello Ruby")',
68
+ group: 'yes'
69
+ end
70
+ after :each do
71
+ group = @client.object_group('yes')
72
+ group.release_all if group
73
+ end
74
+ it 'returns an RemoteObject instance' do
75
+ @object.must_be_kind_of WebkitRemote::Client::RemoteObject
76
+ end
77
+ it 'sets the object properties correctly' do
78
+ @object.js_class_name.must_equal 'TestClass'
79
+ @object.description.must_equal 'TestClass'
80
+ end
81
+ it 'creates a non-released group' do
82
+ @client.object_group('yes').wont_equal nil
83
+ @client.object_group('yes').released?.must_equal false
84
+ end
85
+ end
86
+
87
+ describe 'for a JSON object' do
88
+ before :each do
89
+ @object = @client.remote_eval '({hello: "ruby", answer: 42})',
90
+ group: 'yes'
91
+ end
92
+ after :each do
93
+ group = @client.object_group('yes')
94
+ group.release_all if group
95
+ end
96
+ it 'returns an RemoteObject instance' do
97
+ @object.must_be_kind_of WebkitRemote::Client::RemoteObject
98
+ end
99
+ it 'sets the object properties correctly' do
100
+ @object.js_class_name.must_equal 'Object'
101
+ @object.description.must_equal 'Object'
102
+ end
103
+ it 'creates a non-released group' do
104
+ @client.object_group('yes').wont_equal nil
105
+ @client.object_group('yes').released?.must_equal false
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,99 @@
1
+ require File.expand_path('../helper.rb', File.dirname(__FILE__))
2
+
3
+ describe WebkitRemote::Client do
4
+ before :each do
5
+ @process = WebkitRemote::Process.new port: 9669
6
+ @process.start
7
+ @browser = WebkitRemote::Browser.new process: @process, stop_process: true
8
+ tab = @browser.tabs.first
9
+ @client = WebkitRemote::Client.new tab: tab
10
+ end
11
+ after :each do
12
+ @client.close if @client
13
+ @browser.close if @browser
14
+ @process.stop if @process
15
+ end
16
+
17
+ it 'sets close_browser to false, browser to the given Browser instance' do
18
+ @client.close_browser?.must_equal false
19
+ @client.browser.must_equal @browser
20
+ end
21
+
22
+ describe 'close with close_browser is true' do
23
+ before do
24
+ @client.close_browser = true
25
+ @client.close
26
+ end
27
+
28
+ it 'closes the client debugging connection' do
29
+ @client.closed?.must_equal true
30
+ end
31
+
32
+ it 'closes the browser master debugging session' do
33
+ @browser.closed?.must_equal true
34
+ end
35
+ end
36
+
37
+ describe 'each_event' do
38
+ before do
39
+ @client.rpc.call 'Page.enable'
40
+ @client.rpc.call 'Page.navigate', url: fixture_url(:load)
41
+ @events = []
42
+ @client.each_event do |event|
43
+ @events << event
44
+ break if event.kind_of?(WebkitRemote::Event::PageLoaded)
45
+ end
46
+ end
47
+
48
+ it 'only yields events' do
49
+ @events.each do |event|
50
+ event.must_be_kind_of WebkitRemote::Event
51
+ end
52
+ end
53
+
54
+ it 'contains a PageLoaded instance' do
55
+ @events.map(&:class).must_include WebkitRemote::Event::PageLoaded
56
+ end
57
+ end
58
+
59
+ describe 'wait_for' do
60
+ describe 'with page_events enabled' do
61
+ before do
62
+ @client.page_events = true
63
+ @client.rpc.call 'Page.navigate', url: fixture_url(:load)
64
+ end
65
+
66
+ it 'returns a PageLoaded instance' do
67
+ @client.wait_for(type: WebkitRemote::Event::PageLoaded).
68
+ must_be_kind_of WebkitRemote::Event::PageLoaded
69
+ end
70
+ end
71
+
72
+ describe 'with page_events disabled' do
73
+ it 'raises ArgumentError' do
74
+ lambda {
75
+ @client.wait_for(type: WebkitRemote::Event::PageLoaded)
76
+ }.must_raise ArgumentError
77
+ end
78
+ end
79
+ end
80
+
81
+ describe 'rpc' do
82
+ it 'is is a non-closed WebkitRemote::Rpc instance' do
83
+ @client.rpc.must_be_kind_of WebkitRemote::Rpc
84
+ @client.rpc.closed?.must_equal false
85
+ end
86
+
87
+ describe 'after calling close' do
88
+ before do
89
+ @client_rpc = @client.rpc
90
+ @client.close
91
+ end
92
+
93
+ it 'the Rpc instance is closed' do
94
+ @client_rpc.closed?.must_equal true
95
+ end
96
+ end
97
+ end
98
+ end
99
+
@@ -0,0 +1,83 @@
1
+ require File.expand_path('../helper.rb', File.dirname(__FILE__))
2
+
3
+ describe WebkitRemote::Event do
4
+ before do
5
+ @client = WebkitRemote.local port: 9669
6
+ end
7
+ after do
8
+ @client.close
9
+ end
10
+
11
+ describe 'on a PageLoaded event' do
12
+ before do
13
+ @url = fixture_url(:load)
14
+ @client.page_events = true
15
+ @client.navigate_to @url
16
+ events = []
17
+ # NOTE: wait_for uses Event#matches, and we're testing that here
18
+ @client.each_event do |event|
19
+ events << event
20
+ break if event.kind_of?(WebkitRemote::Event::PageLoaded)
21
+ end
22
+ @event = events.last
23
+ end
24
+
25
+ describe 'matches' do
26
+ it 'handles single conditions' do
27
+ @event.matches?(:class => WebkitRemote::Event::PageLoaded).
28
+ must_equal true
29
+ @event.matches?(type: WebkitRemote::Event).must_equal true
30
+ @event.matches?(:class => WebkitRemote::Event::PageDomReady).
31
+ must_equal false
32
+ @event.matches?(name: 'Page.loadEventFired').must_equal true
33
+ @event.matches?(name: 'loadEventFired').must_equal false
34
+ @event.matches?(domain: 'Page').must_equal true
35
+ @event.matches?(domain: 'Runtime').must_equal false
36
+ end
37
+
38
+ it 'handles multiple conditions' do
39
+ @event.matches?(type: WebkitRemote::Event::PageLoaded,
40
+ domain: 'Page').must_equal true
41
+ @event.matches?(type: WebkitRemote::Event::PageLoaded,
42
+ domain: 'Runtime').must_equal false
43
+ @event.matches?(type: WebkitRemote::Event::PageDomReady,
44
+ domain: 'Page').must_equal false
45
+ end
46
+ end
47
+ end
48
+
49
+ describe 'can_receive?' do
50
+ describe 'when page_events is false' do
51
+ before do
52
+ @client.page_events = false
53
+ end
54
+ it 'should be true for the base class' do
55
+ WebkitRemote::Event.can_receive?(@client, type: WebkitRemote::Event).
56
+ must_equal true
57
+ end
58
+ it 'should be false for PageLoaded' do
59
+ WebkitRemote::Event.can_receive?(@client,
60
+ type: WebkitRemote::Event::PageLoaded).must_equal false
61
+ end
62
+ it 'should be false for Page.loadEventFired' do
63
+ WebkitRemote::Event.can_receive?(@client, name: 'Page.loadEventFired').
64
+ must_equal false
65
+ end
66
+ end
67
+
68
+ describe 'when page_events is true' do
69
+ before do
70
+ @client.page_events = true
71
+ end
72
+ it 'should be true for PageLoaded' do
73
+ WebkitRemote::Event.can_receive?(@client,
74
+ type: WebkitRemote::Event::PageLoaded).must_equal true
75
+ end
76
+ it 'should be true for Page.loadEventFired' do
77
+ WebkitRemote::Event.can_receive?(@client, name: 'Page.loadEventFired').
78
+ must_equal true
79
+ end
80
+ end
81
+ end
82
+ end
83
+
@@ -0,0 +1,52 @@
1
+ require File.expand_path('../helper.rb', File.dirname(__FILE__))
2
+
3
+ describe WebkitRemote::Process do
4
+ before :each do
5
+ @process = WebkitRemote::Process.new port: 9669
6
+ end
7
+ after :each do
8
+ @process.stop if @process
9
+ end
10
+
11
+ describe '#running' do
12
+ it 'returns false before #start is called' do
13
+ @process.running?.must_equal false
14
+ end
15
+ end
16
+
17
+ describe '#start' do
18
+ before :each do
19
+ @browser = @process.start
20
+ end
21
+ after :each do
22
+ @browser.close if @browser
23
+ @process.stop if @process
24
+ end
25
+
26
+ it 'makes running? return true' do
27
+ @process.running?.must_equal true
28
+ end
29
+
30
+ it 'returns a Browser instance that does not auto-stop the process' do
31
+ @browser.must_be_kind_of WebkitRemote::Browser
32
+ @browser.closed?.must_equal false
33
+ @browser.stop_process?.must_equal false
34
+ end
35
+
36
+ describe '#stop' do
37
+ before :each do
38
+ @process.stop
39
+ end
40
+
41
+ it 'makes running? return false' do
42
+ @process.running?.must_equal false
43
+ end
44
+
45
+ it 'kills the http server that responds to /json' do
46
+ lambda {
47
+ @browser.tabs
48
+ }.must_raise EOFError
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,55 @@
1
+ require File.expand_path('../helper.rb', File.dirname(__FILE__))
2
+
3
+ describe WebkitRemote::Rpc do
4
+ before :each do
5
+ @process = WebkitRemote::Process.new port: 9669
6
+ @process.start
7
+ @browser = WebkitRemote::Browser.new process: @process, stop_process: true
8
+ tab = @browser.tabs.first
9
+ @rpc = WebkitRemote::Rpc.new tab: tab
10
+ end
11
+ after :each do
12
+ @rpc.close if @rpc
13
+ @browser.close if @browser
14
+ @process.stop if @process
15
+ end
16
+
17
+ describe 'call' do
18
+ before do
19
+ @result = @rpc.call 'Runtime.evaluate', expression: '1 + 2',
20
+ returnByValue: true
21
+ end
22
+
23
+ it 'produces the correct result' do
24
+ @result.must_include 'result'
25
+ @result['result'].must_include 'value'
26
+ @result['result']['value'].must_equal 3
27
+ @result['result'].must_include 'type'
28
+ @result['result']['type'].must_equal 'number'
29
+ end
30
+ end
31
+
32
+ describe 'each_event' do
33
+ before do
34
+ @rpc.call 'Page.enable'
35
+ @rpc.call 'Page.navigate', url: fixture_url(:load)
36
+ @events = []
37
+ @rpc.each_event do |event|
38
+ @events << event
39
+ break if event[:name] == 'Page.loadEventFired'
40
+ end
41
+ end
42
+
43
+ it 'only yields events' do
44
+ @events.each do |event|
45
+ event.must_include :name
46
+ event.must_include :data
47
+ end
48
+ end
49
+
50
+ it 'contains a Page.loadEventFired event' do
51
+ @events.map { |e| e[:name] }.must_include 'Page.loadEventFired'
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,63 @@
1
+ require File.expand_path('helper.rb', File.dirname(__FILE__))
2
+
3
+ describe WebkitRemote do
4
+ describe 'local' do
5
+ before do
6
+ @client = WebkitRemote.local port: 9669
7
+ end
8
+ after do
9
+ @client.close
10
+ end
11
+
12
+ it 'returns a working client' do
13
+ @client.must_be_kind_of WebkitRemote::Client
14
+ @client.closed?.must_equal false
15
+ end
16
+
17
+ describe 'after #close' do
18
+ before do
19
+ @client.close
20
+ end
21
+
22
+ it 'the client tears down everything' do
23
+ @client.closed?.must_equal true
24
+ @client.browser.closed?.must_equal true
25
+ @client.browser.process.running?.must_equal false
26
+ end
27
+ end
28
+ end
29
+
30
+ describe 'remote' do
31
+ before do
32
+ @process = WebkitRemote::Process.new port: 9669
33
+ browser = @process.start
34
+ browser.close
35
+ @client = WebkitRemote.remote host: 'localhost', port: 9669
36
+ end
37
+
38
+ after do
39
+ @client.close
40
+ @process.stop
41
+ end
42
+
43
+ it 'returns a working client' do
44
+ @client.must_be_kind_of WebkitRemote::Client
45
+ @client.closed?.must_equal false
46
+ end
47
+
48
+ describe 'after #close' do
49
+ before do
50
+ @client.close
51
+ end
52
+
53
+ it 'the client tears the connection' do
54
+ @client.closed?.must_equal true
55
+ @client.browser.closed?.must_equal true
56
+ end
57
+
58
+ it 'the client does not impact the browser process' do
59
+ @process.running?.must_equal true
60
+ end
61
+ end
62
+ end
63
+ end