Skip to content

Commit

Permalink
feat(web): actors can now check if an HTML element attribute is present
Browse files Browse the repository at this point in the history
some attributes, like the anchor "download" attribute, do not have a value, so it's more
semantically correct to check if they're present rather than that they have an empty value
  • Loading branch information
jan-molak committed Feb 13, 2024
1 parent a60f6d1 commit 3ce115a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
@@ -1,7 +1,7 @@
import 'mocha';

import { expect } from '@integration/testing-tools';
import { Ensure, equals } from '@serenity-js/assertions';
import { Ensure, equals, isPresent, not } from '@serenity-js/assertions';
import { actorCalled, LogicError } from '@serenity-js/core';
import { Attribute, By, Navigate, PageElement, PageElements, Text } from '@serenity-js/web';

Expand All @@ -25,6 +25,16 @@ describe('Attribute', () => {
Ensure.that(Attribute.called('lang').of(dom), equals('en')),
));

it('allows the actor to check if an attribute of a DOM element is present', () =>
actorCalled('Wendy').attemptsTo(
Ensure.that(Attribute.called('lang').of(dom), isPresent()),
));

it('allows the actor to check if an attribute of a DOM element is not present', () =>
actorCalled('Wendy').attemptsTo(
Ensure.that(Attribute.called('data-invalid').of(dom), not(isPresent())),
));

it('produces a sensible description of the question being asked', () => {
expect(Attribute.called('lang').of(dom).toString())
.to.equal(`'lang' attribute of DOM`);
Expand Down
14 changes: 12 additions & 2 deletions packages/web/src/screenplay/questions/Attribute.ts
@@ -1,4 +1,4 @@
import type { Answerable, AnswersQuestions, MetaQuestion, MetaQuestionAdapter,QuestionAdapter, UsesAbilities } from '@serenity-js/core';
import type { Answerable, AnswersQuestions, MetaQuestion, MetaQuestionAdapter, Optional, QuestionAdapter, UsesAbilities } from '@serenity-js/core';
import { d, LogicError, Question } from '@serenity-js/core';

import { PageElement } from '../models';
Expand Down Expand Up @@ -97,7 +97,7 @@ import { PageElement } from '../models';
*/
export class Attribute<Native_Element_Type>
extends Question<Promise<string>>
implements MetaQuestion<PageElement<Native_Element_Type>, Question<Promise<string>>>
implements MetaQuestion<PageElement<Native_Element_Type>, Question<Promise<string>>>, Optional
{
private subject: string;

Expand Down Expand Up @@ -157,6 +157,16 @@ export class Attribute<Native_Element_Type>
return element.attribute(name);
}

/**
* @inheritDoc
*/
isPresent(): QuestionAdapter<boolean> {
return Question.about(this.subject, async actor => {
const attribute = await this.answeredBy(actor);
return attribute !== null && attribute !== undefined;
});
}

/**
* @inheritDoc
*/
Expand Down

0 comments on commit 3ce115a

Please sign in to comment.