问题
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?
回答1:
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
回答2:
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...");
});
},
来源:https://stackoverflow.com/questions/26360592/protractor-with-any-headless-browser