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

issue #4028 fix : added fallback for clearValue command #4035

Merged
merged 20 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
11 changes: 10 additions & 1 deletion lib/transport/selenium-webdriver/method-mappings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {WebElement, WebDriver, Origin, By, until, Condition} = require('selenium-webdriver');
const {WebElement, WebDriver, Origin, Key, By, until, Condition} = require('selenium-webdriver');
const {Locator} = require('../../element');
const NightwatchLocator = require('../../element/locator-factory.js');
const {isString} = require('../../utils');
Expand Down Expand Up @@ -586,6 +586,15 @@ module.exports = class MethodMappings {
async clearElementValue(webElementOrId) {
const element = this.getWebElement(webElementOrId);
await element.clear();
try {
const value = await element.getProperty('value');
if (isString(value) && value.length !== 0) {
const backArr = value.split('').map(() => Key.BACK_SPACE);
await element.sendKeys(...backArr);
}
} catch {
// silent catch
}

return null;
},
Expand Down
70 changes: 69 additions & 1 deletion test/src/api/commands/element/testClearValue.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const assert = require('assert');
const MockServer = require('../../../../lib/mockserver.js');
const Nightwatch = require('../../../../lib/nightwatch.js');
const {Key} = require('selenium-webdriver');

describe('clearValue', function() {

Expand Down Expand Up @@ -118,6 +119,73 @@ describe('clearValue', function() {
});
});

it('client.clearValue() with webdriver protocol - ensuring that fallback is working', function (done) {
Nightwatch.initClient({
selenium: {
version2: false,
start_process: false,
host: null
},
output: true,
silent: false,
webdriver: {
host: 'localhost',
start_process: false
}
}).then((client) => {
const bArr = Array(6).fill(Key.BACK_SPACE);
const str = bArr.join('');
let sendKeysMockCalled = false;

MockServer.addMock(
{
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/clear',
response: {value: null}
},
true
);

MockServer.addMock(
{
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/property/value',
response: {value: 'sample'}
},
true
);


MockServer.addMock(
{
url: '/session/13521-10219-202/element/5cc459b8-36a8-3042-8b4a-258883ea642b/value',
method: 'POST',
postdata: {
text: str,
value: bArr
},
response: {
sessionId: '13521-10219-202',
status: 0
},
onRequest() {
sendKeysMockCalled = true;
},
statusCode: 200
},
true
);


client.api.sendKeys('css selector', '#webdriver', bArr, function callback(result) {
assert.strictEqual(sendKeysMockCalled, true);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you calling .sendKeys() command directly? We just want to call .clearValue command from outside and then we want to test if the internal element.sendKeys() (which you specified in method-mappings.js file) was called or not if the element.getProperty('value') command still returns some value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @garg3133 ,
for some reason, the post request was not being sent when I tried checking the logs
So I looked into the codebase to see how keys were being sent in a test and I saw this method
I tried doing it by using .sendKeys() and it worked

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @garg3133 , for some reason, the post request was not being sent when I tried checking the logs So I looked into the codebase to see how keys were being sent in a test and I saw this method I tried doing it by using .sendKeys() and it worked

Are you still working on this issue ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes I am working on this. had been sick that's why the delay
apologies

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, just wanted to make sure that this is all still being worked on. I could land an eye if you need. (not sure that I can be of a great help but I can still try 😬)

}).clearValue('css selector', '#webdriver', function (result) {
assert.strictEqual(result.value, null);
// ensures fallback is working
});

client.start(done);
});
});

it('client.clearValue() with webdriver protocol - element not found', function(done) {
Nightwatch.initClient({
selenium: {
Expand Down Expand Up @@ -163,4 +231,4 @@ describe('clearValue', function() {
client.start(done);
});
});
});
});