nw.js chromedriver won't run from outside of the project folder

232 Views Asked by At

I am trying to set up automated tests on an nw.js based app using selenium-python with chromedriver and for practical reasons (frequent reinstallation...) I want to keep chromedriver separated from the rest of the files in another folder. My tests work only when the chromedriver is located at the same folder as the rest of the project (along with nw.exe). If I try to place it anywhere else and alter paths with 'binary_location', 'chrome_driver_binary' and 'add_argument' accordingly, I always end up with exceptions such as

selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary

or

selenium.common.exceptions.WebDriverException: Message: unknown error: Failed to create a Chrome process

Nw.js documentation wasn't helpful as it only says the following:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("nwapp=/path/to/your/app")

driver = webdriver.Chrome(executable_path='/path/to/nwjs/chromedriver', chrome_options=chrome_options)

Thanks in advance for any ideas.

1

There are 1 best solutions below

0
JohnA On

I was able to do this but I used ruby. It should be possible to translate to python.

Also I'm running Ubuntu 20.04. No idea if this will all work in Windows.

chrome_options.add_argument("nwapp=/path/to/your/app")

This line doesn't specify, but the nwapp should be a directory. If you build your app using phoenix builder, then you probably have a line like this in package.json:

"package:linux": "npm run build:prod && build --tasks linux-x64 --mirror https://dl.nwjs.io/ .",

For me, this command creates a directory called packages/myapp-0.0.1-linux-x64 Inside of there you should see an executable "myapp" and the manifest "package.json"

The nwapp= line should point to this directory. It will use the manifest file to get the app name. In ruby that looks like this:

myapp_dir   = File.join(root_dir, 'packages', 'myapp-0.0.1-linux-x64')
chrome_options.add_argument("nwapp=" + myapp_dir)

executable_path='/path/to/nwjs/chromedriver'

When you install nw.js with npm, it will create a directory tree: node_modules/nw/nwjs Inside that directory is the chromedriver executable.

The "executable_path" is the full path to chromedriver executable. In ruby, it looks like this:

chromedriver_path = File.join(root_dir, 'node_modules', 'nw', 'nwjs', 'chromedriver')
Selenium::WebDriver::Chrome::Service.driver_path = chromedriver_path
@driver = Selenium::WebDriver.for :chrome, options: chrome_options

Ruby does everything relative to where you invoke it, so I added a script in the root of my nw.js app and then added this line to package.json:

"e2e": "ruby ./e2e.rb"

Then this runs via npm run e2e