Skip to content

Commit c232888

Browse files
authored
feat: add posting log event/getting events (#242)
* add log_event endpoint * move posting log event into logs * add tests * add docstring * tweak the test command * add events as a shortcut command * add events * tweak name space
1 parent 6a7a1cd commit c232888

File tree

8 files changed

+224
-2
lines changed

8 files changed

+224
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Read `release_notes.md` for commit level details.
55
## [Unreleased]
66

77
### Enhancements
8+
- Add `Logs#event` to post a custom log by `@driver.logs.event vendor: 'appium', event: 'funEvent'`
9+
- Add `Logs#events` to get events by `@driver.logs.events`. It is equal to `@driver.session_capabilities['events']`
810

911
### Bug fixes
1012

lib/appium_lib_core/common/base/bridge/mjsonwp.rb

+13
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ def sessions
4242
execute :get_all_sessions
4343
end
4444

45+
# For Appium
46+
def log_event(vendor, event)
47+
execute :post_log_event, {}, { vendor: vendor, event: event }
48+
end
49+
50+
# For Appium
51+
def log_events(type = nil)
52+
args = {}
53+
args['type'] = type unless type.nil?
54+
55+
execute :get_log_events, {}, args
56+
end
57+
4558
def take_element_screenshot(element)
4659
execute :take_element_screenshot, id: element.ref
4760
end

lib/appium_lib_core/common/base/bridge/w3c.rb

+13
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ def log(type)
204204
end
205205
end
206206

207+
# For Appium
208+
def log_event(vendor, event)
209+
execute :post_log_event, {}, { vendor: vendor, event: event }
210+
end
211+
212+
# For Appium
213+
def log_events(type = nil)
214+
args = {}
215+
args['type'] = type unless type.nil?
216+
217+
execute :get_log_events, {}, args
218+
end
219+
207220
def take_viewport_screenshot
208221
execute_script('mobile: viewportScreenshot')
209222
end

lib/appium_lib_core/common/command/common.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ module Commands
6565
start_recording_screen: [:post, 'session/:session_id/appium/start_recording_screen'],
6666
compare_images: [:post, 'session/:session_id/appium/compare_images'],
6767
is_keyboard_shown: [:get, 'session/:session_id/appium/device/is_keyboard_shown'],
68-
execute_driver: [:post, 'session/:session_id/appium/execute_driver']
68+
execute_driver: [:post, 'session/:session_id/appium/execute_driver'],
69+
post_log_event: [:post, 'session/:session_id/appium/log_event'],
70+
get_log_events: [:post, 'session/:session_id/appium/events']
6971
}.freeze
7072

