spectron icon indicating copy to clipboard operation
spectron copied to clipboard

goog:chromeOptions not set when passed into `webdriverOptions` prop

Open MauriceWebb opened this issue 3 years ago • 3 comments

I am trying to pass google chrome options into the webdriverOptions prop but it isn't working.

When initializing a page object, I instantiate a Spectron Application instance using the following default options (non-related code has been truncated for simplicity):

Base.page.js

   ...
   global.downloadDir = path.join(__dirname, '../tempDownload')
   ...
   class BasePage {
      constructor(options = {}) {
         const defaultOpts = {
            path: require('electron'),
            args: [ path.join(__dirname), '../../../'],
            env: { ... },
            webdriverOptions: {
               'goog:chromOptions': {
                   prefs: {
                        directory_upgrade: true,
                        prompt_for_download: false,
                        download: {
                            directory_upgrade: true,
                            prompt_for_download: false,
                            default_directory: downloadDir
                        },
                        profile: {
                            default_content_settings: { popups: 0 },
                            default_content_setting_values: { automatic_downloads: 1 }
                        },
                        safebrowsing: { enabled: false }
                    }
               }
            }
         }
         ...
         options = { ...defaultOpts, ...options }
         this.app = new Application(options)
      }
   }

At first, I assumed it was working because after the app has been started, I log the app's settings using a logger method on the page object:

   ...
   await this.app.start()
   this.log('webdriverOptions: ', JSON.stringify(this.app.getSettings().webdriverOptions, null, 2))
   ...

This prints the following to the console:

Terminal Console log

webdriverOptions: {
   "goog:chromeOptions": {
       "prefs": {
          "directory_upgrade": true,
          "prompt_for_download": false,
          "download": {
             "directory_upgrade": true,
             "prompt_for_download": false,
             "default_directory": "/Users/myUser/Code/MyProjectRepo/my.electron.app/automatedtests/tempDownload"
           },
          "profile": {
             "default_content_settings": {
                "popups": 0
             },
             "default_content_setting_values": {
                "automatic_downloads": 1
             }
          },
           "safebrowsing": {
             "enabled": false
         }
      }
   }
}

How can I verify that the webdriverOptions are indeed set and working properly? When the test runs and gets to a download, the prompts still show up despite having passed "prompt_for_download": false and "automatic_downloads": 1, I still see the download prompt occur, and the download folder is not the default "../tempDownload" as specified in the defaultOptions. It is still prompting the download to the "Downloads" folder:

image

The javascript error that is shown in the screen shot pops-up in result of spectron loosing focus to the browser due to the prompt. This error is more of a cascading error and is not the error in question.

P.S.

It should be noted that I have also tried excluding the 'goog:chromeOptions' prop and passed the "prefs" object directly into the webdriverOptions prop as well. Still, the same result:

...
   global.downloadDir = path.join(__dirname, '../tempDownload')
   ...
   class BasePage {
      constructor(options = {}) {
         const defaultOpts = {
            path: require('electron'),
            args: [ path.join(__dirname), '../../../'],
            env: { ... },
            webdriverOptions: {
                prefs: {
                     directory_upgrade: true,
                     prompt_for_download: false,
                     download: {
                         directory_upgrade: true,
                         prompt_for_download: false,
                         default_directory: downloadDir
                     },
                     profile: {
                         default_content_settings: { popups: 0 },
                         default_content_setting_values: { automatic_downloads: 1 }
                     },
                     safebrowsing: { enabled: false }
                 }
            }
         }
         ...
         options = { ...defaultOpts, ...options }
         this.app = new Application(options)
      }
   }

package.json

   ...
   "dependencies": {
      ...
      "electron": "^9.3.5",
      "spectron": "^11.0.0",
      ...
   }
   ...

Questions:

  1. How can I verify that the webdriverOptions are indeed set and working properly?
  2. Are there any examples available for correctly passing "google chrome" options into a new spectron instance (for handling downloads)?

MauriceWebb avatar Jun 14 '21 18:06 MauriceWebb

According to the source code,Try writing it like this

 webdriverOptions: {
      capabilities: {
             'goog:chromOptions': {
                   prefs: {
                        directory_upgrade: true,
                        prompt_for_download: false,
                        download: {
                            directory_upgrade: true,
                            prompt_for_download: false,
                            default_directory: downloadDir
                        },
                        profile: {
                            default_content_settings: { popups: 0 },
                            default_content_setting_values: { automatic_downloads: 1 }
                        },
                        safebrowsing: { enabled: false }
                    }
               }
      }
   }

and you need to add the necessary attributes, as shown in the application file

qiupo avatar Jun 15 '21 06:06 qiupo

@StevenT1 Thank you for your response. I was just looking into that and trying it out. Looks like i'm starting to run into some errors after adding the necessary attributes. This is what I am working with now:

webdriverOptions: {
  capabilities: {
      'goog:chromeOptions': {
          binary: path.join(
              __dirname,
              `../../../node_modules/spectron/lib/${process.platform === 'win32' ? 'launcher.bat' : 'launcher.js'}`
          ),
          args: [
              `spectron-arg0=${path.join(__dirname, '../../../')}`
          ],
          windowTypes: ['app', 'webview'],
          prefs: {
              directory_upgrade: true,
              prompt_for_download: false,
              download: {
                  directory_upgrade: true,
                  prompt_for_download: false,
                  default_directory: downloadDir
              },
              profile: {
                  default_content_settings: { popups: 0 },
                  default_content_setting_values: { automatic_downloads: 1 }
              },
              safebrowsing: { enabled: false }
          }
      }
  }
}

which is working, but I am getting errors like:

image

I believe I will need to pass some args into it per: https://stackoverflow.com/questions/50642308/webdriverexception-unknown-error-devtoolsactiveport-file-doesnt-exist-while-t/50642913#50642913

I'll update this issue with the resolution when I get it working. Thanks!

MauriceWebb avatar Jun 15 '21 13:06 MauriceWebb

@StevenT1 Just wanted to note, that I have not been able to solve this yet. Still having issues with the DevToolsActivePort files doesn't exist error. I've google and applied all I can find to the args and still no luck. I've also updated the first arg in the 'goog:chromeOptions''s args prop to the following:

   ...
   args: [
      `spectron-path=${path.join(__dirname, '../../../')}`
   ]
   ...

Any further insight or examples would be of great value, anyone.

MauriceWebb avatar Jun 16 '21 19:06 MauriceWebb