Reputation: 44823
I am trying to use the ChromeDriver driver for Selenium to run some tests using Chrome, but I'm getting a reference error when I use ChromeOptions
.
I want to force the use of certain options, such as testing it against a particular user profile. Based on the Selenium and ChromeDriver documentation, this is my file test.js
:
opt = new chromeOptions(); // ERROR OCCURS HERE!
opt.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
opt.addArguments("--user-data-dir=C:\\Users\\MyUserAccount\\AppData\\Local\\Google\\Chrome\\User Data");
driver = new ChromeDriver(opt);
// rest of my script goes here
I am executing this using the command node test.js
. This throws the following error on the first line:
\path\to\test.js:1
ction (exports, require, module, __filename, __dirname) { opt = new chromeOpti
^
ReferenceError: chromeOptions is not defined
at Object.<anonymous> (\path\to\test.js:1:73)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3
For what it's worth, if I skip setting options and replace the first four lines of the script with this, it works, but I can't set the options I need to set:
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().
withCapabilities(webdriver.Capabilities.chrome()).
build();
I'm sure that I'm missing something really basic, but I can't figure this one out. How can I set options for Chrome using Selenium and node.js?
Edited to remove some obviously invalid syntax from the samples in some of the documentation I found.
Upvotes: 8
Views: 25727
Reputation: 11
var m_Options = new chrome.Options();
m_Options.addArguments("--user-data-dir=C:/Users/israfil/AppData/Local/Google/Chrome/Profile 2");
m_Options.addArguments("--profile-directory=Default");
m_Options.addArguments("--disable-extensions");
m_Options.addArguments(['user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.11 Safari/537.36"']);
m_Options.setBinaryPath("H://project//nodejs//chrome//chrome.exe");
m_Options.directConnect = false; //setBinaryPath true;
//m_Options.addExtensions();
//m_Options.setProxy();
//m_Options.headless();// Options.headless()}.#headless Options.headless()} Gpu Support ok
//m_Options.addArguments(" --blah"); // not use this codes Importent
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).setChromeOptions(m_Options).build();
Upvotes: -1
Reputation: 151
could you please just try below solution
var webdriver = require("selenium-webdriver");
var chrome = require("selenium-webdriver/chrome");
/**
* Set chrome command line options/switches
*/
var chromeOptions = new chrome.Options();
chromeOptions.addArguments("test-type");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--js-flags=--expose-gc");
chromeOptions.addArguments("--enable-precise-memory-info");
chromeOptions.addArguments("--disable-popup-blocking");
chromeOptions.addArguments("--disable-default-apps");
chromeOptions.addArguments("--disable-infobars");
driver = new webdriver.Builder()
.forBrowser("chrome")
.setChromeOptions(chromeOptions)
.build();
driver.get("http://www.google.com")
Upvotes: 6
Reputation: 2835
This works for me (from this gist)
//import the selenium web driver
var webdriver = require('selenium-webdriver');
var chromeCapabilities = webdriver.Capabilities.chrome();
//setting chrome options to start the browser fully maximized
var chromeOptions = {
'args': ['--test-type', '--start-maximized']
};
chromeCapabilities.set('chromeOptions', chromeOptions);
var driver = new webdriver.Builder().withCapabilities(chromeCapabilities).build();
Upvotes: 1
Reputation: 151370
The following works for me:
var webdriver = require("selenium-webdriver");
var chrome = require("selenium-webdriver/chrome");
// Make sure the PATH is set to find ChromeDriver. I'm on a Unix
// system. You'll need to adapt to whatever is needed for
// Windows. Actually, since you say that you can get a browser to show
// up if you don't try to specify options, your ChromeDriver is
// probably already on your PATH, so you can probably skip this.
process.env["PATH"] += ":/home/user/src/selenium/";
var options = new chrome.Options();
// Commented out because they are obviously not what you want.
// Uncomment and adapt as needed:
//
// options.setChromeBinaryPath("/tmp/foo");
// options.addArguments(["--blah"]);
var driver = new webdriver.Builder().
withCapabilities(options.toCapabilities()).build();
driver.get("http://www.google.com")
I've tested the code above with various values and found that it works.
If you want to see what else you can do with the Options
object, you can open node_modules/selenium_webdriver/chrome.js
and read the source. This is how I figured out the above method.
Upvotes: 17
Reputation: 29032
This seems to be a fundamental OOP JavaScript misunderstanding!
Your problem is:
ChromeOptions opts = new chromeOptions();
You don't instantiate objects this way. Try:
var opt = new ChromeOptions();
Here's proof: http://jsfiddle.net/q5Etk/
If you run that, we get "Unexpected Identifier".
Uncomment the var
bit and comment the Cat cat
bit and see it work.
EDIT After your edit:
You are specifying chromeOptions()
. It's ChromeOptions()
capped C
Upvotes: -1