7173
COMMAND_ANDROID = {

lib/appium_lib_core/common/log.rb

+44-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def initialize(bridge)
2424
#
2525
# @example
2626
#
27-
# @driver.logs.get "syslog" # []
27+
# @driver.logs.get 'syslog' # []
2828
# @driver.logs.get :syslog # []
2929
#
3030
def get(type)
@@ -41,6 +41,49 @@ def get(type)
4141
def available_types
4242
@bridge.available_log_types
4343
end
44+
45+
# @since Appium 1.16.0
46+
#
47+
# Logs a custom event. The event is available via {::Appium::Core::Events#get} or
48+
# <code>@driver.session_capabilities['events']</code> with <code>eventTimings</code> capabilities.
49+
#
50+
# @param [String] vendor The vendor prefix for the event
51+
# @param [String] event The name of event
52+
# @returns [nil]
53+
#
54+
# @example
55+
#
56+
# @driver.logs.event vendor: 'appium', event: 'funEvent'
57+
# @driver.session_capabilities['events'] #=> {...., 'appium:funEvent' => 1572957315}
58+
#
59+
# @driver.logs.event = { vendor: 'appium', event: 'anotherEvent' }
60+
# @driver.logs.events #=> {...., 'appium:funEvent' => [1572957315, 1572960305],
61+
# # 'appium:anotherEvent' => 1572959315}
62+
#
63+
def event(vendor:, event:)
64+
@bridge.log_event vendor, event
65+
end
66+
67+
def event=(log_event)
68+
raise ArgumentError('log_event should be Hash like { vendor: "appium", event: "funEvent"}') unless log_event.is_a?(Hash)
69+
70+
event vendor: log_event[:vendor], event: log_event[:event]
71+
end
72+
73+
# @since Appium 1.16.0
74+
# Returns events with filtering with 'type'. Defaults to all available events.
75+
#
76+
# @param [String] type The type of events to get
77+
# @return [Hash]
78+
#
79+
# @example
80+
#
81+
# @driver.logs.events #=> {}
82+
# @driver.logs.events #=> {'commands' => [{'cmd' => 123455, ....}], 'startTime' => 1572954894127, }
83+
#
84+
def events(type = nil)
85+
@bridge.log_events(type)
86+
end
4487
end
4588
end # module Core
4689
end # module Appium

lib/appium_lib_core/driver.rb

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def initialize(capabilities)
9393

9494
class Driver
9595
include Waitable
96+
9697
# Selenium webdriver capabilities
9798
# @return [Core::Base::Capabilities]
9899
attr_reader :caps
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'test_helper'
16+
require 'webmock/minitest'
17+
18+
# $ rake test:unit TEST=test/unit/android/device/mjsonwp/event_test.rb
19+
class AppiumLibCoreTest
20+
module Android
21+
module Device
22+
module MJSONWP
23+
class EventTest < Minitest::Test
24+
include AppiumLibCoreTest::Mock
25+
26+
def setup
27+
@core ||= ::Appium::Core.for(Caps.android)
28+
@driver ||= android_mock_create_session
29+
end
30+
31+
def test_log_event
32+
stub_request(:post, "#{SESSION}/appium/log_event")
33+
.with(body: { vendor: 'appium', event: 'customEvent' }.to_json)
34+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
35+
36+
@driver.logs.event vendor: 'appium', event: 'customEvent'
37+
38+
assert_requested(:post, "#{SESSION}/appium/log_event", times: 1)
39+
end
40+
41+
def test_log_event_equal
42+
stub_request(:post, "#{SESSION}/appium/log_event")
43+
.with(body: { vendor: 'appium', event: 'customEvent' }.to_json)
44+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
45+
46+
@driver.logs.event = { vendor: 'appium', event: 'customEvent' }
47+
48+
assert_requested(:post, "#{SESSION}/appium/log_event", times: 1)
49+
end
50+
51+
def test_log_events
52+
stub_request(:post, "#{SESSION}/appium/events")
53+
.with(body: {}.to_json)
54+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
55+
56+
@driver.logs.events
57+
58+
assert_requested(:post, "#{SESSION}/appium/events", times: 1)
59+
end
60+
61+
def test_log_events_with_type
62+
stub_request(:post, "#{SESSION}/appium/events")
63+
.with(body: { type: 'commands' }.to_json)
64+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
65+
66+
@driver.logs.events('commands')
67+
68+
assert_requested(:post, "#{SESSION}/appium/events", times: 1)
69+
end
70+
end # class EventTest
71+
end # module MJSONWP
72+
end # module Device
73+
end # module Android
74+
end # class AppiumLibCoreTest
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# frozen_string_literal: true
2+
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
require 'test_helper'
16+
require 'webmock/minitest'
17+
18+
# $ rake test:unit TEST=test/unit/android/device/w3c/event_test.rb
19+
class AppiumLibCoreTest
20+
module Android
21+
module Device
22+
module W3C
23+
class EventTest < Minitest::Test
24+
include AppiumLibCoreTest::Mock
25+
26+
def setup
27+
@core ||= ::Appium::Core.for(Caps.android)
28+
@driver ||= android_mock_create_session
29+
end
30+
31+
def test_log_event
32+
stub_request(:post, "#{SESSION}/appium/log_event")
33+
.with(body: { vendor: 'appium', event: 'customEvent' }.to_json)
34+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
35+
36+
@driver.logs.event vendor: 'appium', event: 'customEvent'
37+
38+
assert_requested(:post, "#{SESSION}/appium/log_event", times: 1)
39+
end
40+
41+
def test_log_event_equal
42+
stub_request(:post, "#{SESSION}/appium/log_event")
43+
.with(body: { vendor: 'appium', event: 'customEvent' }.to_json)
44+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
45+
46+
@driver.logs.event = { vendor: 'appium', event: 'customEvent' }
47+
48+
assert_requested(:post, "#{SESSION}/appium/log_event", times: 1)
49+
end
50+
51+
def test_log_events
52+
stub_request(:post, "#{SESSION}/appium/events")
53+
.with(body: {}.to_json)
54+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
55+
56+
@driver.logs.events
57+
58+
assert_requested(:post, "#{SESSION}/appium/events", times: 1)
59+
end
60+
61+
def test_log_events_with_type
62+
stub_request(:post, "#{SESSION}/appium/events")
63+
.with(body: { type: 'commands' }.to_json)
64+
.to_return(headers: HEADER, status: 200, body: { value: nil }.to_json)
65+
66+
@driver.logs.events('commands')
67+
68+
assert_requested(:post, "#{SESSION}/appium/events", times: 1)
69+
end
70+
end # class EventTest
71+
end # module W3C
72+
end # module Device
73+
end # module Android
74+
end # class AppiumLibCoreTest

0 commit comments

Comments
 (0)