saadtazi / firefox-profile-js

Firefox Profile creation using nodejs and CLI
MIT License
60 stars 30 forks source link

Error: profile.getTemplateDir is not a function when set firefox profile in Protractor #82

Closed tamvo94 closed 7 years ago

tamvo94 commented 7 years ago

I try to use this code with directConnect: true:

var makeFirefoxProfile = function (preferenceMap) {
  var deferred = q.defer();
  var firefoxProfile = new FirefoxProfile();

  for (var key in preferenceMap) {
    firefoxProfile.setPreference(key, preferenceMap[key]);
  }

  firefoxProfile.encoded(function (encodedProfile) {
    var capabilities = {
      browserName: "firefox",
      firefox_profile: encodedProfile
    };

    deferred.resolve(capabilities);
  });
  return deferred.promise;
};

  getMultiCapabilities: function () {
    return q.all([
      makeFirefoxProfile(
        {
          "browser.download.folderList": 2,
          "browser.download.dir": "D:/Automation",
          "browser.helperApps.alwaysAsk.force": false
        }
      )
    ]);
  },

But it show error when start server:

Error: TypeError: profile.getTemplateDir is not a function

at createGeckoDriver (node_modules\protractor\node_modules\selenium-webdriver\firefox\index.js:479:17) at Function.createSession (node_modules\protractor\node_modules\selenium-webdriver\firefox\index.js:631:14)

I have wonder that profile = caps.get(Capability.PROFILE) return string, and getTemplateDir() {return this.template_;} is function in class Profile at node_modules\protractor\node_modules\selenium-webdriver\firefox\index.js I don't know how to fix it. More info:

saadtazi commented 7 years ago

Hi @tamvo94 , I saw that you posted on stackoverflow. I also found this issue on protractor repo...

I feel like there is a mix of firefoxProfile from selenium-webdriver package and from firefox-profile package (this repo)... Which are 2 different ways to generate firefox profile.

I am not really familiar with latest versions of protractor andhow it handles firefox profiles... Do you have a repo with the minimal setup that reproduces the issue?

Thank you,

Saad

tamvo94 commented 7 years ago

Hi, please see my repo: https://github.com/tamvo94/config-firefox You can reprodure it. Error in firefox_profile: encodedProfile Can you help me fix it? Thanks :)

saadtazi commented 7 years ago

If you only pass user prefs, then you should not need firefox-profile: see this

tamvo94 commented 7 years ago

I need to set download files path in FF, so I set firefox profile to do it. So can I remove firefox-profile that I can set "browser.download.dir"???? Thanks.

saadtazi commented 7 years ago

So this is how you should fix it:

var FirefoxProfile = require("selenium-webdriver/firefox").Profile;

var makeFirefoxProfile = function (preferenceMap) {
  // var deferred = q.defer();
  // var firefoxProfile = new FirefoxProfile();
  //
  // for (var key in preferenceMap) {
  //   firefoxProfile.setPreference(key, preferenceMap[key]);
  // }
  //
  // firefoxProfile.encoded(function (encodedProfile) {
  //   var capabilities = {
  //     browserName: "firefox",
  //     marionette: true,
  //     firefox_profile: encodedProfile
  //   };
  //
  //   deferred.resolve(capabilities);
  // });
  // return deferred.promise;
  var profile = new FirefoxProfile();
  for (var key in preferenceMap) {
    profile.setPreference(key, preferenceMap[key]);
  }
  return q.resolve({
    browserName: "firefox",
    marionette: true,
    firefox_profile: profile
  });
};

(I can see the settings in about:config page).

Why: now selenium-webdriver expects a selenium-webdriver/firefox Profile instance when a firefox_profile is provided. In the past, it was expecting a base64 encoded firefox profile. THey probably changed that when they introduced the ability to create firefox profiles in selenium-webdriver.

Note that this has nothing to do firefox-profile. You don't need firefox-profile now that selenium-webdriver introduced firefox.Profile

tamvo94 commented 7 years ago

Thank you for your support <3. I can use firefox profile to set anything. You can close this issue if you have anything else

tamvo94 commented 7 years ago

Hi, I add extension for this firefox profile, but the seem, firefox cannot install it and cannot setPreference both.

  var firefoxExtension = "./extension/firefox_integration/sample.xpi";
  firefoxProfile.addExtension(firefoxExtension);
  return q.resolve({
    browserName: 'firefox',
    marionette: true,
    firefox_profile: firefoxProfile
  });

I also set available profile but it cannot work. var firefoxProfile = new FirefoxProfile("C:/Users/.../AppData/Roaming/Mozilla/Firefox/Profiles/avbq6r8s.TamVo"); I did not know how wrong???

saadtazi commented 7 years ago

@tamvo94, like I mentioned, you no longer need firefox-profile package in your project. Now, you have to refer to selenium-webdriver documentation (and the firefox/profile class...).

I am not sure how firefox extension works with selenium-webdriver profiles...

The selenium-webdriver documentation shows that the Profile constructor seems to accept an opt_str params, which is a Path to an existing Firefox profile directory to use a template for this profile. If not specified, a blank profile will be used... But you'll have to ask your questions to the selenium-webdriver folks using those resources (found here):

- the #selenium channel on freenode IRC
- the selenium-users@googlegroups.com list

Good luck