Selenium: Opening an extension's popup window Selenium: Opening an extension's popup window selenium selenium

Selenium: Opening an extension's popup window


The way I have gotten around this is to open the page directly.

In other words, all the pages in a Chrome extension are just HTML pages built into the package. As such you can open them directly with a specially crafted URL.

The first step is to find out your package ID. It is a unique 32-character string. This value is derived from the key used to sign the package so it should be fairly consistent. The quickest way to find this value is:

  1. Open Chrome with your extension installed
  2. Go to the Extension list (Menu > More Tools > Extensions)
  3. At the top make sure the "Developer mode" checkbox is checked
  4. Find your extension in the list and there will be a entry called "ID:"
  5. Copy down this value

As an example, the app launcher for Drive is "ID: lmjegmlicamnimmfhcmpkclmigmmcbeh"

Knowing this value means you can now access any page within the package.

String EXTENSION_PROTOCOL = "chrome-extension";String EXTENSION_ID = "lmjegmlicamnimmfhcmpkclmigmmcbeh";indexPage = EXTENSION_PROTOCOL + "://" + EXTENSION_ID + "/index.html";optionsPage = EXTENSION_PROTOCOL + "://" + EXTENSION_ID + "/options/options.html";driver.get(indexPage);

There are a few drawbacks to this:

  • It doesn't test the button itself
  • The pages are always rendered full-screen so you may get some layout issues

If you can work around those, you can at least test a majority of the functionality of a Chrome extension.


OK, I managed to get this working.I used Java's Robot to send the required keys I set to open the extension (Control + Shift + Y), and it works, the popup opens. sendKeys didn't work because Selenium disabled the ability to send keys that trigger the browser's functionality, so it's a nice hack I guess.

import java.awt.*;import java.awt.event.KeyEvent;private void openExtension() throws AWTException {    Robot robot = new Robot();    robot.keyPress(KeyEvent.VK_CONTROL);    robot.keyPress(KeyEvent.VK_SHIFT);    robot.keyPress(KeyEvent.VK_Y);}

Edit: And of course, it would be a good idea to release these keys after pressing (else they would remain pressed). So the final solution would look something like:

    public void openExtension() {    robotKeyPress(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_Y);    robotKeyRelease(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_Y);}private void robotKeyPress(int... keys) {    for (int k : keys) {        robot.keyPress(k);    }}private void robotKeyRelease(int... keys) {    for (int k : keys) {        robot.keyRelease(k);    }}