Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isActive() command to new Element API #4261

Merged
merged 9 commits into from
Nov 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/api/web-element/commands/isActive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Determines if an element is currently active/focused in the DOM.
*
* For more info on working with DOM elements in Nightwatch, refer to the <a href="https://nightwatchjs.org/guide/writing-tests/finding-interacting-with-dom-elements.html">Finding & interacting with DOM Elements</a> guide page.
*
* @example
* describe('isActive Demo', function() {
* it('test isActive', function(browser) {
* browser.element('#search')
* .isActive()
* .assert.equals(true);
* });
*
* it('test async isActive', async function(browser) {
* const result = await browser.element('#search').isActive();
* browser.assert.equal(result, true);
* });
* });
*
* @since 3.9.0
* @method isActive
* @memberof ScopedWebElement
* @instance
* @syntax browser.element.find(selector).isActive()
* @link /#get-active-element
* @returns {ScopedValue<boolean>}
*/
module.exports.command = function () {
return this.runQueuedCommandScoped('isElementActive');
};
9 changes: 9 additions & 0 deletions lib/transport/selenium-webdriver/method-mappings.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,15 @@ module.exports = class MethodMappings {
};
},

async isElementActive(webElementOrId) {
const element = this.getWebElement(webElementOrId);
const elementId = await element.getId();

const currentActiveElementId = await this.methods.session.getActiveElement.call(this);

return elementId === currentActiveElementId;
},

async setElementProperty(webElementOrId, name, value) {
const element = this.getWebElement(webElementOrId);

Expand Down
130 changes: 130 additions & 0 deletions test/src/api/commands/web-element/testIsActive.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
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().isActive() command', function() {
before(function (done) {
CommandGlobals.beforeEach.call(this, done);
});

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

it('test .element().isActive() active', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/active',
method: 'GET',
response: JSON.stringify({
value: {
'element-6066-11e4-a52e-4f735466cecf': '0'
}
})
}, true);

const resultPromise = this.client.api.element('#signupSection').isActive();
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, true);
});

it('test .element().isActive() not active', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/active',
method: 'GET',
response: JSON.stringify({
value: {
'element-6066-11e4-a52e-4f735466cecf': 'random-elem'
}
})
}, true);

const resultPromise = this.client.api.element('#signupSection').isActive();
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, false);
});

it('test .element().find().isActive()', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/active',
method: 'GET',
response: JSON.stringify({
value: {
'element-6066-11e4-a52e-4f735466cecf': '1'
}
})
}, true);

const resultPromise = this.client.api.element('#signupSection').find('#helpBtn').isActive();
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, true);
});

it('test .element.find().isActive() not active', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/active',
method: 'GET',
response: JSON.stringify({
value: {
'element-6066-11e4-a52e-4f735466cecf': 'random-elem'
}
})
}, true);

const resultPromise = this.client.api.element.find('#signupSection').isActive();
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, false);
});

it('test .element().isActive() assert', async function() {
MockServer.addMock({
url: '/session/13521-10219-202/element/active',
method: 'GET',
response: JSON.stringify({
value: {
'element-6066-11e4-a52e-4f735466cecf': '0'
}
})
}, true);

const resultPromise = this.client.api.element('#signupSection').isActive();
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(true), true);
assert.strictEqual(await resultPromise.assert.not.equals(false), true);
});
});
1 change: 1 addition & 0 deletions types/tests/webElement.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ describe('new element() api', function () {
expectType<ElementValue<boolean>>(elem.isSelected());
expectType<ElementValue<boolean>>(elem.isVisible());
expectType<ElementValue<boolean>>(elem.isDisplayed());
expectType<ElementValue<boolean>>(elem.isActive());

expectType<ElementValue<ScopedElementRect>>(elem.getRect());
expectType<ElementValue<ScopedElementRect>>(elem.rect());
Expand Down
2 changes: 2 additions & 0 deletions types/web-element.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ export interface ScopedElement extends Element, PromiseLike<WebElement> {

isVisible(): ElementValue<boolean>;
isDisplayed(): ElementValue<boolean>;

isActive(): ElementValue<boolean>;
}

type WaitUntilOptions = {
Expand Down
Loading