mocha selenium test simple example not working - selenium

I am trying to get an understanding of how to use mocha and selenium together. I found a simple tutorial but as soon as it starts I get the following error
Google Search
1) should work
0 passing (2s)
1 failing
1) Google Search should work:
Error: timeout of 2000ms exceeded
at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:157:19)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
Here is the code that I have
var assert = require('assert'),
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver');
test.describe('Google Search', function() {
test.it('should work', function() {
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.firefox()).
build();
driver.get('http://www.google.com');
var searchBox = driver.findElement(webdriver.By.name('q'));
searchBox.sendKeys('simple programmer');
searchBox.getAttribute('value').then(function(value) {
assert.equal(value, 'simple programmer');
});
driver.quit();
});
});
So I ran the following command to test selenium-webdriver and it fails only for mocha
npm test selenium-webdriver
Error I get
1) Mocha Integration it properly allows timeouts and cancels control flow :
Error: timeout of 1000ms exceeded
at Test.done (/usr/local/lib/node_modules/mocha/lib/runnable.js:204:67)
at Test.runnable.callback.mochaCallback (/Users/jcostanzo/Work/Development/automation/phantomjs/node_modules/selenium-webdriver/test/testing/index_test.js:61:30)
at Test.cleanupBeforeCallback [as callback] (/Users/jcostanzo/Work/Development/automation/phantomjs/node_modules/selenium-webdriver/testing/index.js:123:52)
at null.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:157:10)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)

You can increase the timeout by calling this.timeout(5000) either in the describe function or in the it function.
Find all the details and examples here: http://visionmedia.github.io/mocha/#suite-specific-timeouts

Related

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.

Error when calling findElement after findElements on webdriver.WebElement (using Protractor, JS)

I'm using Protractor (Angular JS's webdriver wrapper), and although I can chain findElement indefinitely off of a single WebElement instance, I get an error when I attempt to chain findElement after using findElements (plural).
Error (stack trace given at bottom):
TypeError: Object [object Object] has no method 'findElement'
Chaining findElement:
var elementPromise = browser.findElement(by.css('body')).findElement(by.css('ul')).findElement(by.css('li'));
elementPromise.findElement(by.css('.icon-meter')); // does not raise error
Using findElement after findElements:
var arrayPromise = browser.findElement(by.css('ul')).findElements(by.css('li'));
elementPromise = arrayPromise.then(function(elems) {
return elems[0];
});
elementPromise.findElement(by.css('.icon-meter')); // => raises error
Error message with trace
TypeError: Object [object Object] has no method 'findElement'
at repl:1:16
at /home/markham/src/dataraptor-rails4/spec/node_modules/elementexplorer-convenience.js:83:19
at webdriver.promise.ControlFlow.runInNewFrame_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1598:20)
at webdriver.promise.ControlFlow.runEventLoop_ (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/webdriver/promise.js:1463:8)
at wrapper [as _onTimeout] (timers.js:252:14)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
I got the same problem with the version 0.24 of protractor.
As a workaround, i have replaced all the invocation of findElement() by element() and all findElements() by all().
Chaining element:
var elementPromise = browser.element(by.css('body')).element(by.css('ul')).element(by.css('li'));
elementPromise.element(by.css('.icon-meter'));
Using findElement after findElements:
var arrayPromise = browser.element(by.css('ul')).all(by.css('li'));
elementPromise = arrayPromise.then(function(elems) {
return elems[0];
});
elementPromise.element(by.css('.icon-meter'));
There is more details on this release in this g+ post

Problems using PhantomJS inside Mocha

