protractor with any headless browser? - angularjs

I am using protractor and it works when I specify chrome as the browsertype. I am looking for a headless browser sample code, I have looked for phantomJs but I could not run any of them. Is there a working sample available of another headless browser?

No other headless browser out there besides PhantomJS while the latter is a dead-end with Protractor.
You can try docker-selenium or, if you don't like Docker you can do it yourself with ubuntu-headless sample. Both solutions provide Chrome & Firefox by using Xvfb even though there is no real DISPLAY.
UPDATE 2 Seems to be possible to run Xvfb in OSX: http://xquartz.macosforge.org/landing/
UPDATE 1 Mac OSX selenium headless solution:
Enable multi-user remote desktop access to OSX machine
So can test selenium headless on mac. Not headless really but as another user so it doesn't interfere with your current user display.
To do this you need kickstart: http://support.apple.com/en-us/HT201710
Begin using the kickstart utility
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -restart -agent
Activate Remote Desktop Sharing, enable access privileges for all users and restart ARD Agent:
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -restart -agent -privs -all
Apple Remote Desktop 3.2 or later only
Allow access for all users and give all users full access
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -allowAccessFor -allUsers -privs -all
Kickstart help command
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -help

PhantomJS is a dead-end with Protractor on certain websites when there is a div, which is fixed and always visible (due to https://github.com/ariya/phantomjs/issues/11637). Otherwise you can make it work:
using phantomjs:~1.9.0 and protractor: ~1.8.0
inside protractor config file register function:
onPrepare : function() {
var minWindowWidth = 1024,
minWindowHeight = 768,
browserName,
platform,
window = browser.manage().window();
browser.getCapabilities().then(function (capabilities) {
browserName = capabilities.caps_.browserName;
platform = capabilities.caps_.platform;
}).then(function getCurrentWindowSize() {
return window.getSize();
}).then(function setWindowSize(dimensions) {
var windowWidth = Math.max(dimensions.width, minWindowWidth),
windowHeight = Math.max(dimensions.height, minWindowHeight);
return window.setSize(windowWidth, windowHeight);
}).then(function getUpdatedWindowSize() {
return window.getSize();
}).then(function showWindowSize(dimensions) {
console.log('Browser:', browserName, 'on', platform, 'at', dimensions.width + 'x' + dimensions.height);
console.log("Running e2e tests...");
});
},

Related

Getting ETIMEDOUT error when running protractor conf.js

I am a beginner in protractor. I did the installations necessary for running protractor. When tried running the sample script mentioned in the protractor documentation, i am getting ETIMEDOUT error. and the url points to 127.0.0.1:4444. The same url is not accessible manually also. But when trying http://localhost:4444/wd/hub, page opens properly. I am not sure why the conf.js trying to access the 127.0.0.1:4444, even if i give the 'seleniumAddress' parameter to 'http://localhost:4444/wd/hub'. Please help me guys to resolve this issue
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['todo-spec.js']
};
describe('angularjs homepage todo list', function() {
it('should add a todo', function() {
browser.get('https://angularjs.org');
element(by.model('todoList.todoText')).sendKeys('write first protractor test');
element(by.css('[value="add"]')).click();
var todoList = element.all(by.repeater('todo in todoList.todos'));
expect(todoList.count()).toEqual(3);
expect(todoList.get(2).getText()).toEqual('write first protractor test');
// You wrote your first test, cross it off the list
todoList.get(2).element(by.css('input')).click();
var completedAmount = element.all(by.css('.done-true'));
expect(completedAmount.count()).toEqual(2);
});
});
I agree with the other responses. http://localhost:4444/wd/hub is the same as http://127.0.0.1:4444/wd/hub. Usually this is defined in your /etc/hosts file.
Since I imagine you are just trying to run your Protractor tests, as long as you've downloaded the binaries with webdriver-manager update, you could do one of the two options:
Set directConnect: true (and remove seleniumAddress. This is available for chrome or firefox (version 47*) without the selenium standalone server.
Remove seleniumAddress all together. Protractor will launch the selenium standalone server for you before the test and then tear it down when the test is over.
Note: for the above to work, webdriver-manager update should run from the project directory to download the binaries to the correct directory. Something like node node_modules/.bin/webdriver-manager update or ./node_modules/.bin/webdriver-manager update should download the driver binaries to node_modules/protractor/node_modules/webdriver-manager/selenium.
So why Firefox 47, currently newer versions are not supported. We are currently testing Firefox 48+ but there are still a few outstanding issues.
You need 2 terminal for this.
In first terminal, run below command:
webdriver-manager start
this will create a server for node/client to access(that you have added in seleniumAddress)
In second terminal, run below command:
protractor conf.js
This will start your script, by using server created at http://localhost:4444/wd/hub.
And localhost is same as 127.0.0.1.
If localhost is not the same as 127.0.0.1 it sounds like your hosts files has been played around with or some more nefarious networking issue. I do not feel we have enough information to correctly debug why you are having this issue but I would like to suggest a workaround. Why not use your actual local internal IPv4 address?
To get a list of your IPv4 addresses in Windows type
ipconfig | findstr /R /C:"IPv4 Address"
To get a list of your IPv4 addresses in Linux type
hostname -i
To get your IPv4 address on a Mac type
ifconfig |grep inet
The address on mac should be located on the last line between inet and netmask
Your config file should look something like this after
exports.config = {
seleniumAddress: 'http://192.138.0.100:4444/wd/hub',
specs: ['todo-spec.js']
};

How to run webstorm angularJS application standalone in chrome?

I have an angular application on my local machine, I can open my SPA from webstorm over chrome, firefox or IE, but if I try to open the same html file from my windows explorer without using the IDE, it only opens and runs in firefox but nothing on chrome and IE just a blank page. I remember reading that firefox has an internal server or some sort, and chrome doesn't, I don't remember where I read that, if any body knows why please help.
Long term, you are best off running a local fileserver IMO. You can check out python's simple server example http://effbot.org/librarybook/simplehttpserver.htm, or I typically run something like this in node:
var http = require('http');
var express = require('express');
var app = express();
app.use('./', express.static(__dirname + './'));
app.get('*', function(req, res) {
return res.redirect('./');
});
// Create a server
var server = app.listen(3513, function () {
console.log('Server listening on', 3512)
});
If it is pure angular you are using, you don't need a server, however you might be running into some security problems, try running without security and see what happens.
For OSX, open Terminal and run:
$ open -a Google\ Chrome --args --disable-web-security
For Linux run:
$ google-chrome --disable-web-security
Also if you're trying to access local files for dev purposes like AJAX or JSON, you can use this flag too.
-–allow-file-access-from-files
For Windows go into the command prompt and go into the folder where Chrome.exe is and type
chrome.exe --disable-web-security
That should disable the same origin policy and allow you to access local files.
Otherwise taking a look at the console is the most sane thing to do, sometimes error information will pop-up there.

Can't find variable: angular (PhantomJS webcrawler) [duplicate]

I'm using the following code based on loadspeed.js example to open up a https:// site which requires http server authentication as well.
var page = require('webpage').create(), system = require('system'), t, address;
page.settings.userName = 'myusername';
page.settings.password = 'mypassword';
if (system.args.length === 1) {
console.log('Usage: scrape.js <some URL>');
phantom.exit();
} else {
t = Date.now();
address = system.args[1];
page.open(address, function (status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Page title is ' + page.evaluate(function () {
return document.title;
}));
console.log('Loading time ' + t + ' msec');
}
phantom.exit();
});
}
Its failing to load the page all the time. What could be wrong here? Are secured sites to be handled any differently? The site can be accessed successfully from browser though.
I'm just starting with Phantom right now and find it too good to stop playing around even though i'm not moving forward with this issue.
I tried Fred's and Cameron Tinker's answers, but only --ssl-protocol=any option seem to help me:
phantomjs --ssl-protocol=any test.js
Also I think it should be way safer to use --ssl-protocol=any as you still are using encryption, but --ignore-ssl-errors=true will ignore (duh) all ssl errors, including malicious ones.
The problem is most likely due to SSL certificate errors. If you start phantomjs with the --ignore-ssl-errors=yes option, it should proceed to load the page as it would if there were no SSL errors:
phantomjs --ignore-ssl-errors=yes [phantomOptions] script.js [scriptOptions]
I've seen a few websites having problems with incorrectly implementing their SSL certificates or they've expired, etc. A complete list of command line options for phantomjs is available here: http://phantomjs.org/api/command-line.html.
Note that as of 2014-10-16, PhantomJS defaults to using SSLv3 to open HTTPS connections. With the POODLE vulnerability recently announced, many servers are disabling SSLv3 support.
To get around that, you should be able to run PhantomJS with:
phantomjs --ssl-protocol=tlsv1
Hopefully, PhantomJS will be updated soon to make TLSv1 the default instead of SSLv3.
experienced same issue...
--ignore-ssl-errors=yes was not enough to fix it for me,
had to do two more things:
1) change user-agent
2) tried all ssl-protocols, the only one that worked was tlsv1 for the page in question
Hope this helps...
I experienced the same problem (casperjs 1.1.0-beta3/phantomjs 1.9.7). Using --ignore-ssl-errors=yes and --ssl-protocol=tlsv1 solved it. Using only one of the options did not solve it for me.
I was receiving
Error creating SSL context" from phantomJS (running on CentOS 6.6)
Building from source fixed it for me. Don't forget to use the phantomjs that you built. (instead of the /usr/local/bin/phantomjs if you have it)
sudo yum -y install gcc gcc-c++ make flex bison gperf ruby openssl-devel freetype-devel fontconfig-devel libicu-devel sqlite-devel libpng-devel libjpeg-devel
git clone git://github.com/ariya/phantomjs.git
cd phantomjs
git checkout 2.0
./build.sh
cd bin/
./phantomjs <your JS file>
If someone is using Phantomjs with Sahi the --ignore-ssl-errors option needs to go in your browser_types.xml file. It worked for me.
<browserType>
<name>phantomjs</name>
<displayName>PhantomJS</displayName>
<icon>safari.png</icon>
<path>/usr/local/Cellar/phantomjs/1.9.2/bin/phantomjs</path>
<options>--ignore-ssl-errors=yes --debug=yes --proxy=localhost:9999 /usr/local/Cellar/phantomjs/phantom-sahi.js</options>
<processName>"PhantomJS"</processName>
<capacity>100</capacity>
<force>true</force>
</browserType>
What about shebang?
If you're using shebang to execute phantomjs scripts, use the following shebang line
#!/usr/bin/phantomjs --ignore-ssl-errors=yes
var system = require('system');
var webpage = require('webpage');
// ... rest of your script
Use any of the above answers. i personally like --ignore-ssl-errors=yes since it's irrelevant to validate my loopback web servers' self-signed certificate.
None of the other answers here helped me; it may be that the specific site(s) I was working with were too picky with their HTTP headers. This is what worked:
var page = webpage.create();
page.customHeaders = {
"Connection": "keep-alive"
};
I found out that PhantomJS was using "Keep-Alive" (capitalized), and the connection was not being kept alive. :)
I was getting SSL Handshake Failed yesterday. I tried many combinations of phantomJS options (--ignore-ssl-errors=yes etc.), but none of them worked.
Upgrading to phantomJS 2.1.1 fixed it.
I used the phantomJS installation instructions at https://gist.github.com/julionc/7476620, changing the phantomJS version to 2.1.1.
On the machine you are trying to run phantomjs on to connect to a remote server, run "openssl ciphers." Copy and paste the ciphers listed into the --ssl-ciphers="" command line option. This tells the connecting web server which ciphers are available to use to communicate with your client. If you don't set the ones available on your own machine, it can use any cipher your machine does not understand that the default modern browsers do that are used for the default setting.
phantomjs --web-security=false --ignore-ssl-errors=true scripts.js
The only thing that worked for me was upping phantomjs from 1.9x to 2.x ;)

