selenium-server won't `setValue` - selenium

I'm using selenium-server version 3.0.1 and nightwatch version ^0.9.12 on node 8.9.0. My e2e test do run, clicks work, and reading the DOM works, but setValue just doesn't.
For example, the following test:
browser
.url("...")
.waitForElementPresent('select[name=foo]', 5000)
.click('select[name=foo] option:nth-child(2)')
.waitForElementPresent('input[name=bar]', 5000)
.setValue('input[name=bar]', "hello world")
.getValue('input[name=bar]', function(input) {
this.assert.equal(input.value, "hello world");
})
.end();
will open the url, wait for foo and click the second option. It will wait for bar, then fails:
Running: test
✔ Element <select[name=foo]> was present after 24 milliseconds.
✔ Element <input[name=bar]> was present after 28 milliseconds.
✖ Failed [equal]: ('' == 'hello world') - expected "hello world" but got: ""
at Object.<anonymous> (/test/e2e/specs/test.js:49:21)
FAILED: 1 assertions failed and 2 passed (4.692s)
_________________________________________________
TEST FAILURE: 1 assertions failed, 2 passed. (4.9s)
✖ test
- run through apply process (4.692s)
Failed [equal]: ('' == 'hello world') - expected "hello world" but got: ""
If I replace the setValue with a delay and enter a value by hand, the test will pass, so getValue is working.
This does run, and pass, on other systems, but I can't get it working on my own so I think it's a selenium-server issue.
I've tried a lot of the 101 fixes, clearing the npm cache, re-running an npm install, etc. But with no errors other than the failure, how do I debug this?

