WebDriver: Add preconfigured extension to ChromeDriver with Java

This blog post contains a possible method how to add a preconfigured chrome extension to the ChromeDriver for WebDriver with Java. The technical approach is to set the LocalStorage for the plugin, which contains the main information. The plugin that will be configured here is the ModHeader plugin. It enables the possibility to modify the header of the current page.

Therefor you need to download the *.crx file. If you are using Chrome you can use the plugin ‚Give me CRX‘ (https://chrome.google.com/webstore/detail/give-me-crx/acpimoebmfjpfnbhjgdgiacjfebmmmci).

Example: Mod Header 1.2.4

modheader

public void testAddChromeExtension() {

	ChromeOptions chromeOptions = new ChromeOptions();
	chromeOptions.addExtension(new File("/path/to/mod_header_1.2.4.crx"));

	String extensionID = "idgpnmonknjnojddfkpgkljpfnnfcklj";

	Map<String, String> profileMap = new HashMap<>();
	profileMap.put("NAME_1","VALUE_1");
	profileMap.put("NAME_2","VALUE_2");

	String profilesString = createModHeaderProfiles(profileMap);
   
	createLocalStorageColumnForChromeExtension(extensionID, "profiles", profilesString, true);
	createLocalStorageColumnForChromeExtension(extensionID, "selectedProfile", "0", false);

	DesiredCapabilities capabilities = DesiredCapabilities.chrome();
	capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);

	WebDriver driver = new ChromeDriver(capabilities);

	...
	...

}
private String createModHeaderProfiles(Map _keyValueMap) {
	String headerSets = "";
	for (Map.Entry entry : _keyValueMap.entrySet()) {
		headerSets = headerSets + ",{" +
				"\"name\":\"" + entry.getKey() + "\"," +
				"\"value\":\"" + entry.getValue() + "\"," +
				"\"comment\":\"\"," +
				"\"enabled\":true}";
	}
	String filledHeaders = "{\"headers\":[" + headerSets.replaceFirst(",", "") + "],";
	String emptyHeaders = "{\"headers\":[{\"name\":\"\",\"value\":\"\",\"comment\":\"\",\"enabled\":true}],";
	String filters = "\"filters\":[{\"host\":\"\",\"behavior\":\"true\",\"comment\":\"\"}]}";

	return "[" + filledHeaders + filters + "," + emptyHeaders + filters + "," + emptyHeaders + filters + "," + emptyHeaders + filters + "]";
}
private void createLocalStorageColumnForChromeExtension(String _extensionID, String _key, String _value, boolean _dropTable) {

	String dbString = "chrome-extension_" + _extensionID + "_0.localstorage";
	String pathString = currentSessionChromeProfile.getPath() + "/Default/Local Storage/";
	File path = new File(pathString);
	if (!path.exists()) {
		path.mkdir();
	}

	Connection connection = null;
	try {
		Class.forName("org.sqlite.JDBC");
		connection = DriverManager.getConnection("jdbc:sqlite:" + pathString + dbString);
		Statement statement = connection.createStatement();

		if (_dropTable) {
			statement.executeUpdate("DROP TABLE IF EXISTS ItemTable");
			statement.executeUpdate("CREATE TABLE ItemTable (key TEXT UNIQUE ON CONFLICT FAIL, value BLOB NOT NULL ON CONFLICT FAIL)");
		}
		PreparedStatement ps = connection.prepareStatement("INSERT INTO ItemTable VALUES('" + _key + "',?)");
		ps.setBytes(1, _value.getBytes("UTF-16le"));
		ps.executeUpdate();
	} catch (ClassNotFoundException | UnsupportedEncodingException | SQLException e) {
		e.printStackTrace();
	} finally {
		try {
			if (connection != null) {
				connection.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

Afterwards ‚LocalStorage‘ is set:

devtools_modheader

Extension popup with configured LocalStorage

Advertisements
Dieser Beitrag wurde unter WebDriver abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s