7

我已經創建了一個Chrome擴展,使API調用數據庫,並提取一些數據與當前打開的網站相關。例如,如果我打開target.com並點擊擴展名,它會爲您提供與target.com相關的數據。通過硒webdriver打開鉻擴展

我想通過硒web驅動程序爲它編寫自動化測試,我可以定期運行迴歸測試。爲了測試擴展名,我需要先打開擴展名(通常我們通過點擊擴展名圖標來完成)。

我嘗試了不同的方式嘗試點擊擴展名圖標,但沒有成功。 (例如,使用鍵盤快捷鍵ALT - LEFT_ARROW - SPACE,但不能通過webdriver工作)。

我自己也嘗試這種(自提here):

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

但上面的代碼不開放的擴展幫助。

我將不勝感激關於如何在Selenium Webdriver中使用python做到這一點。

回答

-3

您可以將參數--load-extension通過選項所在的路徑點傳遞到Chrome的webdriver到您的未打包的擴展。所以在你的情況下,你可以使用:options.add_argument("--load-extension=ABSOLUTE_PATH_TO_EXTENSION")

另外,here is鏈接到我寫的一些代碼,使用--load-extension方法。 (這是Ruby不是Python,但應該給你一些見解。)

+0

我已經能夠使用'chrome_options.add_argument(「 - load-extension = ABSOLUTE_PATH_TO_UNPACKED_EXTENSION」)加載鉻擴展;'但我的問題是我無法從webdriver點擊擴展名(即打開擴展名) – curiousJ 2014-08-29 20:10:27

2

有同樣的問題。通過使用鏈接解決它:chrome-extension://<the extension identity>/html/login.html - 而不是圖標。 這樣,我可以測試擴展的所有功能。

+0

雖然這是正確的,但與在彈出窗口中打開的行爲有點不同。 – Xan 2015-12-11 10:17:38

+0

是否可以嘗試subprocess.call和applescripts? – 2015-12-11 20:23:22

+0

@KathyChow你能提供更多詳細信息嗎?我會加載一個彈出窗口,點擊Chrome擴展按鈕 – Volatil3 2016-01-23 08:49:05

3

我們有類似的要求,使用Selenium WebDriver處理chrome附加組件。正如'@Aleksandar Popovic'所說,我們無法使用WebDriver單擊Chrome擴展圖標,因爲圖標不在網頁中。

我們利用sikuli(利用圖像識別的自動化工具)來點擊Chrome加載項。之後,附加彈出窗口將成爲另一個瀏覽器窗口,因此請使用切換窗口對附加彈出窗口執行操作。

下面是使用兩個硒的webdriverSikuli爪哇的代碼示例。

Sikuli將基於圖像識別運行。在運行代碼之前,chrome瀏覽器的屏幕截圖並對其進行裁剪,以便只有Addon在圖像中可用。將該圖像保存爲「AddonIcon.png」。

Sikuli將在屏幕上匹配該圖像(在我們的例子中爲AddonIcon.png)並模擬點擊動作。

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************************/ 
    } 
} 

我希望這會幫助你。

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/') 

開放增加的擴展:我認爲我們可以在Chrome設置shortkey://擴展/ - >鍵盤快捷鍵,鍵盤快捷鍵添加到您添加的擴展,然後發送Keyboard啓動擴展。

+0

https://developer.chrome.com/extensions/commands爲擴展添加快捷鍵 – 2017-06-05 07:21:35

0

我知道它不完全一樣,但你不能注入一個iframe到你的彈出窗口的源代碼頁面,然後與它進行交互以進行測試嗎?

<iframe src='chrome-extension://<extensionId>/popup.html'/>