Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Appium reload session #3506

Merged
merged 2 commits into from Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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