Search code examples
javascriptecmascript-6selenium-chromedriverchrome-optionsgalen

Access Chrome Options in JavaScript (optional: from Galen test)


I am using Galen (a JS tool for testing layout of a HTML page). It is configured from a .js file which can't use ES6 JS :-(

I need to load/amend ChromeOptions from the Chrome driver but I can't figure out how to access it. I see lots of examples of how to set options but when I do so using:

var options = new chrome.Options();

I get an error saying: ReferenceError: "chrome" is not defined I have tried using require() and load() functions but with the require I get similar not defined errors (ES6 issue I think) and with load I can't seem to point it at a script the works, I've tried:

load("../../npm_modules/selenium-webdriver")
load("../../npm_modules/selenium-webdriver/chrome")
load("chrome")
etc.

It seems that is not the webdriver that Galen is using, but how do I find the one that it IS using? and is load() what to use to load it?


Solution

  • Galen uses the Rhino JS Engine. Which means that you can directly call Java classes from within the .js file that configures it, thus allowing for an answer like this:

    importClass(org.openqa.selenium.chrome.ChromeOptions);
    importClass(org.openqa.selenium.chrome.ChromeDriver);
    
    var options = new ChromeOptions();
    options.addArguments("--headless");
    var driver = new ChromeDriver(options);
    

    Trick is to know how to import the class you need from the Selenium Chrome driver!

    Found this on Galen's Google Groups site. Used the Selenium HQ API, documented on GitHub, for further expansion of what I was doing. HTH.