How can I configure the firefox binary location in Protractor? - angularjs

I am successfully running Protractor tests with Chrome, specifying the path to my chrome binary using the following section in my Protractor configuration:
capabilities: {
// You can use other browsers
// like firefox, phantoms, safari, IE
'browserName': 'chrome',
"chromeOptions": {
binary: 'C:/BuildSoftware/Chrome/Application/chrome.exe',
}
This works.
My Firefox is also installed in a non-standard location.
Is there an equivalent way to specify the binary for Firefox in the protractor config?

UPDATED: See newer answer below: https://stackoverflow.com/a/28313583/800699
It seems you have to start the Selenium Server by yourself with custom arguments for firefox driver.
See Protractor test is not starting on Firefox
More options for firefox driver (including custom firefox binary location) can be found here:
https://code.google.com/p/selenium/wiki/FirefoxDriver
P/S: Browsing the firefox driver source reveals more light:
https://code.google.com/p/selenium/source/browse/javascript/node/selenium-webdriver/firefox/index.js
You can try adding:
"browserName": "firefox",
"firefox_binary": "path/to/custom/firefox",
"binary_": "path/to/custom/firefox"

Protractor now supports setting the firefoxPath directly in the configuration file, when using "direct connect" (i.e., without a selenium server). See the reference config file:
https://github.com/angular/protractor/blob/master/docs/referenceConf.js#L67
Add firefoxPath to the config file, at the top-level. It is a string that should be the path to your firefox binary. You will also need directConnect: true in the config.
For more details (handy to see all the doc that changed at once) check out the change that added this support (in Oct 2014).

It's 2018 and it appears you can set a specific binary argument within the 'moz:firefoxOptions' section of the capabilities like so:
capabilities: {
browserName: 'firefox',
'moz:firefoxOptions': {
args: [...Your args here...],
binary: '/Path/To/Custom/firefox'
}
}
Disclaimer: I ran this with directConnect: true in my protractor config also. I'm not sure if it works if you're passing the command to the Selenium server.
Reference: https://github.com/mozilla/geckodriver/blob/master/README.md#mozfirefoxoptions

I added my custom binary location to PATH variable.
export PATH="/custom-firefox-location:$PATH"
That makes my custom firefox version to be available during the session and when running protractor it uses that.

To elaborate Monkpit's answer which is correct .Below code is worked for me , verified and tested in protractor v 5.4.2 .
Only thing is needed to open the cmd via admin mode to avoid the permission error since I installed Firefox in program files.
capabilities: {
browserName: 'firefox',
'moz:firefoxOptions': {
args: ['--verbose'],
binary: 'C:/Program Files/Mozilla Firefox/firefox.exe'
//Need to start cmd via admin mode to avoid permission error
}
},
Full code
exports.config = {
framework: 'jasmine',
directConnect: false, //Start protractor without start the selenium server using webdriver-manager start. default value is fales
//This is only for chrome and firefox and use drivers instead of selenium server
capabilities: {
browserName: 'firefox',
'moz:firefoxOptions': {
args: ['--verbose'],
binary: 'C:/Program Files/Mozilla Firefox/firefox.exe'
//Need to start cmd via admin mode to avoid permission error
}
},
//set to true So each spec will be executed in own browser instance. default value is false
//restartBrowserBetweenTests: true,
jasmineNodeOpts: {
//Jasmine provides only one timeout option timeout in milliseconds don't add ;
defaultTimeoutInterval: 180000
},
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: ['src/com/sam/scriptjs/iframes.spec.js']
}

Related

Can I make protractor use the IEDriverServer.exe webdriver-update installed in node_modules?

I'm writing acceptance tests for my angular web application project. They're run via protractor, and work just fine on chrome. But when I try to run them on Internet Explorer 11, I get a failure complaining that "The path to the driver executable must be set by the webdriver.ie.driver system propery". However, I have my project configured to download the IE driver to the same place as the chromedriver executable.
While I'm sure I could move the IE driver excutable to a folder stored in my PATH env variable, then every developer on the project would have to do the same or update their PATHs to point to the driver.
My question is - is there a simple configuration I'm missing to make this IE driver available to protractor just as Chrome's driver is?
My package.json:
{
//...
"scripts": {
"webdriver-update": "webdriver-manager update --ie",
"preacceptance-tests": "npm run webdriver:update -- --standalone",
"acceptance-tests": "protractor",
//...
}
My protractor.conf.js:
exports.config = {
baseUrl: 'http://localhost:3000/',
specs: [
'src/**/**test.ts',
'src/**/*test.ts'
],
capabilities: {
'browserName': 'internet explorer' //If I put chrome here, the tests pass
},
onPrepare: function() {
browser.ignoreSynchronization = true;
},
seleniumServerJar: "node_modules/protractor/selenium/selenium-server-standalone-2.51.0.jar",
useAllAngular2AppRoots: true
};
I run "npm run acceptance-tests", and chromedriver.exe and IEDriverServer.exe are downloaded to my node_modules/protractor/selenium folder. Protractor seems to be aware of the chromedriver, but why can't it see the IEDriverServer?
I can't seem to find an answer anywhere, other than manually pointing my PATH to this folder. It seems like I shouldn't have to if protractor can find the chromedriver...
As far as I know from having checked the Selenium source, there's only one workaround.
If an executable named IEDriverServer.exe exists in the current working directory (which is explicitly checked, and which you can query with process.cwd()), then that instance will be chosen and the PATH check bypassed (the error message is a little misleading).
You can follow the logic in the Selenium source here and here.

Is `webdriver-manager start` necessary?

I'm delving into the world of Protractor tests for AngularJS.
All the tutorials recommend I execute the following after webdriver-manager update and prior to executing the test:
webdriver-manager start
According to the webdriver-manager man, the start command will 'start up the selenium server'. True enough, once I run the above command, I can see something at http://127.0.0.1:4444/wd/hub
My questions is: is the above necessary?
I currently run my tests without the above command.
All I do is:
webdriver-manager update
php -S localhost:8000 -t dist/
protractor ./test/protractor.config.js
My tests run as expected even though I've excluded webdriver-manager start.
Can someone please explain why webdriver-manager start is necessary?
:EDIT:
My protractor/fooTests.js:
exports.config = {
directConnect: true,
capabilities: {
'browserName': 'chrome'
},
specs: ['protractor/fooTests.js'],
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
}
};
My protractor/fooTests.js:
describe('test for the bar code', function() {
it('should login', function() {
browser.get('http://localhost:8000');
element(by.model('password')).sendKeys('123456');
element(by.css('[type="submit"]')).click();
});
it('should inspect element ', function() {
expect(element(by.id('foo-script')).isPresent()).toBe(true);
console.log('Login Success');
});
});
Protractor is sending commands to Selenium and Selenium is communicating with the browsers using its drivers.
webdriver-manager start
is starting Selenium.
There are 3 basic options:
directConnect. That makes protractor communicate with the selenium drivers directly, without using the Selenium server. However, the functionality of this option is limited:
directConnect: true - Your test script communicates directly Chrome Driver or Firefox Driver, bypassing any Selenium Server. If this is true, settings for seleniumAddress and seleniumServerJar will be ignored. If you attempt to use a browser other than Chrome or Firefox an error will be thrown.
Connecting to an already running selenium server (either local or remote), specified by seleniumAddress. A server can be started using the webdriver-manager start script.
Starting the server from the test script.
You can explore all the options in the documentation https://github.com/angular/protractor/blob/master/docs/server-setup.md

protractor fails to run with directConnect

I'm trying to run protractor by connecting directly to Chrome, not running Selenium. As the docs say, this is possible by setting the directConnect to true in the config file.
When I run protractor with directConnect: true, I get:
Using ChromeDriver directly...
[launcher] Running 1 instances of
WebDriver Error: spawn ENOENT
at errnoException (child_process.js:1001:11)
at Process.ChildProcess._handle.onexit (child_process.js:792:34) [launcher] Process exited with error code 1
If I run protractor without directConnect and instead specify my selenium url, everything runs fine.
I'm running Protractor version 1.6.1, Chrome version 41.0.2272.35 beta-m (64-bit), and Chromedriver version 2.13.0.
Update: While the solution below worked for me, after talking with a protractor dev I realized that if I run webdriver-manager update to install a local chromedriver, then I don't need to set the chromeDriver setting in my protractor config.
I had the same issue and solved it by varying the chromeDriver path setting in protractor-conf.js depending on whether I was on Windows or OSX/Linux.
Solution and writeup below assumes you are using chromedriver provided by npm install chromedriver. Also this solution worked with protractor 3.2.2 and chromedriver 2.21.2.
Protractor+chromedriver worked on OSX and Linux but I was getting ENOENT errors on Windows. I have filed an issue here and have also documented a workaround.
The issue (I think) is that childProcess.spawn has issues on Windows (see a list of issues here) and the node_modules/chromedriver/bin/chromedriver file will not correctly run when called via childProcess.spawn - likely because this file is not executable and Windows doesn't know to use the node binary to interpret the file.
The workaround is to provide the path to the windows executable when running on Windows. It is easy enough - though hackish - to vary the chromeDriver arg in protractor-conf.js as demonstrated below :
protractor-conf.js for all three OS:
var chromeDriverPath = process.platform === 'win32' ? 'node_modules/chromedriver/lib/chromedriver/chromedriver.exe' : 'node_modules/chromedriver/bin/chromedriver';
exports.config = {
directConnect: true,
chromeDriver: chromeDriverPath,
specs: [
'features/*.feature'
],
capabilities: {
browserName: 'chrome',
platform: 'ANY',
chromeOptions: {
args: ['--test-type']
}
}
}
Hope this helps.
ENOENT means Error, NO such ENTity, and relates to the file system.
Check you have the right path set in the chromeDriver option.
capabilities : {
chromeOptions: {
"debuggerAddress":"127.0.0.1:8088"
}
},
directConnect: true,
chromeDriver : "../bin/chromedriver_2.15.322448.exe"

Running Protractor tests on Browserstack Automate

I'm developing an AngularJS app and want to do end-2-end testing with Protractor. I would like to benefit from the suite of test browsers available at Browserstack and run the tests on Browserstack Automate instead of a local Selenium server.
How do I set up a system to run these tests?
NOTE: These instructions are only relevant for Protractor versions older than v3.0. Protractor 3.0 includes built-in support for Browserstack.
Prerequisites
You will need to have node and npm installed. Check your node version with node --version to ensure it is greater than v0.10.0.
Ready?
1. Install Protractor
Use npm to install Protractor globally with:
npm install -g protractor
If you get errors, you might need to run the above command as sudo.
Here's a more detailed tutorial for installing and using Protractor.
2. Install Browserstack Selenium web driver
EDIT: #elgalu pointed out in the comments this step is not necessary. Apparently the BrowserStackLocal tunnel (set up in step 4) is enough.
Following Browserstack's instructions for setting up node.js, install the seleniun web driver:
npm install -g browserstack-webdriver
3. Set up Protractor configuration
Create a protractor.conf.js file (see documentation for BrowserStack's supported capabilities):
exports.config = {
capabilities: {
'browserstack.user' : 'my_user_name',
'browserstack.key' : 'my_secret_key',
// Needed for testing localhost
'browserstack.local' : 'true',
// Settings for the browser you want to test
// (check docs for difference between `browser` and `browserName`
'browser' : 'Chrome',
'browser_version' : '36.0',
'os' : 'OS X',
'os_version' : 'Mavericks',
'resolution' : '1024x768'
},
// Browserstack's selenium server address
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
// Pattern for finding test spec files
specs: ['test/**/*.spec.js']
}
Change your user name and secret key to the ones given on the Browserstack Automate page. If you're logged in to Browserstack, the instructions for setting up node.js will have you user and key substituted in the examples, and you can just copy-paste the javascript from there.
The same page also has a tool for generating the code for different test browser settings.
4. Download and run BrowserStackLocal
Download the BrowserStackLocal binary from the node.js instructions page.
Make the following changes to the command below, and run the binary to open the Browserstack tunnel required for testing.
Change your secret key in the command. Again, your_secret_key will be automatically substituted in the node.js guide, if you're logged in to Browserstack.
Change the port number to match the port you're hosting your AngularJS files at on localhost. The example uses port 3000.
./BrowserStackLocal your_secret_key localhost,3000,0
5. Run the tests
With everything ready for testing, run your tests:
protractor protractor.conf.js
You can watch the test run on Browserstack Automate and even see an updating live screenshot of the test browser.
Protractor from version 3.0.0 onwards has added inbuilt support for BrowserStack.
You simply need to add the following two parameters in your conf.js to launch the test on BrowserStack:
browserstackUser: '<username>'
browserstackKey: '<automate-key>'
Your username and automate key can be found here, after you have logged in to your account.
Hence, lets say you wish to run your test on Chrome 50 / OS X Yosemite, your conf.js should look something like this:
exports.config = {
specs: ['spec.js'],
browserstackUser: '<username>',
browserstackKey: '<automate-key>',
capabilities: {
browserName: 'Chrome',
browser_version: '50.0',
os: 'OS X',
os_version: 'Yosemite'
},
};
If you wish to run tests in parallel on different browser and OS combinations, you can use the multiCapabilities as given below:
exports.config = {
specs: ['spec.js'],
browserstackUser: '<username>',
browserstackKey: '<automate-key>',
multiCapabilities: [
{
browserName: 'Safari',
browser_version: '8.0',
os: 'OS X',
os_version: 'Yosemite'
},
{
browserName: 'Firefox',
browser_version: '30.0',
os: 'Windows',
os_version: '7'
},
{
browserName: 'iPhone',
platform: 'MAC',
device: 'iPhone 5S'
}
]
};
Some helpful links:
Code Generator - Helps you configure the capabilities to test on different various browser and OS combinations especially mobile devices.
Sample Github project for Protractor-BrowserStack - This should help you get started.
Hello! To only run the test against Browserstack, you may need to skip Step 4 from Niko Nyman answer, and in your conf.js you should have something like here's the one that I've used (+ report), then run Step 5:
var HtmlReporter = require('protractor-html-screenshot-reporter');
var reporter=new HtmlReporter({
baseDirectory: './protractor-result', // a location to store screen shots.
docTitle: 'Report Test Summary',
docName: 'protractor-tests-report.html'
});
// An example configuration file.
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://hub.browserstack.com/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome',
'version': '22.0',
'browserstack.user' : 'user_name',
'browserstack.key' : 'user_key',
'browserstack.debug' : 'true'
},
// Spec patterns are relative to the current working directly when
// protractor is called.
specs: ['./specs/home_page_spec.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 30000
},
onPrepare: function() {
jasmine.getEnv().addReporter(reporter);
}
};
We just wrote a blog post and open-source modules for this.
http://www.blog.wishtack.com/#!Cross-browser-testing-test-automation-with-Protractor-and-Browserstack/cuhk/554b3b5f0cf27313351f1163
wt-protractor-boilerplate
wt-protractor-runner
wt-protractor-utils

