Testing React Apps with Phantom/Casper - testing

I'm diving into functional testing and attempting to get a few simple tasks working. The app is built in ReactJS and I've decided to use Phantom/Casper. The problem is that even the most basic tasks fail.
In short, is there a trick for testing React apps with Phantom/Casper?
I've installed Phantom (v.2.1.1) and Casper (v1.1.0-beta5). As a first attempt I created a simple script to capture an image:
capture.js
var casper = require('casper').create({
viewportSize: {
width: 1024,
height: 768
},
verbose: true,
logLevel: "debug"
});
casper.start('http://localhost:9494', function() {
console.log("page loaded");
});
casper.then(function() {
this.capture('img.png');
});
});
casper.run();
Then run the script:
> casperjs capture.js
Viewing localhost:9494 in my browser pulls up the app as it should. But the resulting capture() image is a blank screen.
I've also tried adding a wait(), waitForSelector() and waitForResource() to no avail.
Here's the output in the console:
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://localhost:9494/, HTTP GET
[debug] [phantom] Navigation requested: url=http://localhost:9494/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://localhost:9494/"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 http://localhost:9494/ (HTTP 200) page loaded
[debug] [phantom] Capturing page to /path/to/img.png
[info] [phantom] Capture saved to /path/to/img.png
[info] [phantom] Step anonymous 2/2: done in 848ms.
[info] [phantom] Done 2 steps in 848ms
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "about:blank"

You need to add the babel-polyfill NPM package to your project (or any other version of the polyfill) and then in your main index.js(x) entry point for your app, include this line at the top:
import 'babel-polyfill';
We were having the exact same issue you are experiencing and this fixed it for us.
We had tried injecting the babel polyfill as part of the Casper test suite, but it was not working.

Not sure how you did the waiting. Make sure your capture is in the wait callback. I usually use code like this and it often happens that you need to adjust the waiting time to see results.
3 seconds is usually enough to crawl public websites, that's how I use it.
casper.then(function() {
this.wait(3000, function() {
this.capture('foo.jpg', undefined,
{
format: 'jpg',
quality: 75
});
});
});

Related

click link without ID or Class with casperJS

I'm trying to get the list of links on the menu on the left on this page: http://www.hpenterprisesecurity.com/vulncat
the first thing I'm trying to do is click the "Expand All" link (on the top left corner) so the list gets expanded.
I have tried multiple ways to use the Xpath in different ways with no success on this page, this was my last try:
casper.start('http://www.hpenterprisesecurity.com/vulncat', function() {
casper.wait(5000);
this.echo("---------- TITLE: ");
this.echo(this.getTitle());
});
casper.thenClick(x('//*[#id="theContentCenter"]/div/p/a[1]/strong[contains(text(),"Expand")]'),function(){
casper.wait(5000,function(){
casper.capture("expanded.png");
});
});
Maybe since the page lacks Ids and Tags for most elements my approach won't work?
Thanks
the set-up:
casper = require('casper').create({
verbose:true,
logLevel:"debug"
});
casper.userAgent('Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Mobile/7B405');
casper.viewport = {width: 1280, height: 720};
var x = require('casper').selectXPath;
phantomjs: 1.9.8
casperjs: 1.1.0-beta3
RESOURCE ERROR:
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[info] [phantom] Starting...
[info] [phantom] Running suite: 4 steps
[debug] [phantom] opening url: http://www.hpenterprisesecurity.com/vulncat, HTTP GET
[debug] [phantom] Navigation requested: url=http://www.hpenterprisesecurity.com/vulncat, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html"
[debug] [phantom] Navigation requested: url=http://www.hpenterprisesecurity.com/vulncat/en/vulncat/header.html, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=http://www.hpenterprisesecurity.com/vulncat/en/vulncat/all.html, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=http://www.hpenterprisesecurity.com/vulncat/en/vulncat/intro.html, type=Other, willNavigate=true, isMainFrame=false
ResourceError: {
"errorCode": 203,
"errorString": "Error downloading http://www.hpenterprisesecurity.com/vulncat/en/vulncat/css/frame.css - server replied: Not Found",
"id": 7,
"url": "http://www.hpenterprisesecurity.com/vulncat/en/vulncat/css/frame.css"
}
ResourceError: {
"errorCode": 2,
"errorString": "Connection closed",
"id": 9,
"url": "http://www.fortifysoftware.com/images/gui/maincontent.level3.h1.bg.png"
}
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/4 http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html (HTTP 200)
---------- TITLE:
A Taxonomy of Coding Errors that Affect Security
[info] [phantom] Step anonymous 2/4: done in 770ms.
[info] [phantom] Step _step 3/5 http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html (HTTP 200)
[info] [phantom] Step _step 3/5: done in 770ms.
[info] [phantom] wait() finished waiting for 5000ms.
[info] [phantom] Step _step 4/5 http://www.hpenterprisesecurity.com/vulncat/en/vulncat/index.html (HTTP 200)
[debug] [phantom] Mouse event 'mousedown' on selector: xpath selector: //*[#id="theContentCenter"]/div/p/a[1]/strong[contains(text(),"Expand")]
CasperError: Cannot dispatch mousedown event on nonexistent selector: xpath selector: //*[#id="theContentCenter"]/div/p/a[1]/strong[contains(text(),"Expand")]
/usr/lib/node_modules/casperjs/modules/casper.js:1355 in mouseEvent
/usr/lib/node_modules/casperjs/modules/casper.js:462 in click
/usr/lib/node_modules/casperjs/modules/casper.js:1793 in _step
/usr/lib/node_modules/casperjs/modules/casper.js:1553 in runStep
/usr/lib/node_modules/casperjs/modules/casper.js:399 in checkStep
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.
Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///usr/lib/node_modules/casperjs/bin/bootstrap.js. Domains, protocols and ports must match.

