Skip to content

Commit

Permalink
Add type and types test
Browse files Browse the repository at this point in the history
  • Loading branch information
subhajit20 committed Feb 22, 2024
1 parent e7c2c5b commit 0d95b73
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 39 deletions.
6 changes: 3 additions & 3 deletions lib/api/web-element/commands/isEnabled.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* @example
* export default {
* demoTest(browser: NightwatchAPI): void {
* const result = browser.element('#main ul li a.first').getText();
* const result = browser.element('#main ul li a.first').isEnabled();
* .assert.valueEquals('custom text');
* },
*
* async demoTestAsync(browser: NightwatchAPI): Promise<void> {
* const result = await browser.element('#main ul li a.first').getText();
* const result = await browser.element('#main ul li a.first').isEnabled();
* console.log('element text:', result);
* }
* }
Expand All @@ -20,7 +20,7 @@
* @method getText
* @memberof ScopedWebElement
* @instance
* @syntax browser.element(selector).getText()
* @syntax browser.element(selector).isEnabled()
* @see https://www.w3.org/TR/webdriver#dfn-get-element-text
* @returns {ScopedValue<string>}
*/
Expand Down
138 changes: 138 additions & 0 deletions test/src/api/commands/web-element/testIsEnabled.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
const assert = require('assert');
const {WebElement} = require('selenium-webdriver');
const MockServer = require('../../../../lib/mockserver.js');
const CommandGlobals = require('../../../../lib/globals/commands-w3c.js');
const common = require('../../../../common.js');
const Element = common.require('element/index.js');

describe('element().isEnabled() command', function() {
before(function(done) {
CommandGlobals.beforeEach.call(this, done);
});

after(function(done) {
CommandGlobals.afterEach.call(this, done);
});

it('test .element().isEnabled()', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/property/value',
method: 'GET',
response: JSON.stringify({
value: ''
})
},
true
);

const resultPromise = this.client.api.element('#signupSection').getValue();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, '');

const resultValue = await resultPromise.value;
assert.strictEqual(resultValue, '');
});

it('test .element().find().isEnabled()', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/1/property/value',
method: 'GET',
response: JSON.stringify({
value: 'Help'
})
},
true
);

const resultPromise = this.client.api
.element('#signupSection')
.find('#helpBtn')
.getValue();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, 'Help');

const resultValue = await resultPromise.value;
assert.strictEqual(resultValue, 'Help');
});

it('test .element.find().isEnabled()', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/property/value',
method: 'GET',
response: JSON.stringify({
value: ''
})
},
true
);

const resultPromise = this.client.api.element
.find('#signupSection')
.getValue();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

const result = await resultPromise;
assert.strictEqual(result instanceof WebElement, false);
assert.strictEqual(result, '');

const resultValue = await resultPromise.value;
assert.strictEqual(resultValue, '');
});

it('test .element().isEnabled() assert', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/0/property/value',
method: 'GET',
response: JSON.stringify({
value: 'Signup'
})
},
true
);

const resultPromise = this.client.api.element('#signupSection').getValue();
assert.strictEqual(resultPromise instanceof Element, false);
assert.strictEqual(typeof resultPromise.find, 'undefined');

assert.strictEqual(resultPromise instanceof Promise, false);
assert.strictEqual(typeof resultPromise.then, 'function');

assert.strictEqual(await resultPromise.assert.equals('Signup'), 'Signup');
assert.strictEqual(await resultPromise.assert.contains('Sign'), 'Signup');
assert.strictEqual(
await resultPromise.assert.matches(/Si[a-z]{2}up/),
'Signup'
);

assert.strictEqual(
await resultPromise.assert.not.equals('Signupx'),
'Signup'
);
assert.strictEqual(
await resultPromise.assert.not.contains('Signupx'),
'Signup'
);
assert.strictEqual(
await resultPromise.assert.not.matches(/Si[a-z]{2}upx/),
'Signup'
);
});
});
61 changes: 31 additions & 30 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectAssignable, expectError, expectType} from "tsd";
import { expectAssignable, expectError, expectType } from "tsd";
import { Element, ElementAssertions, ElementValue, Elements, ScopedElement, ScopedElementRect, ValueAssertions } from "..";
import { WebElement, WebElementPromise } from "selenium-webdriver";

