Skip to content

Commit

Permalink
Fixed WebExtensions installation
Browse files Browse the repository at this point in the history
Fixed installation of new firefox webextension built with new WebExtension API i.e. the one which contains manifest.json instead of install.rdf

Fixes SeleniumHQ#4093

Signed-off-by: Alexei Barantsev <barancev@gmail.com>
  • Loading branch information
kumar-nitish authored and barancev committed Sep 10, 2017
1 parent 865c140 commit f9f0f6b
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package org.openqa.selenium.firefox.internal;

import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.io.FileHandler;
import org.openqa.selenium.io.TemporaryFilesystem;
Expand All @@ -30,6 +32,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;
import java.io.*;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;
Expand Down Expand Up @@ -58,7 +61,7 @@ public void writeTo(File extensionsDir) throws IOException {

File root = obtainRootDirectory(toInstall);

String id = readIdFromInstallRdf(root);
String id = getExtensionId(root);

File extensionDirectory = new File(extensionsDir, id);

Expand Down Expand Up @@ -87,6 +90,46 @@ private File obtainRootDirectory(File extensionToInstall) throws IOException {
return root;
}

private String getExtensionId(File root) {
File manifestJson = new File(root, "manifest.json");
File installRdf = new File(root, "install.rdf");

if (installRdf.exists())
return readIdFromInstallRdf(root);
else if (manifestJson.exists())
return readIdFromManifestJson(root);
else
throw new WebDriverException(
"Extension should contain either install.rdf or manifest.json metadata file");

}

private String readIdFromManifestJson(File root) {
final String MANIFEST_JSON_FILE = "manifest.json";
File manifestJsonFile = new File(root, MANIFEST_JSON_FILE);
try {
String addOnId = null;
JsonObject manifestObject = new JsonParser().parse(new FileReader(manifestJsonFile)).getAsJsonObject();
if (manifestObject.has("applications")) {
JsonObject applicationObj = manifestObject.getAsJsonObject("applications");
if (applicationObj.has("gecko")) {
JsonObject geckoObj = applicationObj.getAsJsonObject("gecko");
if (geckoObj.has("id")) {
addOnId = geckoObj.get("id").getAsString().trim();
}
}
}

if (addOnId == null || addOnId.isEmpty()) {
addOnId = manifestObject.get("name").getAsString().replaceAll(" ", "") +
"@" + manifestObject.get("version").getAsString();
}

return addOnId;
} catch (FileNotFoundException e1) {
throw new WebDriverException("Unable to file manifest.json in xpi file");
}
}

private String readIdFromInstallRdf(File root) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class FirefoxProfileTest {
private static final String FIREBUG_PATH = "third_party/firebug/firebug-1.5.0-fx.xpi";
private static final String FIREBUG_RESOURCE_PATH =
"/org/openqa/selenium/testing/drivers/firebug-1.5.0-fx.xpi";
private static final String MOOLTIPASS_PATH = "third_party/firebug/mooltipass-1.1.87.xpi";

private FirefoxProfile profile;

Expand Down Expand Up @@ -159,6 +160,14 @@ public void shouldInstallExtensionFromZip() throws IOException {
assertTrue(extensionDir.exists());
}

@Test
public void shouldInstallWebExtensionFromZip() throws IOException {
profile.addExtension(InProject.locate(MOOLTIPASS_PATH).toFile());
File profileDir = profile.layoutOnDisk();
File extensionDir = new File(profileDir, "extensions/MooltipassExtension@1.1.87");
assertTrue(extensionDir.exists());
}

@Test
public void shouldInstallExtensionFromDirectory() throws IOException {
File extension = InProject.locate(FIREBUG_PATH).toFile();
Expand All @@ -169,6 +178,16 @@ public void shouldInstallExtensionFromDirectory() throws IOException {
assertTrue(extensionDir.exists());
}

@Test
public void shouldInstallWebExtensionFromDirectory() throws IOException {
File extension = InProject.locate(MOOLTIPASS_PATH).toFile();
File unzippedExtension = Zip.unzipToTempDir(new FileInputStream(extension), "unzip", "stream");
profile.addExtension(unzippedExtension);
File profileDir = profile.layoutOnDisk();
File extensionDir = new File(profileDir, "extensions/MooltipassExtension@1.1.87");
assertTrue(extensionDir.exists());
}

@Test
public void shouldInstallExtensionUsingClasspath() throws IOException {
profile.addExtension(Firebug.class, FIREBUG_RESOURCE_PATH);
Expand Down
Binary file added third_party/firebug/mooltipass-1.1.87.xpi
Binary file not shown.

0 comments on commit f9f0f6b

Please sign in to comment.