casperjs workaround for url=about:blank willNavigate=true, isMainFrame=false

I'm stuck on a page which returns 4 lines like this and then hangs up:
[debug] [phantom] opening url: https://xxx, HTTP GET
[debug] [phantom] Navigation requested: url=https://xxx, type=Other, willNavigate=true, isMainFrame=true
[info] [phantom] Step anonymous 3/6: done in 170ms.
[debug] [phantom] url changed to "https://xxx"
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
I launch the command this way, since I read about several ssl related bugs, and it works for the whole website xxx:
casperjs --ignore-ssl-errors=true --ssl-protocol=any script.js
My code is not nested since it's generated by my own class. However it's quite simple:
casper.userAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.7 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.7');
casper.start().then(function() {
this.open('https://xxx', {
headers: {
'Accept': 'text/html'
}
});
});
casper.then(function () {
this.viewport(1920, 1080);
});
casper.wait(
3000,
function () {
this.echo('timeout occured');
}
);
casper.then(function() {
this.capture('capture.png', {
top: 0,
left: 0,
width: 1920,
height: 1080
});
});
casper.run();
I need a hint by an expert, unluckily I'm not. Thank you in advance
Fixed in phantomjs 1.9.8, dont trust apt-get regular source (1.9.0-1) and guides around. Don't use 2.00 if you coupled it with casperjs, since is not supported.
Instructions for UBUNTU, for CENTOS replace apt-get with yum.
sudo apt-get update
apt-get install python
apt-get install ttf-mscorefonts-installer
sudo apt-get install build-essential chrpath libssl-dev libxft-dev
sudo apt-get install libfreetype6 libfreetype6-dev
sudo apt-get install libfontconfig1 libfontconfig1-dev
export PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
sudo tar xvjf $PHANTOM_JS.tar.bz2
sudo mv $PHANTOM_JS /usr/local/share
sudo ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin

Chrome not firing by default through the webdriver-manager

I'm a newbie in e2e tests with protractor and I've recently encountered an issue. When I try to run my test (which is basically the same one I found on the official protractor website), the server starts by using Firefox as a default browser. As I read, chrome supposed to be the one used by default.
16:17:24.345 INFO - Executing: [new session: Capabilities [{count=1, browserName=firefox}]])
16:17:24.345 INFO - Creating a new session for Capabilities [{count=1, browserName=firefox}]
16:17:27.305 INFO - Done: [new session: Capabilities [{count=1, browserName=firefox}]]
Due to the Firefox issue #1734, the test fails all the time
Failures:
1) angularjs homepage todo list should add a todo
Message:
timeout: timed out after 36000 msec waiting for spec to complete
Stacktrace:
undefined
Finished in 36.777 seconds
1 test, 1 assertion, 1 failure
So I added chrome as another browser to use into the configuration file
conf.js
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
browserName: 'chrome'
},
specs: ['todo-spec.js'],
framework: 'jasmine'
};
Here's the test
todo-spec.js
describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('http://angularjs.org');
element(by.model('todoText')).sendKeys('write a protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todos'));
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write a protractor test');
});
});
But it still does not work. What am I doing wrong here?
Thanks for your help!

