Skip to content

Commit

Permalink
Send buildId in SessionStartedUserAction when GRAVITY_BUILD_ID or…
Browse files Browse the repository at this point in the history
… `REACT_APP_GRAVITY_BUILD_ID` is set.
  • Loading branch information
aymeric-cr committed Jan 16, 2023
1 parent 7cb9e5b commit 0777855
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 1 deletion.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Send `buildId` in SessionStartedUserAction when `GRAVITY_BUILD_ID` or `REACT_APP_GRAVITY_BUILD_ID` is set.

### Changed

### Deprecated
Expand Down Expand Up @@ -43,7 +45,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- New option `excludeRegex`: regular expression to define ID and class names to ignore in selector computation.
- New option `customSelector`: string indicates the attribute to use as a selector if defined on an HTML element targeted by a user action.
- New option `customSelector`: string indicates the attribute to use as a selector if defined on an HTML element
targeted by a user action.

## [3.1.1](https://github.com/Smartesting/gravity-data-collector/compare/v3.1.0...v3.1.1)

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,17 @@ window.GravityCollector.identifySession('connected', true)
**Note**: Please, keep in mind that each trait can only have a single value. It means if you set the trait `connected`
to `true` and then to `false`, the first value will be overwritten.

### Send build information to Gravity

In order to easily identify your tests sessions in Gravity, the data-collector can send build information to Gravity:

| environment variable name | Gravity data |
| -------------------------- | ------------ |
| GRAVITY_BUILD_ID | buildId |
| REACT_APP_GRAVITY_BUILD_ID | buildId |

Those variables can be easily exposed in `process.env`.

## Sandbox

In order to test modifications on the library, a sandbox is accessible in [index.html](sample/index.html) file
Expand Down
26 changes: 26 additions & 0 deletions src/user-action/createSessionStartedUserAction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,32 @@ describe('action', () => {
expect(createSessionStartedUserAction().recordedAt).toEqual(now)
})

it('sets buildId field when GRAVITY_BUILD_ID env var is set', () => {
process.env.GRAVITY_BUILD_ID = '51'
expect(createSessionStartedUserAction().buildId).toEqual('51')
})

it('does not set buildId field when GRAVITY_BUILD_ID is empty', () => {
process.env.GRAVITY_BUILD_ID = ''
expect(createSessionStartedUserAction().buildId).toEqual(undefined)
})

it('sets buildId field when REACT_APP_GRAVITY_BUILD_ID env var is set', () => {
process.env.REACT_APP_GRAVITY_BUILD_ID = '42'
expect(createSessionStartedUserAction().buildId).toEqual('42')
})

it('does not set buildId field when REACT_APP_GRAVITY_BUILD_ID is empty', () => {
process.env.REACT_APP_GRAVITY_BUILD_ID = ''
expect(createSessionStartedUserAction().buildId).toEqual(undefined)
})

it('sets buildId field from GRAVITY_BUILD_ID when GRAVITY_BUILD_ID and REACT_APP_GRAVITY_BUILD_ID are set', () => {
process.env.GRAVITY_BUILD_ID = '12'
process.env.REACT_APP_GRAVITY_BUILD_ID = '13'
expect(createSessionStartedUserAction().buildId).toEqual('12')
})

it('returns Cypress current test if any', () => {
expect(createSessionStartedUserAction().test).toBeUndefined()

Expand Down
14 changes: 14 additions & 0 deletions src/user-action/createSessionStartedUserAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ import { SessionStartedUserAction, UserActionType } from '../types'
import { config } from '../config'
import gravityDocument from '../utils/gravityDocument'

function buildId() {
return (
rejectBlankString(process.env.GRAVITY_BUILD_ID) ??
rejectBlankString(process.env.REACT_APP_GRAVITY_BUILD_ID) ??
undefined
)
}

function rejectBlankString(value: string | undefined): string | null {
if (value !== undefined && value !== '') return value
return null
}

export function createSessionStartedUserAction(): SessionStartedUserAction {
const action: SessionStartedUserAction = {
type: UserActionType.SessionStarted,
Expand All @@ -13,6 +26,7 @@ export function createSessionStartedUserAction(): SessionStartedUserAction {
viewportData: viewport(),
version: config.COLLECTOR_VERSION,
agent: navigator.userAgent,
buildId: buildId(),
}

const cypress = (window as any).Cypress
Expand Down

0 comments on commit 0777855

Please sign in to comment.