Skip to content

Commit

Permalink
feat: add clickable, displayedInViewport, selected
Browse files Browse the repository at this point in the history
  • Loading branch information
jedwards1211 committed Dec 18, 2020
1 parent d234d07 commit a47781a
Show file tree
Hide file tree
Showing 20 changed files with 187 additions and 326 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ All assertions start with a [WebdriverIO-compatible selector](http://webdriver.i

Then, we can add our assertion to the chain.

- `await expect(selector).to.be.existing()` - Test whether [at least one] matching element exists in the DOM
- `await expect(selector).to.be.clickable()` - Test whether [at least one] matching element is clickable
- `await expect(selector).to.be.displayed()` - Test whether or not [at least one] matching element is displayed
- `await expect(selector).to.be.displayedInViewport()` - Test whether or not [at least one] matching element is displayed in viewport
- `await expect(selector).to.be.existing()` - Test whether [at least one] matching element exists in the DOM
- `await expect(selector).to.be.focused()` - Test whether or not [at least one] matching element is focused
- `await expect(selector).to.be.selected()` - Test whether or not [at least one] matching element is selected
- `await expect(selector).to.have.text('string')` - Test the text value of the selected element(s) against supplied string. Succeeds if at least one element matches exactly
- `await expect(selector).to.have.text(/regex/)` - Test the text value of the selected element(s) against the supplied regular expression. Succeeds if at least one element matches
- `await expect(selector).to.have.count(number)` - Test how many elements exist in the DOM with the supplied selector
Expand Down
12 changes: 0 additions & 12 deletions chains/immediately.js

This file was deleted.

5 changes: 0 additions & 5 deletions chains/immediately.js.flow

This file was deleted.

26 changes: 0 additions & 26 deletions src/assertions/atLeastOne.js

This file was deleted.

8 changes: 4 additions & 4 deletions src/assertions/booleanAssertion.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const booleanAssertion = ({ predicate, expectation, allowNone }) => (
if (!allowNone && !elements.length) {
throw new chai.AssertionError(
negate
? `Expected <${selector}> to not be ${expectation} but no matching elements were found`
: `Expected <${selector}> to be ${expectation} but no matching elements were found`
? `Expected element <${selector}> to not be ${expectation} but no matching elements were found`
: `Expected element <${selector}> to be ${expectation} but no matching elements were found`
)
}

Expand All @@ -29,8 +29,8 @@ const booleanAssertion = ({ predicate, expectation, allowNone }) => (

this.assert(
filteredList.length > 0,
`Expected <${selector}> to be ${expectation} but it is not`,
`Expected <${selector}> to not be ${expectation} but it is`
`Expected element <${selector}> to be ${expectation} but it is not`,
`Expected element <${selector}> to not be ${expectation} but it is`
)
}

Expand Down
12 changes: 12 additions & 0 deletions src/assertions/clickable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* NOTICE
* This file has been modified from the source in
* https://github.com/marcodejongh/chai-webdriverio
*/

import booleanAssertion from './booleanAssertion'

export default booleanAssertion({
predicate: el => el.isClickable(),
expectation: 'clickable',
})
13 changes: 13 additions & 0 deletions src/assertions/displayedInViewport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* NOTICE
* This file has been modified from the source in
* https://github.com/marcodejongh/chai-webdriverio
*/

import booleanAssertion from './booleanAssertion'

export default booleanAssertion({
predicate: el => el.isDisplayedInViewport(),
expectation: 'displayed in viewport',
allowNone: true,
})
13 changes: 13 additions & 0 deletions src/assertions/selected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* NOTICE
* This file has been modified from the source in
* https://github.com/marcodejongh/chai-webdriverio
*/

import booleanAssertion from './booleanAssertion'

export default booleanAssertion({
predicate: el => el.isSelected(),
expectation: 'selected',
allowNone: false,
})
24 changes: 15 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import existing from './assertions/existing'
import displayed from './assertions/displayed'
import clickable from './assertions/clickable'
import count from './assertions/count'
import displayed from './assertions/displayed'
import displayedInViewport from './assertions/displayedInViewport'
import enabled from './assertions/enabled'
import existing from './assertions/existing'
import focused from './assertions/focused'
import selected from './assertions/selected'
import text from './assertions/text'
import value from './assertions/value'
import focused from './assertions/focused'
import enabled from './assertions/enabled'

export default function(client, options = {}) {
return function chaiWebdriverIO(chai, utils) {
const methodsToAdd = {
existing,
displayed,
clickable,
count,
text,
value,
displayed,
displayedInViewport,
enabled,
existing,
focus: focused,
focused,
enabled,
selected,
text,
value,
}

for (const name in methodsToAdd) {
Expand Down
86 changes: 86 additions & 0 deletions test/assertions/booleanAssertionTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import chai, { expect } from 'chai'

import { describe, beforeEach, afterEach, it } from 'mocha'
import FakeClient from '../stubs/fake-client'
import FakeElement from '../stubs/fake-element'
import chaiWebdriverio from '../../src'
import { upperFirst, lowerCase } from 'lodash'

export const booleanAssertionTest = ({
method,
expectation = lowerCase(method),
allowNone,
}) =>
describe(method, () => {
let fakeClient
let fakeElement1

beforeEach(() => {
fakeClient = new FakeClient()
fakeElement1 = new FakeElement()

fakeElement1[`is${upperFirst(method)}`].resolves(false)
fakeClient.$$.withArgs('.some-selector').resolves([fakeElement1])
fakeClient.$$.withArgs('.other-selector').resolves([])

chai.use(chaiWebdriverio(fakeClient))
})

afterEach(() => {
fakeClient.__resetStubs__()
fakeElement1.__resetStubs__()
})

describe('When not negated', () => {
it(`resolves when element is ${expectation}`, async function() {
fakeElement1[`is${upperFirst(method)}`].resolves(true)
await expect('.some-selector').to.be[method]()
})
it(`rejects when element is not ${expectation}`, async function() {
await expect('.some-selector')
.to.be[method]()
.to.be.rejectedWith(
`Expected element <.some-selector> to be ${expectation} but it is not`
)
})
it(`rejects when element does not exist`, async function() {
await expect('.other-selector')
.to.be[method]()
.to.be.rejectedWith(
allowNone
? `Expected element <.other-selector> to be ${expectation} but it is not`
: `Expected element <.other-selector> to be ${expectation} but no matching elements were found`
)
})
})
describe('When negated', () => {
it(`rejects when element is ${expectation}`, async function() {
fakeElement1[`is${upperFirst(method)}`].resolves(true)
await expect(
expect('.some-selector')
.not.to.be[method]()
.then(null)
).to.be.rejectedWith(
`Expected element <.some-selector> to not be ${expectation} but it is`
)
})
it(`resolves when element is not ${expectation}`, async function() {
await expect('.some-selector').not.to.be[method]()
})
if (allowNone) {
it(`resolves when element does not exist`, async function() {
await expect(expect('.other-selector').not.to.be[method]())
})
} else {
it(`rejects when element does not exist`, async function() {
await expect(
expect('.other-selector')
.not.to.be[method]()
.then(null)
).to.be.rejectedWith(
`Expected element <.other-selector> to not be ${expectation} but no matching elements were found`
)
})
}
})
})
9 changes: 9 additions & 0 deletions test/assertions/clickable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* NOTICE
* This file has been modified from the source in
* https://github.com/marcodejongh/chai-webdriverio
*/

import { booleanAssertionTest } from './booleanAssertionTest'

booleanAssertionTest({ method: 'clickable' })
65 changes: 2 additions & 63 deletions test/assertions/displayed-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,6 @@
* https://github.com/marcodejongh/chai-webdriverio
*/

import chai, { expect } from 'chai'
import FakeClient from '../stubs/fake-client'
import chaiWebdriverio from '../../src/index'
import FakeElement from '../stubs/fake-element'
import { booleanAssertionTest } from './booleanAssertionTest'

describe('displayed', () => {
let fakeClient
let fakeElement1

beforeEach(() => {
fakeClient = new FakeClient()
fakeElement1 = new FakeElement()

fakeElement1.isDisplayed.resolves(false)
fakeClient.$$.withArgs('.some-selector').resolves([fakeElement1])
fakeClient.$$.withArgs('.other-selector').resolves([])

chai.use(chaiWebdriverio(fakeClient))
})

afterEach(() => {
fakeClient.__resetStubs__()
fakeElement1.__resetStubs__()
})

describe('When not negated', () => {
it(`resolves when element is displayed`, async function() {
fakeElement1.isDisplayed.resolves(true)
await expect('.some-selector').to.be.displayed()
})
it(`rejects when element is not displayed`, async function() {
await expect('.some-selector')
.to.be.displayed()
.to.be.rejectedWith(
'Expected <.some-selector> to be displayed but it is not'
)
})
it(`rejects when element does not exist`, async function() {
await expect('.other-selector')
.to.be.displayed()
.to.be.rejectedWith(
'Expected <.other-selector> to be displayed but it is not'
)
})
})
describe('When negated', () => {
it(`rejects when element is displayed`, async function() {
fakeElement1.isDisplayed.resolves(true)
await expect(
expect('.some-selector')
.not.to.be.displayed()
.then(null)
).to.be.rejectedWith(
'Expected <.some-selector> to not be displayed but it is'
)
})
it(`resolves when element is not displayed`, async function() {
await expect('.some-selector').not.to.be.displayed()
})
it(`resolves when element does not exist`, async function() {
await expect('.other-selector').not.to.be.displayed()
})
})
})
booleanAssertionTest({ method: 'displayed', allowNone: true })
9 changes: 9 additions & 0 deletions test/assertions/displayedInViewport-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* NOTICE
* This file has been modified from the source in
* https://github.com/marcodejongh/chai-webdriverio
*/

import { booleanAssertionTest } from './booleanAssertionTest'

booleanAssertionTest({ method: 'displayedInViewport', allowNone: true })
Loading

0 comments on commit a47781a

Please sign in to comment.