diff --git a/integration/web-specs/spec/screenplay/questions/Attribute.spec.ts b/integration/web-specs/spec/screenplay/questions/Attribute.spec.ts index e587b133acb..55954a79b48 100644 --- a/integration/web-specs/spec/screenplay/questions/Attribute.spec.ts +++ b/integration/web-specs/spec/screenplay/questions/Attribute.spec.ts @@ -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'; @@ -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`); diff --git a/packages/web/src/screenplay/questions/Attribute.ts b/packages/web/src/screenplay/questions/Attribute.ts index 93bc6c97654..9beb88cac32 100644 --- a/packages/web/src/screenplay/questions/Attribute.ts +++ b/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'; @@ -97,7 +97,7 @@ import { PageElement } from '../models'; */ export class Attribute extends Question> - implements MetaQuestion, Question>> + implements MetaQuestion, Question>>, Optional { private subject: string; @@ -157,6 +157,16 @@ export class Attribute return element.attribute(name); } + /** + * @inheritDoc + */ + isPresent(): QuestionAdapter { + return Question.about(this.subject, async actor => { + const attribute = await this.answeredBy(actor); + return attribute !== null && attribute !== undefined; + }); + } + /** * @inheritDoc */