Skip to content

Commit 8e2c43f

Browse files
committed
test(browser): Add E2E tests for device.id
1 parent 6f8080e commit 8e2c43f

File tree

6 files changed

+144
-3
lines changed

6 files changed

+144
-3
lines changed

Diff for: test/browser/features/device.feature

+31-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,38 @@
22
Feature: Browser device data
33

44
Scenario: Device data is included by default
5-
When I navigate to the URL "/on_error/script/a.html"
6-
Then I wait to receive a request
7-
And the request is a valid browser payload for the error reporting API
5+
When I navigate to the URL "/device/script/a.html"
6+
And I wait to receive a request
7+
Then the request is a valid browser payload for the error reporting API
88
And the event "device.time" is not null
99
And the event "device.locale" is not null
1010
And the event "device.userAgent" is not null
1111
And the event "device.orientation" matches "^(portrait|landscape)(-primary|-secondary)?$"
12+
And the event device ID is valid
13+
14+
Scenario: Device ID is not collected when the config option is disabled
15+
When I navigate to the URL "/device/script/b.html"
16+
And I wait to receive a request
17+
Then the request is a valid browser payload for the error reporting API
18+
And the event "device.time" is not null
19+
And the event "device.locale" is not null
20+
And the event "device.userAgent" is not null
21+
And the event "device.orientation" matches "^(portrait|landscape)(-primary|-secondary)?$"
22+
And the event "device.id" is null
23+
24+
@skip_if_local_storage_is_unavailable
25+
Scenario: Device ID is read from local storage
26+
When I navigate to the URL "/device/script/c.html"
27+
And I wait to receive a request
28+
Then the request is a valid browser payload for the error reporting API
29+
And the event device ID is "cabcdefghijklmnopqrstuvwx"
30+
When I navigate to the URL "/device/script/a.html"
31+
# This is 2 requests because the first request still counts (i.e. it's only 1 _new_ request)
32+
And I wait to receive 2 requests
33+
Then the request is a valid browser payload for the error reporting API
34+
And the event device ID is "cabcdefghijklmnopqrstuvwx"
35+
When I navigate to the URL "/device/script/a.html"
36+
# This is 3 requests because the first 2 requests still count
37+
And I wait to receive 3 requests
38+
Then the request is a valid browser payload for the error reporting API
39+
And the event device ID is "cabcdefghijklmnopqrstuvwx"

Diff for: test/browser/features/fixtures/device/script/a.html

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<script src="/node_modules/@bugsnag/browser/dist/bugsnag.min.js"></script>
6+
<script type="text/javascript">
7+
var ENDPOINT = decodeURIComponent(window.location.search.match(/ENDPOINT=([^&]+)/)[1])
8+
var API_KEY = decodeURIComponent(window.location.search.match(/API_KEY=([^&]+)/)[1])
9+
10+
Bugsnag.start({
11+
apiKey: API_KEY,
12+
endpoints: { notify: ENDPOINT, sessions: '/noop' }
13+
})
14+
</script>
15+
</head>
16+
<body>
17+
<script>
18+
Bugsnag.notify(new Error('device data does work'))
19+
</script>
20+
</body>
21+
</html>

Diff for: test/browser/features/fixtures/device/script/b.html

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<script src="/node_modules/@bugsnag/browser/dist/bugsnag.min.js"></script>
6+
<script type="text/javascript">
7+
var ENDPOINT = decodeURIComponent(window.location.search.match(/ENDPOINT=([^&]+)/)[1])
8+
var API_KEY = decodeURIComponent(window.location.search.match(/API_KEY=([^&]+)/)[1])
9+
10+
Bugsnag.start({
11+
apiKey: API_KEY,
12+
endpoints: { notify: ENDPOINT, sessions: '/noop' },
13+
generateAnonymousId: false
14+
})
15+
</script>
16+
</head>
17+
<body>
18+
<script>
19+
Bugsnag.notify(new Error('device ID can be disabled'))
20+
</script>
21+
</body>
22+
</html>

Diff for: test/browser/features/fixtures/device/script/c.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<script src="/node_modules/@bugsnag/browser/dist/bugsnag.min.js"></script>
6+
<script type="text/javascript">
7+
window.localStorage.setItem('bugsnag-anonymous-id', 'cabcdefghijklmnopqrstuvwx')
8+
9+
var ENDPOINT = decodeURIComponent(window.location.search.match(/ENDPOINT=([^&]+)/)[1])
10+
var API_KEY = decodeURIComponent(window.location.search.match(/API_KEY=([^&]+)/)[1])
11+
12+
Bugsnag.start({
13+
apiKey: API_KEY,
14+
endpoints: { notify: ENDPOINT, sessions: '/noop' }
15+
})
16+
</script>
17+
</head>
18+
<body>
19+
<script>
20+
Bugsnag.notify(new Error('device ID is read from localStorage'))
21+
</script>
22+
</body>
23+
</html>

Diff for: test/browser/features/steps/browser_steps.rb

+18
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,21 @@
101101
And the payload has a valid sessions array
102102
}
103103
end
104+
105+
Then("the event device ID is valid") do
106+
if has_local_storage
107+
step('the event "device.id" matches "^c[a-z0-9]{20,32}$"')
108+
else
109+
$logger.info('Local storage is not supported in this browser, assuming device ID is null')
110+
step('the event "device.id" is null')
111+
end
112+
end
113+
114+
Then("the event device ID is {string}") do |expected_id|
115+
if has_local_storage
116+
step("the event \"device.id\" equals \"#{expected_id}\"")
117+
else
118+
$logger.info('Local storage is not supported in this browser, assuming device ID is null')
119+
step('the event "device.id" is null')
120+
end
121+
end

Diff for: test/browser/features/support/env.rb

+29
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,35 @@ def get_error_message id
2929
ERRORS[ENV['BROWSER']][id]
3030
end
3131

32+
# check if Selenium supports running javascript in the current browser
33+
def can_run_javascript
34+
$driver.execute_script('return true')
35+
rescue Selenium::WebDriver::Error::UnsupportedOperationError
36+
false
37+
end
38+
39+
# check if the browser supports local storage, e.g. safari 10 on browserstack
40+
# does not have working local storage
41+
def has_local_storage
42+
# Assume we can use local storage if we aren't able to verify by running JavaScript
43+
return true unless can_run_javascript
44+
45+
$driver.execute_script <<-JAVASCRIPT
46+
try {
47+
window.localStorage.setItem('__localstorage_test__', 1234)
48+
window.localStorage.removeItem('__localstorage_test__')
49+
50+
return true
51+
} catch (err) {
52+
return false
53+
}
54+
JAVASCRIPT
55+
end
56+
57+
Before('@skip_if_local_storage_is_unavailable') do |scenario|
58+
skip_this_scenario unless has_local_storage
59+
end
60+
3261
AfterConfiguration do
3362
# Necessary as Appium removes any existing $driver instance on load
3463
bs_local_start

0 commit comments

Comments
 (0)