Automate a button click on chrome://extensions pag

2019-01-25 17:04发布

I'm trying to write an automated test that will automate the process of updating a google chrome extension. I'm not aware of another method of doing this automatically so here is what I'm currently trying to do:

  1. Open the chrome extensions page (as far as I'm aware this is just an html page unless I'm missing something).

  2. Click on the "Update extensions" button

Here is what I have tried having opened the chrome extensions page:

IwebElement UpdateButton = driver.findelement(By.Id("update-extensions-now"));
UpdateButton.Click();

For some reason the button click is not registering. I have tried some other locators such as CSS path and Xpath but they don't work either. Also, when I debug this test, it passes fine so I know it's not an issue with any of my locators. I have (as a test) tried to automate clicks on the other elements on this page and it's the same issue. I can't get a handle on any elements on the chrome://extensions page at all.

Has anyone encountered this or have any ideas as to what's going on?

2条回答
贪生不怕死
2楼-- · 2019-01-25 17:29

If you look at the HTML of the "chrome://extensions" page you will notice that the "Update extensions now" button is contained within an iframe. You need to switch to the iframe before trying to register a button click. i.e:

(This is in c#. Note that this code is written from memory so it may not be 100% accurate. Also, you will want to write more robust method. This code just quickly demonstrates that by switching to the iframe, it will work ok)

String ChromeExtensionsPage = "chrome://extensions";
driver.Navigate().GoToUrl(ChromeExtensionsPage);
driver.Switchto().Frame("DesiredFrame");
IwebElement UpdateButton = driver.findelement(By.Id("DesiredButtonID"));
UpdateButton.Click();
查看更多
迷人小祖宗
3楼-- · 2019-01-25 17:38

You can use the Chrome extensions API to auto-update required extension. Find the file "manifest.json" in the default Google Chrome

C:\Users\*UserName*\AppData\Local\Google\Chrome\User Data\Default\Extensions

There find the update URL of your extension:

{
  "name": "My extension",
  ...
  "update_url": "http://myhost.com/mytestextension/updates.xml",
  ...
}

The returned XML by the Google server looks like:

<?xml version='1.0' encoding='UTF-8'?>
<gupdate xmlns='http://www.google.com/update2/response' protocol='2.0'>
  <app appid='yourAppID'>
    <updatecheck codebase='http://myhost.com/mytestextension/mte_v2.crx' version='2.0' />
  </app>
</gupdate>

appid The extension or app ID, generated based on a hash of the public key, as described in Packaging. You can find the ID of an extension or Chrome App by going to the Extensions page (chrome://extensions).

codebase A URL to the .crx file.

version Used by the client to determine whether it should download the .crx file specified by codebase. It should match the value of "version" in the .crx file's manifest.json file.

The update manifest XML file may contain information about multiple extensions by including multiple elements.

Another option is to use the --extensions-update-frequency command-line flag to set a more frequent interval in seconds. For example, to make checks run every 45 seconds, run Google Chrome like this:

chrome.exe --extensions-update-frequency=45

Note that this affects checks for all installed extensions and apps, so consider the bandwidth and server load implications of this. You may want to temporarily uninstall all but the one you are testing with, and should not run with this option turned on during normal browser usage.

The request to update each individual extension would be:

http://test.com/extension_updates.php?x=id%3DyourAppID%26v%3D1.1

You can find even more detailed information on exntesions developers site: https://developer.chrome.com/extensions

查看更多
登录 后发表回答