Expand All @@ -18,7 +18,7 @@ describe('new element() api', function () {
expectType<ScopedElement>(browser.element(locateWith(by.css('selector')).above(await elem)));

// accepts ElementProperties
expectType<ScopedElement>(browser.element({selector: 'selector', locateStrategy: 'css selector', abortOnFailure: false}));
expectType<ScopedElement>(browser.element({ selector: 'selector', locateStrategy: 'css selector', abortOnFailure: false }));

// accepts Element (ScopedElement is also assignable to Element)
expectType<ScopedElement>(browser.element(elem));
Expand All @@ -44,21 +44,21 @@ describe('new element() api', function () {
expectType<ScopedElement>(elementFind);
expectType<WebElement>(await elementFind);

const elementFindByText = browser.element.findByText('some-text', {exact: true, abortOnFailure: false});
const elementFindByText = browser.element.findByText('some-text', { exact: true, abortOnFailure: false });
expectType<ScopedElement>(elementFindByText);

const elementFindByRole = browser.element.findByRole('heading', {level: 2, expanded: true, retryInterval: 100});
const elementFindByRole = browser.element.findByRole('heading', { level: 2, expanded: true, retryInterval: 100 });
expectType<ScopedElement>(elementFindByRole);
browser.element.findByRole('button', {current: false, expanded: true, index: 2});
expectError(browser.element.findByRole('button', {level: 2, expanded: true, retryInterval: 100}));
browser.element.findByRole('button', { current: false, expanded: true, index: 2 });
expectError(browser.element.findByRole('button', { level: 2, expanded: true, retryInterval: 100 }));

const elementFindByPlaceholderText = browser.element.findByPlaceholderText('some-text', {exact: true, abortOnFailure: false});
const elementFindByPlaceholderText = browser.element.findByPlaceholderText('some-text', { exact: true, abortOnFailure: false });
expectType<ScopedElement>(elementFindByPlaceholderText);

const elementFindByLabelText = browser.element.findByLabelText('some-text', {exact: true, abortOnFailure: false});
const elementFindByLabelText = browser.element.findByLabelText('some-text', { exact: true, abortOnFailure: false });
expectType<ScopedElement>(elementFindByLabelText);

const elementFindByAltText = browser.element.findByAltText('some-text', {exact: false, abortOnFailure: false});
const elementFindByAltText = browser.element.findByAltText('some-text', { exact: false, abortOnFailure: false });
expectType<ScopedElement>(elementFindByAltText);

// findAll
Expand All @@ -68,18 +68,18 @@ describe('new element() api', function () {
expectType<ScopedElement>(elementFindAll.nth(2));
expectType<number>(await elementFindAll.count());

const elementFindAllByText = browser.element.findAllByText('some-text', {exact: true, abortOnFailure: false});
const elementFindAllByText = browser.element.findAllByText('some-text', { exact: true, abortOnFailure: false });
expectType<Elements>(elementFindAllByText);

const elementFindAllByRole = browser.element.findAllByRole('heading', {level: 2, expanded: true, retryInterval: 100});
const elementFindAllByRole = browser.element.findAllByRole('heading', { level: 2, expanded: true, retryInterval: 100 });
expectType<Elements>(elementFindAllByRole);
browser.element.findAllByRole('button', {current: false, expanded: true, index: 2});
expectError(browser.element.findAllByRole('button', {level: 2, expanded: true, retryInterval: 100}));
browser.element.findAllByRole('button', { current: false, expanded: true, index: 2 });
expectError(browser.element.findAllByRole('button', { level: 2, expanded: true, retryInterval: 100 }));

const elementFindAllByPlaceholderText = browser.element.findAllByPlaceholderText('some-text', {exact: true, abortOnFailure: false});
const elementFindAllByPlaceholderText = browser.element.findAllByPlaceholderText('some-text', { exact: true, abortOnFailure: false });
expectType<Elements>(elementFindAllByPlaceholderText);

const elementFindAllByAltText = browser.element.findAllByAltText('some-text', {exact: false, abortOnFailure: false});
const elementFindAllByAltText = browser.element.findAllByAltText('some-text', { exact: false, abortOnFailure: false });
expectType<Elements>(elementFindAllByAltText);
});

Expand All @@ -103,27 +103,27 @@ describe('new element() api', function () {
expectType<ScopedElement>(elem.find('selector'));
expectType<ScopedElement>(elem.get('selector'));

expectType<ScopedElement>(elem.findByText('some-text', {exact: true, abortOnFailure: false}));
expectType<ScopedElement>(elem.findByText('some-text', { exact: true, abortOnFailure: false }));

expectType<ScopedElement>(elem.findByRole('heading', {level: 2, expanded: true, retryInterval: 100}));
expectType<ScopedElement>(elem.findByRole('button', {current: false, expanded: true, index: 2}));
expectError(elem.findByRole('button', {level: 2, expanded: true, retryInterval: 100}));
expectType<ScopedElement>(elem.findByRole('heading', { level: 2, expanded: true, retryInterval: 100 }));
expectType<ScopedElement>(elem.findByRole('button', { current: false, expanded: true, index: 2 }));
expectError(elem.findByRole('button', { level: 2, expanded: true, retryInterval: 100 }));

expectType<ScopedElement>(elem.findByPlaceholderText('some-text', {exact: true, abortOnFailure: false}));
expectType<ScopedElement>(elem.findByLabelText('some-text', {exact: true, abortOnFailure: false}));
expectType<ScopedElement>(elem.findByAltText('some-text', {exact: true, abortOnFailure: false}));
expectType<ScopedElement>(elem.findByPlaceholderText('some-text', { exact: true, abortOnFailure: false }));
expectType<ScopedElement>(elem.findByLabelText('some-text', { exact: true, abortOnFailure: false }));
expectType<ScopedElement>(elem.findByAltText('some-text', { exact: true, abortOnFailure: false }));

expectType<Elements>(elem.findAll('selector'));
expectType<Elements>(elem.getAll('selector'));

expectType<Elements>(elem.findAllByText('some-text', {exact: true, abortOnFailure: false}));
expectType<Elements>(elem.findAllByText('some-text', { exact: true, abortOnFailure: false }));

expectType<Elements>(elem.findAllByRole('heading', {level: 2, expanded: true, retryInterval: 100}));
expectType<Elements>(elem.findAllByRole('button', {current: false, expanded: true, index: 2}));
expectError(elem.findAllByRole('button', {level: 2, expanded: true, retryInterval: 100}));
expectType<Elements>(elem.findAllByRole('heading', { level: 2, expanded: true, retryInterval: 100 }));
expectType<Elements>(elem.findAllByRole('button', { current: false, expanded: true, index: 2 }));
expectError(elem.findAllByRole('button', { level: 2, expanded: true, retryInterval: 100 }));

expectType<Elements>(elem.findAllByPlaceholderText('some-text', {exact: true, abortOnFailure: false}));
expectType<Elements>(elem.findAllByAltText('some-text', {exact: true, abortOnFailure: false}));
expectType<Elements>(elem.findAllByPlaceholderText('some-text', { exact: true, abortOnFailure: false }));
expectType<Elements>(elem.findAllByAltText('some-text', { exact: true, abortOnFailure: false }));

expectType<ScopedElement>(elem.getFirstElementChild());
expectType<ScopedElement>(elem.getLastElementChild());
Expand All @@ -140,6 +140,7 @@ describe('new element() api', function () {
expectType<ElementValue<string | null>>(elem.getProperty('property-name'));
expectType<ElementValue<string | null>>(elem.getAttribute('attrib-name'));
expectType<ElementValue<string | null>>(elem.getValue());
expectType<Promise<string | null>>(elem.isEnabled());

expectType<ElementValue<ScopedElementRect>>(elem.getRect());
expectType<ElementValue<ScopedElementRect>>(elem.getSize());
Expand All @@ -157,12 +158,12 @@ describe('new element() api', function () {
expectType<Promise<WebElement>>(elem.submit());
expectType<Promise<WebElement>>(elem.setProperty('type', 'text'));
expectType<Promise<WebElement>>(elem.setAttribute('role', 'button'));
expectType<Promise<WebElement>>(elem.dragAndDrop({xOffset: 150, yOffset: 500}));
expectType<Promise<WebElement>>(elem.dragAndDrop({ xOffset: 150, yOffset: 500 }));
expectType<Promise<WebElement>>(elem.moveTo(100, 100));
expectType<Promise<WebElement>>(elem.clickAndHold());
expectType<Promise<WebElement>>(elem.doubleClick());
expectType<Promise<WebElement>>(elem.rightClick());
expectType<Promise<WebElement>>(elem.waitUntil('visible', {timeout: 5000}));
expectType<Promise<WebElement>>(elem.waitUntil('visible', { timeout: 5000 }));
});

test('test element assertions', async function () {
Expand Down
14 changes: 8 additions & 6 deletions types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
WebElementPromise
} from 'selenium-webdriver';

import {ElementProperties} from './page-object';
import {Element, LocateStrategy, NightwatchClient} from './index';
import { ElementProperties } from './page-object';
import { Element, LocateStrategy, NightwatchClient } from './index';

export interface ScopedElement extends Element, PromiseLike<WebElement> {
assert: ElementAssertions;
Expand Down Expand Up @@ -45,28 +45,28 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {
readonly expanded?: boolean;
}
): ScopedElement;

findByPlaceholderText(
text: string,
options?: Omit<ScopedSelectorObject, 'selector'> & {
readonly exact?: boolean;
}
): ScopedElement;

findByLabelText(
text: string,
options?: Omit<ScopedSelectorObject, 'selector'> & {
readonly exact?: boolean;
}
): ScopedElement;

findByAltText(
text: string,
options?: Omit<ScopedSelectorObject, 'selector'> & {
readonly exact?: boolean;
}
): ScopedElement;

findAll(selector: ScopedSelector | Promise<ScopedSelector>): Elements;
getAll(selector: ScopedSelector | Promise<ScopedSelector>): Elements;

Expand Down Expand Up @@ -188,6 +188,8 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {
rightClick(): Promise<WebElement>;

waitUntil(signalOrOptions: WaitUntilActions | WaitUntilOptions, waitOptions?: WaitUntilOptions): Promise<WebElement>;

isEnabled(): Promise<string | null>;
}

type WaitUntilOptions = {
Expand Down

0 comments on commit 0d95b73

Please sign in to comment.