Getting Selenium Going for Use with Protractor for my AngularJS app

I'm building an AngularJS app on Windows. I want to create end-to-end tests with Jasmine. From my understanding, I need protractor to run these kinds of tests. For that reason, I've added the following to my package.json:
"devDependencies": {
...
"grunt-protractor-runner": "0.2.4",
"selenium-webdriver":"2.41.0",
...
}
In my gruntfile.js, I've configured Protractor as such:
grunt.initConfig({
protractor: {
options: {
configFile: "node_modules/protractor/referenceConf.js", // Default config file
keepAlive: true, // If false, the grunt process stops when the test fails.
noColor: false, // If true, protractor will not use colors in its output.
args: {
// Arguments passed to the command
}
},
tests: {
options: {
configFile: "tests/config/e2e.conf.js",
args: {} // Target-specific arguments
}
},
}
});
I'm then running the protractor:tests target. The contents of e2e.conf.js look like the following:
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
// Capabilities to be passed to the webdriver instance.
capabilities: {
'browserName': 'chrome'
},
// Spec patterns are relative to the location of the spec file. They may
// include glob patterns.
specs: ['../../tests/e2e/user-tests.e2e.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
}
};
Now, when I run grunt from the command-line, I get an error that says:
Using the selenium server at http://localhost:4444/wd/hub
C:\Projects\MyProject\node_modules\grunt-protractor-runner\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver\promise.js:1702
throw error;
^
Error: ECONNREFUSED connect ECONNREFUSED
...
I don't understand why I'm getting this error. I see in the Protractor Getting Started Guide that it expects a selenium standalone server to be running. However, I thought that was the purpose of the Grunt task runner: start the selenium server. I see webdriver-manager in node_modules\grunt-protractor-runner\node_modules\protractor\bin, however, if I change to that directory and run webdriver-manager update from the command-line, I get an error that says:
'webdriver-manager' is not recognized as an internal or external command,
operable program or batch file.
How do I get the selenium piece going so that I can run end-to-end tests with protractor?
Thank you!
Have you tried with "node" command ?
It worked for me :
node .\node_modules\grunt-protractor-runner\node_modules\protractor\bin\webdriver-manager update
This should download for you chromedriver and selenium server. If not, ou can also manually download/extract :
http://selenium-release.storage.googleapis.com/2.40/selenium-server-standalone-2.40.0.jar
https://chromedriver.storage.googleapis.com/2.9/chromedriver_win32.zip
in :
src/test/config/selenium/selenium-server-standalone.jar
src/test/config/selenium/chromedriver.exe
An other thing is that you need to be sure that Chrome is isntalled in :
<WINDOWS_USERS_FOLDER>\<USERNAME>\AppData\Local\Google\Chrome\Application\chrome.exe
First up:
There are many components in there that I don't understand :)
Feel free to add comments to help me improve my answer.
I do know some Selenium so here goes.
seleniumAddress: 'http://localhost:4444/wd/hub',
To me, this line indicates that you are trying to run the Selenium Grid Server.
Here's how to start it: (yes, it needs to be started manually as far as I know)
From a separate console, run the following:
java -jar selenium-server-standalone-2.41.0.jar -role hub
#now wait a few seconds for the hub to start
java -jar selenium-server-standalone-2.41.0.jar -role node -hub http://localhost:4444/grid/register
What did we just do?
Started a hub. This hub is like a test distributor - it receives the requests.
Started a node. (You can start any number of nodes). This node is what will actually conduct the test.
You can verify that this server was started successfully. Just visit the local host link on your browser.
Gotchas:
Check that your firewall is not giving you problems. I've had crippling issues getting started on Windows 7 and finally moved over to Ubuntu (but that's probably just my situation).
Open ports 4444 (for hub), 5555(for node) for both incoming and outgoing connections on Windows Firewall.

Resources