Start Selenium server automatically for e2e testing

I followed this SO post
to set up my Gruntfile. If I manually downloaded Selenium standalone and specified its location in the file, my test runs successfully. Since I would like to automate this process, I tried the following configuration:
protractor_webdriver: {
start: {
options: {
path: 'node_modules/grunt-protractor-runner/node_modules/protractor/bin/',
command: 'webdriver-manager start'
}
}
};
grunt.loadNpmTasks('grunt-protractor-webdriver');
grunt.registerTask('test', ['protractor_webdriver:start','protractor:run'])
Is there a way to avoid downloading manually? I tried the above but when I ran it, I got the warning:
Running "protractor_webdriver:start" (protractor_webdriver) task
Verifying property protractor_webdriver.start exists in config...OK
File: [no files]
Options: path="node_modules/grunt-protractor-runner/node_modules/protractor/bin/", command="webdriver-manager start", keepAlive=false
Starting Selenium server
>> Selenium Standalone is not present.
Install with webdriver-manager update --standalone
So I still need to download the selenium standalone server manually?
Or maybe I missed some configuration here?
Protractor is a wrapper around WebDriverJS.
It's a nodejs program that interacts with Selenium Server and specific Browser drivers (e.g. ChromeDriver, IEDriver).
So, without using selenium server (at least for IE), you cannot run tests written with protractor. Test scripts send commands to the Selenium Server, which in turn then communicates with the browser driver. See this for a description of the architecture.
In a nutshell, without having started a Selenium server instance beforehand, nothing will happen.
You can run Protractor without Selenium by specifying
directConnect: true
in your respective Protractor configuration file (e.g. protractor.conf.js).