I'm having issues while trying to send PhantomJS calls to servers from my Mocha testsuite.
Problem
I'm trying to use PhantomJS to do calls against an endpoint. I have the first call working.
But I have two problems:
The current script runs well for the first time, but consecutive runs when watching some files make the tests fail. Do do I solve this?
This does not seem like a nice way to do these tests, is there a better alternative?
Setup:
Gulp as watcher
gulp-mocha to run tests
phantom-sync to do some end2end testing
Mocha test file (simplified):
if(typeof process != 'undefined') {
var should = require('chai').should();
var _ps = require('phantom-sync');
var phantom = _ps.phantom;
var sync = _ps.sync;
}
describe('Login', function() {
this.timeout(5000);
it('should be able to open CMS', function(done) {
sync(function() {
var ph = phantom.create();
var page = ph.createPage();
var status = page.open('http://www.google.com'); // Get a default CMS url...
status.should.equal('success');
ph.exit();
return done();
});
});
it('should redirect after successful login');
});
Gulpfile (simplified):
gulp.task('test-develop', ['scripts'], function() {
return test(null, true);
});
var keepAlive = false;
function test(reporter, _keepAlive) {
keepAlive = _keepAlive;
return gulp.src('test/**/*.js')
.pipe(plugins.plumber())
.pipe(plugins.mocha({ reporter: reporter || 'spec' })
.on('error', onError));
}
function onError(err) {
console.log(err.toString());
if (keepAlive) {
this.emit('end');
} else {
// if you want to be really specific
process.exit(1);
}
}
gulp.task('watch-test', function() {
gulp.watch('test/**/*.js', ['test-develop']);
});
Errors:
➜ [project_dir] git:(feature/phantomjs-tests) ✗ gulp watch-test
[gulp] Using gulpfile [project_dir]/gulpfile.js
[gulp] Starting 'watch-test'...
[gulp] Finished 'watch-test' after 26 ms
[gulp] Starting 'scripts'...
[gulp] Finished 'scripts' after 530 ms
[gulp] Starting 'test-develop'...
Homepage
Menu
- should open without error
- should close without error
Login
✓ should be able to open CMS (2126ms)
- should capture wrong username
- should capture wrong password
- should capture wrong username & password
- should redirect after successful login
ArtobjectPage
#ArtobjectPage
- should save $container
- should call setupZoom
- should call setupInfoButton
- should call setupObjectData
1 passing (2s)
10 pending
[gulp] Finished 'test-develop' after 2.42 s
[gulp] Starting 'scripts'...
[gulp] Finished 'scripts' after 94 ms
[gulp] Starting 'test-develop'...
Homepage
Menu
- should open without error
- should close without error
Login
1) should be able to open CMS
- should capture wrong username
- should capture wrong password
- should capture wrong username & password
- should redirect after successful login
ArtobjectPage
#ArtobjectPage
- should save $container
- should call setupZoom
- should call setupInfoButton
- should call setupObjectData
0 passing (2ms)
10 pending
1 failing
1) Login should be able to open CMS:
TypeError: undefined is not a function
at sync ([project_dir]/node_modules/phantom-sync/node_modules/make-sync/lib/make-sync.js:132:10)
at Context.<anonymous> ([project_dir]/test/e2e/login.js:13:5)
at Test.Runnable.run ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runnable.js:196:15)
at Runner.runTest ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:374:10)
at [project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:452:12
at next ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:299:14)
at [project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:309:7
at next ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:247:23)
at Object._onImmediate ([project_dir]/node_modules/gulp-mocha/node_modules/mocha/lib/runner.js:276:5)
at processImmediate [as _immediateCallback] (timers.js:330:15)
[gulp] Error in plugin 'gulp-mocha': 1 test failed.
[gulp] Finished 'test-develop' after 85 ms
It ended up state being evil and Phantom not properly clearing it's cache & cookies when asked to.

CasperJS "Syntax error: Parse error"

I've just been starting to experiment with CasperJS for some testing purposes, but I'm apparently pretty crappy at it because I've isolated my code down to the very first task,:
"use strict";
var casper = require('casper').create({
verbose: true,
logLevel: 'debug'
waitTimeout: 10000
});
phantom.cookiesEnabled = true;
casper.start('http://foobar.com', function afterstart() {
if (this.exists('.logo-link')) {
this.echo('BOOYA! Page is loaded', 'INFO');
} else {
this.echo('Page didnt load, something went all screwy.', 'ERROR');
}
});
casper.run();
run it through a linter, made the appropriate changes, and I still get this error:
Test file: Test.js
FAIL SyntaxError: Parse error
# type: uncaughtError
# error: "SyntaxError: Parse error"
SyntaxError: Parse error
FAIL 1 tests executed in 0.103s, 0 passed, 1 failed.
Details for the 1 failed test:
In Test.js:0
uncaughtError: SyntaxError: Parse error
I've looked up some possible explanations, I added the phantom.cookiesEnabled = true, but I simply can't figure it out.
Two best answers taken from similar question getting more information from phantomjs "SyntaxError: Parse error" message are:
1) run it with node:
node Test.js
2) use online syntax checker like http://esprima.org/demo/validate.html
You're missing a comma:
logLevel: 'debug' <--- Right here
waitTimeout: 10000