Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goog:chromeOptions not set when passed into webdriverOptions prop #973

Open
MauriceWebb opened this issue Jun 14, 2021 · 3 comments
Open

Comments

@MauriceWebb
Copy link

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)?
@qiupo
Copy link

qiupo commented Jun 15, 2021

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

@MauriceWebb
Copy link
Author

@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
Copy link
Author

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants