Skip to content

Commit

Permalink
Fix Appium reload session (#3506)
Browse files Browse the repository at this point in the history
## Proposed changes

## Types of changes

[//]: # (What types of changes does your code introduce to WebdriverIO?)
[//]: # (_Put an `x` in the boxes that apply_)

- [x] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)

## Checklist

- [x] I have read the [CONTRIBUTING](https://github.com/webdriverio/webdriverio/blob/master/CONTRIBUTING.md) doc
- [x] I have added tests that prove my fix is effective or that my feature works
- [x] I have added necessary documentation (if appropriate)

## Further comments

fix #3494 

webdriver typings and webdriverio docs have to be generated before releasing 

### Reviewers: @webdriverio/technical-committee
  • Loading branch information
mgrybyk authored and christian-bromann committed Feb 5, 2019
1 parent f4f09ad commit 5cde958
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion packages/wdio-runner/tests/reporter.test.js
Expand Up @@ -159,7 +159,7 @@ describe('BaseReporter', () => {
reporterSyncTimeout: 100
})

setTimeout(() => (reporter.reporters[0].inSync = true), 110)
setTimeout(() => (reporter.reporters[0].inSync = true), 112)
await expect(reporter.waitForSync())
.rejects.toEqual(new Error('Some reporters are still unsynced: CustomReporter'))
})
Expand Down
8 changes: 4 additions & 4 deletions packages/webdriverio/src/commands/browser/reloadSession.js
Expand Up @@ -8,14 +8,14 @@
*
* <example>
:reloadSync.js
it('should reload my session', () => {
it('should reload my session with current capabilities', () => {
console.log(browser.sessionId) // outputs: e042b3f3cd5a479da4e171825e96e655
browser.reload()
browser.reloadSession()
console.log(browser.sessionId) // outputs: 9a0d9bf9d4864160aa982c50cf18a573
})
* </example>
*
* @alias browser.reload
* @alias browser.reloadSession
* @type utility
*
*/
Expand All @@ -41,7 +41,7 @@ export default async function reloadSession () {
)

const response = await sessionRequest.makeRequest(this.options)
const newSessionId = response.sessionId
const newSessionId = response.sessionId || (response.value && response.value.sessionId)
this.sessionId = newSessionId

if (Array.isArray(this.options.onReload) && this.options.onReload.length) {
Expand Down
15 changes: 13 additions & 2 deletions packages/webdriverio/tests/__mocks__/request.js
Expand Up @@ -2,12 +2,14 @@ import { ELEMENT_KEY } from '../../src/constants'

let manualMockResponse

const sessionId = 'foobar-123'
const defaultSessionId = 'foobar-123'
let sessionId = defaultSessionId
const genericElementId = 'some-elem-123'
const genericSubElementId = 'some-sub-elem-321'
const genericSubSubElementId = 'some-sub-sub-elem-231'
const requestMock = jest.fn().mockImplementation((params, cb) => {
let value = {}
let jsonwpMode = false
let sessionResponse = {
sessionId,
capabilities: {
Expand All @@ -21,6 +23,7 @@ const requestMock = jest.fn().mockImplementation((params, cb) => {
params.body.capabilities &&
params.body.capabilities.alwaysMatch.jsonwpMode
) {
jsonwpMode = true
sessionResponse = {
sessionId,
browserName: 'mockBrowser'
Expand Down Expand Up @@ -217,7 +220,7 @@ const requestMock = jest.fn().mockImplementation((params, cb) => {
}

let response = { value }
if (params.jsonwpMode) {
if (jsonwpMode) {
response = { value, sessionId, status: 0 }
}

Expand All @@ -233,4 +236,12 @@ requestMock.setMockResponse = (value) => {
manualMockResponse = value
}

requestMock.getSessionId = () => sessionId
requestMock.setSessionId = (newSessionId) => {
sessionId = newSessionId
}
requestMock.resetSessionId = () => {
sessionId = defaultSessionId
}

export default requestMock
67 changes: 52 additions & 15 deletions packages/webdriverio/tests/commands/browser/reloadSession.test.js
Expand Up @@ -2,22 +2,59 @@ import request from 'request'
import { remote } from '../../../src'

describe('reloadSession test', () => {
it('should allow to check if an element is enabled', async () => {
const hook = jest.fn()
const browser = await remote({
baseUrl: 'http://foobar.com',
capabilities: {
browserName: 'foobar'
},
onReload: [hook]
})
const scenarios = [{
name: 'should be undefined if sessionId is missing in response',
sessionIdMock: 'ignored if jsonwpMode is false',
requestMock: [{}, {}],
newSessionId: undefined,
jsonwpMode: false
}, {
name: 'should be ok if sessionId is in response',
sessionIdMock: 'foobar-234',
requestMock: [{}, {}],
newSessionId: 'foobar-234',
jsonwpMode: true
}, {
name: 'should be ok if sessionId is in response.value',
sessionIdMock: undefined,
requestMock: [{}, { sessionId: 'foobar-345' }],
newSessionId: 'foobar-345',
}, {
name: 'should be sessionId if sessionId and value.sessionId are present',
sessionIdMock: 'foobar-456',
requestMock: [{}, { sessionId: 'foobar-567' }],
newSessionId: 'foobar-456',
jsonwpMode: true
}]

scenarios.forEach(scenario => {
it(scenario.name, async () => {
const oldSessionId = request.getSessionId()
const hook = jest.fn()
const browser = await remote({
baseUrl: 'http://foobar.com',
capabilities: {
jsonwpMode: scenario.jsonwpMode,
browserName: 'foobar'
},
onReload: [hook]
})

request.setSessionId(scenario.sessionIdMock)
request.setMockResponse(scenario.requestMock)
await browser.reloadSession()

await browser.reloadSession()
expect(request.mock.calls[1][0].method).toBe('DELETE')
expect(request.mock.calls[1][0].uri.pathname).toBe(`/wd/hub/session/${oldSessionId}`)
expect(request.mock.calls[2][0].method).toBe('POST')
expect(request.mock.calls[2][0].uri.pathname).toBe('/wd/hub/session')
expect(hook).toBeCalledWith(oldSessionId, scenario.newSessionId)
})
})

expect(request.mock.calls[1][0].method).toBe('DELETE')
expect(request.mock.calls[1][0].uri.pathname).toBe('/wd/hub/session/foobar-123')
expect(request.mock.calls[2][0].method).toBe('POST')
expect(request.mock.calls[2][0].uri.pathname).toBe('/wd/hub/session')
expect(hook).toBeCalledWith('foobar-123', undefined)
afterEach(() => {
request.mockClear()
request.resetSessionId()
request.setMockResponse()
})
})
1 change: 1 addition & 0 deletions scripts/templates/webdriver.tpl.d.ts
Expand Up @@ -239,6 +239,7 @@ declare namespace WebDriver {
isAndroid: boolean;
isMobile: boolean;
isIOS: boolean;
sessionId: string;
}

// generated typings
Expand Down

0 comments on commit 5cde958

Please sign in to comment.