Skip to content

Commit

Permalink
embed expect-webdriverio in framework adapters (#5335)
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Apr 28, 2020
1 parent 99a7346 commit 5bb5399
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 21 deletions.
41 changes: 38 additions & 3 deletions docs/TypeScript.md
Expand Up @@ -66,7 +66,7 @@ And your `tsconfig.json` needs the following:
"*": [ "./*" ],
"src/*": ["./src/*"]
},
"types": ["node", "webdriverio", "expect-webdriverio"]
"types": ["node", "webdriverio"]
},
"include": [
"./src/**/*.ts"
Expand Down Expand Up @@ -109,10 +109,28 @@ export { config }

## Framework types

Depending on the framework you use, you will need to add the types for that framework to your `tsconfig.json` types property, as well as install its type definitions.
Depending on the framework you use, you will need to add the types for that framework to your `tsconfig.json` types property, as well as install its type definitions. This is especially important if you want to have type support for the built-in assertion library [`expect-webdriverio`](https://www.npmjs.com/package/expect-webdriverio).

For instance, if you decide to use the Mocha framework, you need to install `@types/mocha` and add it like this to have all types globally available:

<!--DOCUSAURUS_CODE_TABS-->
<!--Mocha-->
```json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [ "./*" ],
"src/*": ["./src/*"]
},
"types": ["node", "webdriverio", "@wdio/mocha-framework"]
},
"include": [
"./src/**/*.ts"
]
}
```
<!--Jasmine-->
```json
{
"compilerOptions": {
Expand All @@ -121,13 +139,30 @@ For instance, if you decide to use the Mocha framework, you need to install `@ty
"*": [ "./*" ],
"src/*": ["./src/*"]
},
"types": ["node", "webdriverio", "expect-webdriverio", "@wdio/mocha-framework"]
"types": ["node", "webdriverio", "@wdio/jasmine-framework"]
},
"include": [
"./src/**/*.ts"
]
}
```
<!--Cucumber-->
```json
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"*": [ "./*" ],
"src/*": ["./src/*"]
},
"types": ["node", "webdriverio", "@wdio/cucumber-framework"]
},
"include": [
"./src/**/*.ts"
]
}
```
<!--END_DOCUSAURUS_CODE_TABS-->

Instead of having all type definitions globally available, you can also `import` only the types that you need, like this:

Expand Down
2 changes: 2 additions & 0 deletions packages/wdio-cucumber-framework/cucumber-framework.d.ts
@@ -1,3 +1,5 @@
/// <reference types="expect-webdriverio"/>

import { SourceLocation, ScenarioResult } from "cucumber";

declare module "webdriverio" {
Expand Down
1 change: 1 addition & 0 deletions packages/wdio-cucumber-framework/package.json
Expand Up @@ -33,6 +33,7 @@
"@wdio/logger": "6.0.16",
"@wdio/utils": "6.1.0",
"cucumber": "^6.0.5",
"expect-webdriverio": "^1.1.5",
"glob": "^7.1.2",
"is-glob": "^4.0.0",
"mockery": "~2.1.0"
Expand Down
11 changes: 10 additions & 1 deletion packages/wdio-cucumber-framework/src/index.js
Expand Up @@ -70,9 +70,18 @@ class CucumberAdapter {
}

async run () {
/**
* import and set options for `expect-webdriverio` assertion lib once
* the framework was initiated so that it can detect the environment
*/
const { setOptions } = require('expect-webdriverio')
setOptions({
wait: this.config.waitforTimeout, // ms to wait for expectation to succeed
interval: this.config.waitforInterval, // interval between attempts
})

let runtimeError
let result

try {
this.registerRequiredModules()
Cucumber.supportCodeLibraryBuilder.reset(this.cwd)
Expand Down
4 changes: 4 additions & 0 deletions packages/wdio-cucumber-framework/tests/adapter.test.js
Expand Up @@ -2,6 +2,8 @@ import path from 'path'
import mockery from 'mockery'
import * as Cucumber from 'cucumber'
import * as utils from '@wdio/utils'
import { setOptions } from 'expect-webdriverio'

const { executeHooksWithArgs, testFnWrapper } = utils

import CucumberAdapterFactory, { CucumberAdapter } from '../src'
Expand Down Expand Up @@ -52,6 +54,7 @@ test('should properly set up cucumber', async () => {
const result = await adapter.run()
expect(result).toBe(0)

expect(setOptions).toBeCalledTimes(1)
expect(adapter.registerRequiredModules).toBeCalled()
expect(adapter.loadSpecFiles).toBeCalled()
expect(adapter.wrapSteps).toBeCalled()
Expand Down Expand Up @@ -441,4 +444,5 @@ afterEach(() => {
mockery.enable.mockClear()
mockery.registerMock.mockClear()
mockery.disable.mockClear()
setOptions.mockClear()
})
1 change: 1 addition & 0 deletions packages/wdio-jasmine-framework/jasmine-framework.d.ts
@@ -1,4 +1,5 @@
/// <reference types="jasmine"/>
/// <reference types="expect-webdriverio/jasmine"/>

declare module WebdriverIO {
interface Config extends JasmineNodeOptsConfig {}
Expand Down
1 change: 1 addition & 0 deletions packages/wdio-jasmine-framework/package.json
Expand Up @@ -38,6 +38,7 @@
"dependencies": {
"@wdio/logger": "6.0.16",
"@wdio/utils": "6.1.0",
"expect-webdriverio": "^1.1.5",
"jasmine": "^3.5.0"
},
"peerDependencies": {
Expand Down
10 changes: 10 additions & 0 deletions packages/wdio-jasmine-framework/src/index.js
Expand Up @@ -182,6 +182,16 @@ class JasmineAdapter {
}

async run () {
/**
* import and set options for `expect-webdriverio` assertion lib once
* the framework was initiated so that it can detect the environment
*/
const { setOptions } = require('expect-webdriverio')
setOptions({
wait: this.config.waitforTimeout, // ms to wait for expectation to succeed
interval: this.config.waitforInterval, // interval between attempts
})

const result = await new Promise((resolve) => {
this.jrunner.env.beforeAll(this.wrapHook('beforeSuite'))
this.jrunner.env.afterAll(this.wrapHook('afterSuite'))
Expand Down
4 changes: 4 additions & 0 deletions packages/wdio-jasmine-framework/tests/adapter.test.js
@@ -1,5 +1,6 @@
import logger from '@wdio/logger'
import { runTestInFiberContext, executeHooksWithArgs } from '@wdio/utils'
import { setOptions } from 'expect-webdriverio'

import JasmineAdapterFactory, { JasmineAdapter } from '../src'

Expand Down Expand Up @@ -49,7 +50,9 @@ test('should properly set up jasmine', async () => {
const adapter = adapterFactory()
await adapter.init()
const result = await adapter.run()

expect(result).toBe(0)
expect(setOptions).toBeCalledTimes(1)
expect(adapter.jrunner.addSpecFiles.mock.calls[0][0]).toEqual(['/foo/bar.test.js'])
expect(adapter.jrunner.jasmine.addReporter.mock.calls).toHaveLength(1)
expect(executeHooksWithArgs.mock.calls).toHaveLength(1)
Expand Down Expand Up @@ -465,6 +468,7 @@ describe('hasTests', () => {
})

afterEach(() => {
setOptions.mockClear()
runTestInFiberContext.mockClear()
executeHooksWithArgs.mockClear()
})
1 change: 1 addition & 0 deletions packages/wdio-mocha-framework/mocha-framework.d.ts
@@ -1,4 +1,5 @@
/// <reference types="mocha"/>
/// <reference types="expect-webdriverio"/>

declare module WebdriverIO {
interface Config extends MochaOptsConfig {}
Expand Down
1 change: 1 addition & 0 deletions packages/wdio-mocha-framework/package.json
Expand Up @@ -32,6 +32,7 @@
"dependencies": {
"@wdio/logger": "6.0.16",
"@wdio/utils": "6.1.0",
"expect-webdriverio": "^1.1.5",
"mocha": "^7.0.1"
},
"devDependencies": {
Expand Down
11 changes: 11 additions & 0 deletions packages/wdio-mocha-framework/src/index.js
Expand Up @@ -86,6 +86,17 @@ class MochaAdapter {

async run () {
const mocha = this.mocha

/**
* import and set options for `expect-webdriverio` assertion lib once
* the framework was initiated so that it can detect the environment
*/
const { setOptions } = require('expect-webdriverio')
setOptions({
wait: this.config.waitforTimeout, // ms to wait for expectation to succeed
interval: this.config.waitforInterval, // interval between attempts
})

let runtimeError
const result = await new Promise((resolve) => {
try {
Expand Down
3 changes: 3 additions & 0 deletions packages/wdio-mocha-framework/tests/adapter.test.js
Expand Up @@ -2,6 +2,7 @@ import path from 'path'
import Mocha from 'mocha'
import logger from '@wdio/logger'
import { runTestInFiberContext, executeHooksWithArgs } from '@wdio/utils'
import { setOptions } from 'expect-webdriverio'

import MochaAdapterFactory, { MochaAdapter } from '../src'
import { loadModule } from '../src/utils'
Expand Down Expand Up @@ -49,6 +50,7 @@ test('should properly set up mocha', async () => {
const result = await adapter.run()
expect(result).toBe(0)

expect(setOptions).toBeCalledTimes(1)
expect(adapter.mocha.loadFiles).toBeCalled()
expect(adapter.mocha.reporter).toBeCalled()
expect(adapter.mocha.fullTrace).toBeCalled()
Expand Down Expand Up @@ -370,4 +372,5 @@ afterEach(() => {
Mocha.Runner.mockClear()
runTestInFiberContext.mockReset()
executeHooksWithArgs.mockReset()
setOptions.mockClear()
})
1 change: 0 additions & 1 deletion packages/wdio-runner/package.json
Expand Up @@ -34,7 +34,6 @@
"@wdio/logger": "6.0.16",
"@wdio/utils": "6.1.0",
"deepmerge": "^4.0.0",
"expect-webdriverio": "^1.0.0",
"gaze": "^1.1.2",
"webdriver": "6.1.4",
"webdriverio": "6.1.5"
Expand Down
10 changes: 0 additions & 10 deletions packages/wdio-runner/src/index.js
Expand Up @@ -134,16 +134,6 @@ export default class Runner extends EventEmitter {
content: { sessionId, isW3C, protocol, hostname, port, path, queryParams, isMultiremote, instances }
})

/**
* import and set options for `expect-webdriverio` assertion lib once
* the framework was initiated so that it can detect the environment
*/
const { setOptions } = require('expect-webdriverio')
setOptions({
wait: this.config.waitforTimeout, // ms to wait for expectation to succeed
interval: this.config.waitforInterval, // interval between attempts
})

/**
* kick off tests in framework
*/
Expand Down
6 changes: 0 additions & 6 deletions packages/wdio-runner/tests/index.test.js
@@ -1,6 +1,5 @@
import fs from 'fs'

import { setOptions } from 'expect-webdriverio'
import { executeHooksWithArgs } from '@wdio/utils'
import { attach } from 'webdriverio'
import WDIORunner from '../src'
Expand All @@ -10,10 +9,6 @@ jest.mock('fs')
jest.mock('util', () => ({ promisify: (fn) => fn }))

describe('wdio-runner', () => {
it('comes with own assertion lib', () => {
expect(process.env.WDIO_ASSERTION_LIB_ACTIVATED).toBe('1')
})

describe('_fetchDriverLogs', () => {
let runner

Expand Down Expand Up @@ -213,7 +208,6 @@ describe('wdio-runner', () => {
specs
})

expect(setOptions).toBeCalledTimes(1)
expect(runner._shutdown).toBeCalledWith(123)
expect(beforeSession).toBeCalledWith(config, caps, specs)
expect(executeHooksWithArgs).toBeCalledWith(config.before, [caps, specs])
Expand Down
4 changes: 4 additions & 0 deletions tests/typings/sync-cucumber/cucumber.ts
Expand Up @@ -13,6 +13,10 @@ const hook: WebdriverIO.HookFunctions = {
afterScenario: function (uri, feature, scenario, result, sourceLocation) {
},
afterFeature: function (uri, feature, scenarios) {
expect(browser).toHaveTitle('some title')

const el = $('selector')
expect(el).toHaveTextContaining('foobar')
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/typings/sync-jasmine/jasmine.ts
Expand Up @@ -13,4 +13,13 @@ const config: WebdriverIO.Config = {
}
}

describe('foo', () => {
it('bar', () => {
expect(browser).toHaveTitle('foobar')

const el = $('selector')
expect(el).toHaveTextContaining('foobar')
})
})

export default {}
9 changes: 9 additions & 0 deletions tests/typings/sync-mocha/mocha.ts
Expand Up @@ -16,4 +16,13 @@ const config: WebdriverIO.Config = {
}
}

describe('foo', () => {
it('bar', () => {
expect(browser).toHaveTitle('some title')

const el = $('selector')
expect(el).toHaveTextContaining('foobar')
})
})

export default {}

0 comments on commit 5bb5399

Please sign in to comment.