Casperjs script runs on OSX but not Windows

Phantomjs version - 1.9.7
Casperjs version - 1.1.0-beta3
This is the code
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
});
casper.start('https://www.gmail.com', function() {
casper.wait(3000, function() {
this.capture('gmail.png');
});
});
casper.run(function() {
this.exit();
});
The logs on Windows show
casperjs --ssl-protocol=any --ignore-ssl-errors=true login.js
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: https://www.gmail.com/, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.gmail.com/, type=Other, willNavigate=true, isMainFrame=true
[warning] [phantom] Loading resource failed with status=fail: https://www.gmail.com/
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2: done in 349ms.
[info] [phantom] Step _step 3/3: done in 371ms.
[info] [phantom] wait() finished waiting for 3000ms.
[debug] [phantom] Capturing page to C:/Users/username/Desktop/gmail.png
[info] [phantom] Capture saved to C:/Users/username/Desktop/gmail.png
[info] [phantom] Done 3 steps in 3408ms
On OSX it shows the following
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: https://www.gmail.com/, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.gmail.com/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://mail.google.com/mail/, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Navigation requested: url=https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] url changed to "https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1"
[debug] [phantom] Navigation requested: url=https://accounts.youtube.com/accounts/CheckConnection?pmpo=https://accounts.google.com&v=-320444755&timestamp=1420230909413, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2 https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1 (HTTP 200)
[info] [phantom] Step anonymous 2/2: done in 1398ms.
[info] [phantom] Step _step 3/3 https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1 (HTTP 200)
[info] [phantom] Step _step 3/3: done in 1418ms.
[info] [phantom] wait() finished waiting for 3000ms.
[debug] [phantom] Capturing page to /Users/username/code/casperjs/gmail.png
[info] [phantom] Capture saved to /Users/username/code/casperjs/gmail.png
[info] [phantom] Done 3 steps in 4511ms
The code is the same on both platforms, I have tried various combinations of --ignore-ssl-errors etc with no luck. Any suggestions?
Based on Artjom B.'s suggestions, I have updated the code like this -
var webPage = require('webpage');
var page = webPage.create();
var casper = require('casper').create({
verbose: true,
logLevel: 'debug',
pageSettings: {
userAgent: 'Mozilla/5.0 (Windows NT 6.0) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.41 Safari/535.1'
}
});
casper.on('resource.error', function(resource) {
this.echo('Error: ' +msg, 'ERROR'); });
casper.on("page.error", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
});
page.onResourceTimeout = function(request) {
console.log('Response (#' + request.id + '): ' + JSON.stringify(request));
};
casper.on("remote.message", function(msg, trace) {
this.echo("Error: " + msg, "ERROR");
});
casper.start('https://www.gmail.com', function() {
casper.wait(3000, function() {
this.capture('gmail.png');
});
});
casper.run(function() {
this.exit();
});
This gives the same result -
C:\Users\username\Desktop>casperjs --ssl-protocol=any --ignore-ssl-errors=true pt-login.js
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: https://www.gmail.com/, HTTP GET
[debug] [phantom] Navigation requested: url=https://www.gmail.com/, type=Other, willNavigate=true, isMainFrame=true
[warning] [phantom] Loading resource failed with status=fail: https://www.gmail.com/
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 2/2: done in 338ms.
[info] [phantom] Step _step 3/3: done in 361ms.
[info] [phantom] wait() finished waiting for 3000ms.
[debug] [phantom] Capturing page to C:/Users/username/Desktop/gmail.png
[info] [phantom] Capture saved to C:/Users/username/Desktop/gmail.png
[info] [phantom] Done 3 steps in 3462ms

