Skip to content

Commit

Permalink
added support for expect.element().an() to accept case-insensitive ta…
Browse files Browse the repository at this point in the history
…g names and regular expressions
  • Loading branch information
beatfactor committed Feb 1, 2022
1 parent c33f630 commit efb6f10
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/api/expect/assertions/element/type.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TypeAssertion extends BaseAssertion {
super.init();

this.type = type;
this.article = ['a', 'e', 'i', 'o'].indexOf(type.substring(0, 1)) > -1 ? 'an' : 'a';
this.article = ['a', 'e', 'i', 'o'].indexOf(type.toString().substring(0, 1)) > -1 ? 'an' : 'a';
this.hasCustomMessage = typeof msg != 'undefined';
this.flag('typeFlag', true);
this.message = msg || 'Expected element %s to ' + (this.negate ? 'not be' : 'be') + ' ' + this.article +' ' + type;
Expand All @@ -48,7 +48,15 @@ class TypeAssertion extends BaseAssertion {
return;
}

this.passed = this.negate ? (this.resultValue !== this.type) : (this.resultValue === this.type);
if (this.type instanceof RegExp) {
const result = this.type.test(this.resultValue);
this.passed = this.negate ? !result : result;
} else {
this.type = this.type.toLowerCase();
this.resultValue = this.resultValue.toLowerCase();
this.passed = this.negate ? (this.resultValue !== this.type) : (this.resultValue === this.type);
}

this.expected = this.negate ? 'not be ' + this.article + ' ' + this.type : 'be ' + this.article + ' ' + this.type;
this.actual = this.resultValue;

Expand Down
44 changes: 44 additions & 0 deletions test/src/api/expect/testExpectType.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,50 @@ describe('expect.type', function() {
});
});

it('to not be [PASSED] with upper case', function() {
Nocks.elementFound().name('INPUT');
this.client.api.globals.waitForConditionTimeout = 40;
this.client.api.globals.waitForConditionPollInterval = 20;
let expect = this.client.api.expect.element('#weblogin').to.be.an('input');

return this.client.start(function() {
strictEqual(expect.assertion.passed, true);
});
});

it('to not be [FAILED] with upper case', function() {
Nocks.elementFound().name('INPUT');
this.client.api.globals.waitForConditionTimeout = 40;
this.client.api.globals.waitForConditionPollInterval = 20;
let expect = this.client.api.expect.element('#weblogin').to.not.be.an('input');

return this.client.start(function() {
strictEqual(expect.assertion.passed, false);
});
});

it('to not be [PASSED] with regex', function() {
Nocks.elementFound().name('INPUT');
this.client.api.globals.waitForConditionTimeout = 40;
this.client.api.globals.waitForConditionPollInterval = 20;
let expect = this.client.api.expect.element('#weblogin').to.be.an(/^input/i);

return this.client.start(function() {
strictEqual(expect.assertion.passed, true);
});
});

it('to not be [FAILED] with regex', function() {
Nocks.elementFound().name('INPUT');
this.client.api.globals.waitForConditionTimeout = 40;
this.client.api.globals.waitForConditionPollInterval = 20;
let expect = this.client.api.expect.element('#weblogin').to.not.be.an(/^input/i);

return this.client.start(function() {
strictEqual(expect.assertion.passed, false);
});
});

it('to not be - element not found', function() {
this.client.api.globals.waitForConditionTimeout = 40;
this.client.api.globals.waitForConditionPollInterval = 20;
Expand Down

0 comments on commit efb6f10

Please sign in to comment.