0

I have a profile folder src/main/resources/firefox/profile/selenium and an extension folder under src/main/resources/firefox/extensions/ I am using the maven shade plugin to package this as an uber jar so we can run the test suite on our server.

        <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                            <transformer
                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <manifestEntries>
                                    <Main-Class>com.erac.automation.automationCommon.test.gui.TestGui</Main-Class>
                                    <X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
                                    <X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
                                </manifestEntries>
                            </transformer>
                        </transformers>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>

and how I access the profile and extension folder

try{
        firefoxProfileDirectory = getExtension("/firefox/profiles/selenium/", "");
        System.out.println("the file for the firefoxProfileDirectory - " + firefoxProfileDirectory.exists());
    }catch(Exception e){

    }
    FirefoxProfile profile = new FirefoxProfile(firefoxProfileDirectory);
try{
    profile.addExtension(getExtension("/firefox/extensions/", FIREBUG_XPI));
    profile.addExtension(getExtension("/firefox/extensions/", FIREPATH_XPI));
    }catch(Exception e){

    }

 public static File getExtension(String path, String file) throws IOException {
    URL url = AutomationProfileFactory.class.getResource(path + file);
    File firefoxProfileFolder = new File("");
    if(url == null) {
        //nothing
    }
    else{
        firefoxProfileFolder = new File(url.getPath());
        System.out.println(firefoxProfileFolder.exists());
        return firefoxProfileFolder;
    }
    return new File(""); 
}

When running the program in MyEclipse the tests run as normal, but after creating the uber jar and running in command line I get this error

Caused by: org.openqa.selenium.firefox.UnableToCreateProfileException: Given model profile directory does not exist: file:\C:\rmqa\rmqa\target\rmqa-0.0.1-SNAPSHOT.jar!\firefox\profiles\selenium
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'XD-DW764-676', ip: '10.23.14.55', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_72'
Driver info: driver.version: EventFiringFirefoxDriver
        at org.openqa.selenium.firefox.FirefoxProfile.verifyModel(FirefoxProfile.java:180)
        at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:91)
        at org.openqa.selenium.firefox.FirefoxProfile.<init>(FirefoxProfile.java:78)
        at com.erac.automation.automationCommon.driver.AutomationProfileFactory.getFirefoxProfile(AutomationProfileFactory.java:27)
        at com.erac.automation.automationCommon.driver.EventFiringFirefoxDriver.<init>(EventFiringFirefoxDriver.java:11)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
        at java.lang.reflect.Constructor.newInstance(Unknown Source)
        at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:148)

How should I access these .xpi files and get the profile folder?

Faulkry
  • 7
  • 4
  • Possible duplicate of http://stackoverflow.com/questions/21679990/how-to-add-resources-to-jar-using-maven-shade-plugin – user2272115 May 24 '16 at 18:46
  • I don't think this question is a duplicate of the other one because the other question talks about how to add resources, whereas this question seems to be around how to make selenium work with a profile thats embedded in a jar. – Krishnan Mahadevan May 26 '16 at 04:07

1 Answers1

0

The reason why it works in your IDE is because you are perhaps still resolving to the actual path of the profile in your file system, whereas when you package it as a uber jar and then try running it, your file path url is now comprised of the location of the jar within which the profile sits, coupled with the relative path of the profile within the jar. That I don't think selenium understands well.

So to get past this, you should be extracting your profile into one directory [ it can be a one time activity since webdriver basically makes a copy of the profile when it spawns a new browser ] and then construct your FirefoxProfile object by referring to the directory to where you extracted.

In a nutshell

firefoxProfileFolder

should point to a valid path on the local file system.

Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66