End To End Testing on Headless Server

I am trying to set up an environment for end-to-end testing on a droplet running Ubuntu server 12.04.3 on digital ocean.
What I am trying to achieve in the end is for my jenkins (installed on the one droplet) to be able to run my end-to-end tests. Now, the server is ofcourse headless and the end-to-end tests need to run through a browser (I am using protractor with the selenium standalone server with chromedriver).
My question is: how do I spawn a browser on that machine? I have installed xorg and if I do startx on the server, log out and ssh -X to it, I can manually run the end-to-end tests (a browser pops up on my local machine). But I can get it to work without ssh -X to it, and since jenkins is on the same droplet where the tests are to be run. Well I dont get a browser to spawn.
NOTE: I know I might be missing something really trivial here since I don't fully understand the configuration nor the xorg.
Any hints or a complete answer is very much appreciated, this is giving me gray hair.
Edit: After a little digging I think i got the xorg stuff a bit wrong, i am guessing the purpose of X is to be able to spawn a window on a remote machine ( ie my local machine). And what i am after is more along the lines of a virtual frame buffer such as Xvfb...
There is PhantomJS but with Protractor is buggy and a dead-end.
You can still use Chrome & Firefox headless through docker-selenium or, if you don't like Docker you can do it yourself with ubuntu-headless sample. Both solutions provide Chrome & Firefox by using Xvfb even though there is no real DISPLAY.
UPDATE 2 Seems to be possible to run Xvfb in OSX: http://xquartz.macosforge.org/landing/
UPDATE 1 Mac OSX selenium headless solution:
Enable multi-user remote desktop access to OSX machine
So can test selenium headless on mac. Not headless really but as another user so it doesn't interfere with your current user display.
To do this you need kickstart: http://support.apple.com/en-us/HT201710
Begin using the kickstart utility
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -restart -agent
Activate Remote Desktop Sharing, enable access privileges for all users and restart ARD Agent:
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -configure -access -on -restart -agent -privs -all
Apple Remote Desktop 3.2 or later only
Allow access for all users and give all users full access
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -allowAccessFor -allUsers -privs -all
Kickstart help command
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -help
A lot of angular apps use Travis CI to perform Protractor based end-to-end integration tests on headless vms all the time. I do not know the details of exactly how they do it but I do know that they use a linux service called xvfb which is a headless x windows implementation. Looking at a typical Travis configuration file, it appears that all they do before firing up their web server, selenium server and kicking off Protractor is to call sh -e /etc/init.d/xvfb start to start this service.

Resources