Assuming you are trying to test using Chrome, you need to update your ChromeDriver.
Chrome 65 was released recently and older ChromeDriver versions are apparently incompatible with it.
Download the latest one from the downloads page.
Make Nightwatch use it, nightwatch.json -
{
...
"selenium": {
...
"cli_args": {
"webdriver.chrome.driver": "path/to/chromedriver.exe"
Assuming Nightwatch uses it (you can see which one it uses using the Windows Task Manager - assuming you are using Windows - look for the command line of chromedriver.exe), setValue should now work again.

The error says it all :
Failed [equal]: ('' == 'hello world') - expected "hello world" but got: ""
In your code block you have induced wait for the WebElement identified as 'input[name=bar]' and next invoked setValue() which is successful as follows :
.waitForElementPresent('input[name=bar]', 5000)
.setValue('input[name=bar]', "hello world")
Now the JavaScript associated with this WebElement identified as 'input[name=bar]' will require some time to render the value within the HTML DOM. But in you code block you are trying to access the entered value (in the previous step) too early. Hence your script finds <null> as the value.
Solution
You need to induce a waiter i.e. expect clause for the value to be rendered within the HTML DOM through the associated JavaScript as follows :
.waitForElementPresent('input[name=bar]', 5000)
.setValue('input[name=bar]', "hello world")
.expect.element('input[name=bar]').to.have.value.that.equals('hello world');
.getValue('input[name=bar]', function(input) {
this.assert.equal(input.value, "hello world");
})

Related

Protractor throws "Invalid session Id" when you have browser.quit in afterAll block

I have two specs in a suite. Each spec has "browser.quit()" in afterAll block.
When I run these specs as suite, first spec execute without any issue and other spec throws "Invalid session ID" exception. How to fix this issue?.
Sample script.
test1.js & test2.js are same as below
describe("test1",function(){
beforeAll(function(){
console.log("Pre-condition");
});
it("test1 scenario",function(){
browser.waitForAngualrEnabled(false);
browser.get("https://www.google.com");
});
afterAll(function(){
browser.quit();
});
});
Protractor version : 7.0.0
conf.js
exports.config={
framework:'jasmine',
directConnect: false,
suites:{
test:["test1.js","test2.js"]
}
}
When these specs are run individually, it executes without any issues. I need each spec in suite has to launch and quit browser.
I don't understand why are you surprised about this error, if your script is:
console.log("Pre-condition");
browser.waitForAngualrEnabled(false);
browser.get("https://www.google.com");
browser.quit(); // close browser!!! <------------------
run spec 2, but the browser is closed at this time
Don't close the browser after the spec 1 is completed

How to add cookie to Selenium IDE test running in grid via selenium-side-runner for Zalenium messages

I've recorded a test using Selenium IDE and am submitting the generated .side file to selenium-side-runner to run on a Selenium Grid built using Zalenium. Is it possible to run a command that calls driver.manage().addCookie() from the test that was submitted to selenium-side-runner? I want to do this to send messages back to Zalenium with test progress and status
I added a command executeScript to the Selenium IDE editor with a target of driver.manage().addCookie({name: 'test', value: 'test'})
I see that the command that selenium-side-runner generated in commons.js was
await driver.executeScript(`driver.manage().addCookie({name:'test', value: 'test'});`);
Doing this causes the browser to report an error JavascriptError: javascript error: driver is not defined
I think what I need is the code to be generated without the driver.executeScript wrapper. Is there a way to accomplish this without exporting my Selenium IDE test to NUnit?
I was able to make this functionality work by crudely modifying the selenium-side-runner package on my Windows dev machine
In file ~\node_modules\selenium-side-runner\node_modules\selianize\dist\selianize.cjs.js
Change
function generateScript(script, isExpression = false) {
return `await driver.executeScript(\`${isExpression ? `return (${script.script})` : script.script}\`${script.argv.length ? ',' : ''}${script.argv.map(n => `vars["${n}"]`).join(',')});`;
}
to
function generateScript(script, isExpression = false) {
if (script.script.indexOf('zalenium') > -1)
{
return script.script;
} else
{
return `await driver.executeScript(\`${isExpression ? `return (${script.script})` : script.script}\`${script.argv.length ? ',' : ''}${script.argv.map(n => `vars["${n}"]`).join(',')});`;
}
}
Now when running a test with selenium-side-runner, calling "executeScript" with any value that contains zalenium will generate the command verbatim in the test script

Clicking element once gives error, clicking it twice works fine, why? (Protractor)

I'm running end-to-end tests on a website with Protractor, and one of the elements is having a strange behavior. When I first tried specifying the code to click the element once, I got the following error.
Problematic function:
fooField = element(by.css('#inputFoo'));
foosList = $$('[type=radio]');
async chooseFoo(id: number) {
const foo = this.foosList.get(id - 1);
// await browser.sleep(2000);
// await this.fooField.click();
await this.fooField.click();
await browser.sleep(this.sleepTime);
await foo.click();
await this.selectFooButton.click();
}
Error:
**************************************************
* Failures *
**************************************************
1) reimbursement request page should allow a user to register a request
- Failed: element not interactable
(Session info: chrome=72.0.3626.81)
(Driver info: chromedriver=2.46.628388 (4a34a70827ac54148e092aafb70504c4ea7ae926),platform=Linux 4.15.0-45-generic x86_64)
Executed 2 of 2 specs (1 FAILED) in 28 secs.
[16:50:44] I/launcher - 0 instance(s) of WebDriver still running
[16:50:44] I/launcher - chrome #01 failed 1 test(s)
[16:50:44] I/launcher - overall: 1 failed spec(s)
[16:50:44] E/launcher - Process exited with error code 1
An unexpected error occurred: undefined
After changing the code to click on the element twice in a row, and changing nothing else, the code ran successfully. I also tried removing the redundant click and adding a sleep right before the click, but I still got the same error. Why is this happening?
Not sure which element you're trying to click, but this way of clicking has helped me when having similar problems:
browser.executeScript('arguments[0].click()', elementToClick);
let fooField = element(by.css('#inputFoo'));
await browser.wait(ExpectedConditions.elementToBeClickable(fooField), 5000, 'Element is not clickable.');
await fooField.click();
If the element is not clickable after the certain time will throw error, there is a problem with the button. Something is blocking it, to be clicked.
Another way to bypass it (not the best way) is using javascript click.
let fooField = element(by.css('#inputFoo'));
browser.executeScript('arguments[0].scrollIntoView(true); arguments[0].click();', fooField);

TestCafe - Test fails onGetBposShellInfoNavBarData

I am running TestCafe version 0.22.0 on Win10. I am not sure if my issue has something with the Win10 or not but unfortunately, I don´t have another computer to test on. The issue is that my tests fail with error message "GetBposShellInfoNavBarData failed: SyntaxError: Unexpected end of JSON input"
Here is the simple code I use:
Error on page "https://outlook.live.com/mail/inbox": GetBposShellInfoNavBarData failed: SyntaxError: Unexpected end of JSON input
Browser: Chrome 69.0.3497 / Windows 10.0.0
await t.click('body > section > div > div > nav > div > div > div > a');
await t.typeText('#i0116', login);
await t.click('#idSIButton9');
await t.typeText('#i0118', password);
await t.click('#idSIButton9');
await t.maximizeWindow();
Test fails on this t.maximizeWindow(). It also fails with same error on next "t.click(Selector...) if I comment out t.maximizeWindow() line.
As #ioseph properly mentioned, this error occurs on your web page and is not related to TestCafe.
I recommend that you address this error on your website and run your tests again.
Alternatively, you can use the --skip-js-errors argument to ignore such errors.

Selenium test fails when running phantomjs

I using nightwatch framework for running End-to-End test. I am able to run all the test cases successfully in chrome and IE but few tests fails for phantomjs. Test cases below error out
'Search' : function(browser) {
browser
.url('http://localhost:8001/index.html')
browser
.setValue('input[id=SearchInput-inner]', 'abc')
.click('button[id=nsnButton]')
.pause('1000')
.verify.attributeEquals('#nsnButton', 'disabled', 'true')
},
Error I get is
Testing if attribute disabled of <#nsnButton> equals "true". Element does not have a disabled attribute. - expected "true" but got: "null"
other test that fails is
.getCssProperty('#SearchInput-inner', 'border-color', function(result) {
this.verify.equal(result.value, '#ee0000');
})
Error I get is Failed [equal]: ('rgb(238, 0, 0)' == '#ee0000') - expected "#ee0000" but got: "rgb(238, 0, 0)"
How Can I handle this situation where same test works for ie and chrome but fails for phantomjs.
Thanks