简体   繁体   中英

Chrome Extension Not Running in Selenium Javascript Webdriver Test

I have a Selenium Javascript Webdriver test that is working as expected, giving no errors, except that a Chrome Extension that should change the title of the page, then get a cookie is not working. When I have run the extension manually on a test page it works as expected, so I am pretty sure the problem is how I'm calling the extension. Any pointers would be greatly appreciated.

I do have one question about the "binary" chromeOption, from the docs I looked at it looked like it was just the folder that held the extension, but then those same docs had their "extensions" in chromeOption pointing at a file in the same folder. What exactly am I supposed to put in "binary"?

Thanks again!

Code Snippet:

    const path = require('path');
    const chromePath = require('chromedriver').path;
    const webdriver = require('selenium-webdriver');
    const chrome = require('selenium-webdriver/chrome');
    const until = webdriver.until;
    var chromeOptions = webdriver.Capabilities.chrome();

    var service = new chrome.ServiceBuilder(chromePath).build();
    chrome.setDefaultService(service);

    var builder = new webdriver.Builder();
    var options = new chrome.Options();
    var preferences = new webdriver.logging.Preferences();
    var driver;

    preferences.setLevel(webdriver.logging.Type.BROWSER, webdriver.logging.Level.ALL);
    options.setLoggingPrefs(preferences);

    var extensionArray = [""];

    async function AppTest() {

        let driver = builder
                        .forBrowser('chrome')
                        .withCapabilities({
                            'browserName': 'chrome',
                            'chromeOptions':
                            {
                                binary: 
    // Folder containing a copy of the extension

'/Users/MyUserName/Desktop/Testing/chrome-extensions',
                                args: [],
    // Local copy of the extension in the same folder as the test
                                extensions: ['./chrome-extension/extension-demo.crx']
                            }
                        })
                        .setChromeOptions(options)
                        .build();

        // Tests

        await driver.get('https://testURL.com');

        await driver.manage().getCookie("test").then(function(cookie){
            console.log("test", cookie);
        });

        await driver.quit();
    }

I'm not sure what the "binary" key is for as I have never encountered it.

You can see how I was able to add an extension in Java at this post . The crux is that you cannot add the extension as-is; it needs to be converted to base-64.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM