Skip to content
Permalink
Browse files
feat(protractor): expectation to check if an element isActive()
  • Loading branch information
jan-molak committed Feb 17, 2020
1 parent 4e99dd2 commit bb7f6c58e481b37793123ce0ba2ff1177240ba8b
Showing 3 changed files with 80 additions and 0 deletions.
@@ -0,0 +1,56 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Ensure, not } from '@serenity-js/assertions';
import { actorCalled, AssertionError } from '@serenity-js/core';
import { by } from 'protractor';

import { Click, isActive, Navigate, Target, Wait } from '../../src';
import { pageFromTemplate } from '../fixtures';

describe('isActive', function () {

const Page = {
Active_Input: Target.the('active input').located(by.id('active')),
Inactive_Input: Target.the('inactive input').located(by.id('inactive')),
};

beforeEach(() => actorCalled('Bernie').attemptsTo(
Navigate.to(pageFromTemplate(`
<html>
<body>
<input type="text" id="active" />
<input type="text" id="inactive" />
</body>
</html>
`)),
));

/** @test {isActive} */
it('allows the actor flow to continue when the element is active', () => expect(actorCalled('Bernie').attemptsTo(
Wait.until(Page.Active_Input, not(isActive())),
Ensure.that(Page.Active_Input, not(isActive())),
Click.on(Page.Active_Input),
Wait.until(Page.Active_Input, isActive()),
Ensure.that(Page.Active_Input, isActive()),
)).to.be.fulfilled);

/** @test {isActive} */
it('breaks the actor flow when element is inactive', () => {
return expect(actorCalled('Bernie').attemptsTo(
Ensure.that(Page.Inactive_Input, isActive()),
)).to.be.rejectedWith(AssertionError, `Expected the inactive input to become active`);
});

/** @test {isActive} */
it('contributes to a human-readable description of an assertion', () => {
expect(Ensure.that(Page.Active_Input, isActive()).toString())
.to.equal(`#actor ensures that the active input does become active`);
});

/** @test {isActive} */
it('contributes to a human-readable description of a wait', () => {
expect(Wait.until(Page.Active_Input, isActive()).toString())
.to.equal(`#actor waits up to 5s until the active input does become active`);
});
});
@@ -1,3 +1,4 @@
export * from './isActive';
export * from './isClickable';
export * from './isEnabled';
export * from './isPresent';
@@ -0,0 +1,23 @@
import { Expectation } from '@serenity-js/assertions';
import { ElementFinder, WebElement } from 'protractor';
import { ElementFinderExpectation } from './ElementFinderExpectation';

/**
* @desc
* Expectation that the element is active.
*
* @returns {Expectation<boolean, ElementFinder>}
*
* @see {@link @serenity-js/assertions~Ensure}
* @see {@link @serenity-js/assertions~Check}
* @see {@link Wait}
*/
export function isActive(): Expectation<boolean, ElementFinder> {
return ElementFinderExpectation.forElementTo('become active', (actual: ElementFinder) =>
actual.getWebElement().then(element =>
element.getDriver().switchTo().activeElement().then((active: WebElement) =>
actual.equals(active),
),
),
);
}

0 comments on commit bb7f6c5

Please sign in to comment.