Casper JS : Strange error: status=fail (HTTP 200)

I'm trying to load the following webpage with casperjs/phantomjs http://m.10bet.com/#leage_panel#10096.
Therefore I wrote the following simple casper script:
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
});
if( casper.cli.args.length != 1 )
casper.echo('No URL as arguments given. Exiting.\n').exit();
var id = casper.cli.args[0]
casper.start( 'http://m.10bet.com/#leage_panel#' + id, function() {
casper.waitForResource("http://m.10bet.com/pagemethods.aspx/UpdateEvents", function() {
this.echo(casper.getPageContent())
}, function(){}, function(){}, 10000 );
});
casper.run(function() {
this.echo('Done.').exit();
});
So, I'm waiting for the last resource to be loaded which is in this case "http://m.10bet.com/pagemethods.aspx/UpdateEvents". I checked this with the chrome developer tools. Subsequently I want to output the rendered html on the console.
However, but instead of the html I get a in my opinion very strange error:
solaris:js_loaders Tom$ casperjs 10bet_loader.js 10096
2014-01-03 17:31:36.545 phantomjs[8733:130b] *** WARNING: Method userSpaceScaleFactor in class NSView is deprecated on 10.7 and later. It should not be used in new applications. Use convertRectToBacking: instead.
[info] [phantom] Starting...
[info] [phantom] Running suite: 2 steps
[debug] [phantom] opening url: http://m.10bet.com/#leage_panel#10096, HTTP GET
[debug] [phantom] Navigation requested: url=http://m.10bet.com/#leage_panel#10096, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "http://m.10bet.com/#leage_panel#10096"
[debug] [phantom] Navigation requested: url=http://m.10bet.com/#leage_panel#10096, type=Reload, lock=true, isMainFrame=true
[warning] [phantom] Loading resource failed with status=fail (HTTP 200): http://m.10bet.com/#leage_panel#10096
[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] url changed to "http://m.10bet.com/#leage_panel#10096"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step 2/2 http://m.10bet.com/#leage_panel#10096 (HTTP 200)
[info] [phantom] Step 2/2: done in 761ms.
[info] [phantom] Step 3/3 http://m.10bet.com/#leage_panel#10096 (HTTP 200)
[info] [phantom] Step 3/3: done in 771ms.
[warning] [phantom] Casper.waitFor() timeout
[info] [phantom] Done 3 steps in 790ms
Done.
solaris:js_loaders Tom$
As you can see from the log the error "Loading resource failed with status=fail (HTTP 200): http://m.10bet.com/#leage_panel#10096" gives a http 200 ok but failed. Eventually the webpage is not loaded or printed on the console. So I'm wondering what is going wrong here?
Usage:
casperjs stackoverflow.js --idEvent=10096
Code:
var casper = require('casper').create ({
waitTimeout: 15000,
stepTimeout: 15000,
verbose: true,
viewportSize: {
width: 1024,
height: 768
},
pageSettings: {
"userAgent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.10 (KHTML, like Gecko) Chrome/23.0.1262.0 Safari/537.10',
"loadImages": false,
"loadPlugins": false,
"webSecurityEnabled": false,
"ignoreSslErrors": true
},
onWaitTimeout: function() {
casper.echo('Wait TimeOut Occured');
},
onStepTimeout: function() {
casper.echo('Step TimeOut Occured');
}
});
//vars
var idEvent = casper.cli.get('idEvent');
// start
casper.start();
// check args
casper.then(function() {
if (!idEvent) {
//usage check
this.echo('Invalid usage: Must supply Event Id');
casper.exit();
}
});
casper.thenOpen('http://m.10bet.com/#leage_panel#' + idEvent, function() {
casper.waitForResource('http://m.10bet.com/pagemethods.aspx/UpdateEvents', function() {
//casper.waitForSelector('#league_block', function() {
}, function then() { //selector found
this.echo(casper.getPageContent());
casper.exit();
}, function timeout() { //selector not found
this.echo("Timeout On Selector...Exiting").exit();
});
});
// executes
casper.run();
Does your arguments work in other scripts? I'm curious because the documentation shows that arguments are referenced in this way.
casper.echo(casper.cli.has(0));
I'm wondering if maybe this is the issue?