12

I have created a chrome extension that makes API calls to database and fetches some data relevant to a website that is currently open. For example, if I open target.com and click on extension that it will give you data relevant to target.com.

I am trying to write automated tests for it through selenium web driver which I can run on a regular basis for regression testing. To test the extension, I need to first open the extension (generally we do it by clicking on the extension icon).

I have tried different ways of attempting to click on the extension icon but have not been successful. (For example, using the keyboard shortcut ALT - LEFT_ARROW - SPACE but that does not work through webdriver).

I have also tried this (mentioned here):

options = webdriver.ChromeOptions()
options.add_argument("--app-id = mbopgmdnpcbohhpnfglgohlbhfongabi")

But above code does not help in opening extension.

I would appreciate any thoughts on how can I do this using python in Selenium Webdriver.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
curiousJ
  • 203
  • 1
  • 4
  • 11

8 Answers8

4

Selenium supports interaction with web view only - so this is not possible.

I have been searching for solution to this one for some time - and it turned out there is none.

https://code.google.com/p/selenium/issues/detail?id=7805

http://grokbase.com/t/gg/selenium-developer-activity/148xndmkna/issue-7805-in-selenium-clicking-on-chrome-extension-to-open-popup

Venkatesh Achanta
  • 624
  • 1
  • 14
  • 31
4

Had the same problem. Worked around it by using the link: chrome-extension://<the extension identity>/html/login.html - instead of the icon. This way, I can test all the functions of the extension.

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
Kathy Chow
  • 41
  • 2
  • 2
    While true, the behavior differs a bit from being open in the popup. – Xan Dec 11 '15 at 10:17
  • Is it possible to try the subprocess.call and applescripts? – Kathy Chow Dec 11 '15 at 20:23
  • @KathyChow Can you provide further details? I ave to load a popup on clicking Chrome Extension button – Volatil3 Jan 23 '16 at 08:49
  • 1
    @Volatil3 Yes, I also ave to load a popup on clicking Chrome Extension button. But sometimes, using the link: chrome-extension:///html is much useful. – Kathy Chow Jan 25 '16 at 20:47
  • 1
    Most extensions, I would assume, interact with the current active tab - this doesn't really help in this case as presumably the extension also becomes the active tab? – oatsoda Jan 20 '20 at 15:16
4

We have similar requirement, working on chrome add-on with Selenium WebDriver. As '@Aleksandar Popovic' said, we can't click chrome extension icon with WebDriver, as icon is out of the Webpage.

We make use of sikuli (Automation tool that make use of image recognition), to click the chrome add-on. After that add-on popup will be another browser window, so use switch window to perform actions on add-on popup.

Here is the sample code in Java using both Selenium Webdriver and Sikuli.

Sikuli will run based on image recognition. Before running the code, the screen shot of the chrome browser and crop it so that only Addon is available in the image. Save that image as "AddonIcon.png".

Sikuli will match that image (In our case AddonIcon.png ) on screen and simulate the click action on it.

import java.io.File;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.sikuli.script.App;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
public class PageTest {

    public static void main(String[] args) {
        // Opening chrome with that addon
        ChromeOptions options = new ChromeOptions();
        options.addExtensions(new File("Path to ur chrome addon (.cxt file)"));     
        System.setProperty("webdriver.chrome.driver", "path to chromedriver.exe");
        WebDriver driver = new ChromeDriver(options);
        driver.manage().window().maximize();

        // Creating object to the Sukali screen class
        Screen s=new Screen();

        //Finding and clicking on the Addon image
         try {
            s.find("Path to the 'AddonIcon.png'");
            s.click("Path to the 'AddonIcon.png'");
        } catch (FindFailed e) {            
            e.printStackTrace();
        }

        //Wait until new Addon popup is opened.
         WebDriverWait wait = new WebDriverWait(driver, 5);      
         wait.until(ExpectedConditions.numberOfWindowsToBe(2));

         // Switch to the Addon Pop up
         String parentWindow= driver.getWindowHandle();
         Set<String> allWindows = driver.getWindowHandles();
         for(String curWindow : allWindows){             
             if(!parentWindow.equals(curWindow)){
             driver.switchTo().window(curWindow);
             }
         }

         /***********Ur code to work on Add-on popup************************/
    }
}

I hope this will help you.

Venkatesh Achanta
  • 624
  • 1
  • 14
  • 31
1
from selenium import webdriver
option = webdriver.ChromeOptions()
option.add_extension("./testCRX/angular_ext.crx")
driver = webdriver.Chrome(chrome_options=option)
driver.get('chrome://extensions/')

for open the added Extension: I think we can set shortkey in chrome://extensions/ -> Keyboard shortcuts , add the Keyboard shortcuts to your added extension, then send Keyboard to launch the extension.

Bao Jie
  • 11
  • 1
  • https://developer.chrome.com/extensions/commands add shortkey for Extension – Bao Jie Jun 05 '17 at 07:21
  • 3
    Did you manage to prove this works? I can see that this might get you to the point that the "popup" appears - but can Selenium actually then access the HTML page displayed within and automate it? – oatsoda Jan 20 '20 at 15:20
1

You can use this solution, for simulating the click on the extension icon

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.postMessage('clicked_browser_action', '*')");
Norayr Sargsyan
  • 1,737
  • 1
  • 12
  • 26
0

I know it's not exactly the same but couldn't you inject an iframe into the page with the source of your popup and then interact with it for testing?

<iframe src='chrome-extension://<extensionId>/popup.html'/>
Co-der
  • 363
  • 1
  • 14
-3

Try this: Running Selenium WebDriver using Python with extensions (.crx files)

use

options.add_extension('path_to_extension.crx')

Community
  • 1
  • 1
German Petrov
  • 1,475
  • 1
  • 17
  • 21
  • The problem is clicking on the extension icon, not loading the browser with the extension option. – QualiT Mar 07 '17 at 07:38
-3

You can pass the argument --load-extension to the Chrome webdriver through the options where the path points to your unpacked extension. So in your case you could use: options.add_argument("--load-extension=ABSOLUTE_PATH_TO_EXTENSION")

Also, here is a link to some code I've written that works using the --load-extension method. (It's Ruby not Python, but should give you some insights.)

berrberr
  • 1,914
  • 11
  • 16
  • I have been able to load the chrome extension using `chrome_options.add_argument("--load-extension=ABSOLUTE_PATH_TO_UNPACKED_EXTENSION");` but my problem is I am not able to click on the extension (i.e. to open the extension) from webdriver – curiousJ Aug 29 '14 at 20:10