Skip to content

Commit

Permalink
feat(protractor): expectation to check if an element isActive()
Browse files Browse the repository at this point in the history
  • Loading branch information
jan-molak committed Feb 17, 2020
1 parent 4e99dd2 commit bb7f6c5
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
56 changes: 56 additions & 0 deletions packages/protractor/spec/expectations/isActive.spec.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions packages/protractor/src/expectations/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './isActive';
export * from './isClickable';
export * from './isEnabled';
export * from './isPresent';
Expand Down
23 changes: 23 additions & 0 deletions packages/protractor/src/expectations/isActive.ts
Original file line number Diff line number Diff line change
@@ -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.