1

I wants to run Selenium grid hub, With my project framework Maven + TestNG.

I have added selenium-server 3.12 dependency in POM.XML and When I call this selenium-server jar from our maven project, its gives me error as 'No main manifest attribute'.

I am having Doubt, to call "java -jar selenium-server-standalone-3.12.0.jar -role hub" Do we need to externally download this Jar for Selenium Hub ? As its already imported in Maven project.

Error with Maven project setup,'No main manifest attribute'

C:\Users\Desktop-pc>java -jar org\seleniumhq\selenium\selenium-server\3.12.0\selenium-server-3.12.0.jar -role hub
no main manifest attribute, in org\seleniumhq\selenium\selenium-server\3.12.0\selenium-server-3.12.0.jar

Where if I use separate 'Selenium Server Standalone' jar, hub is created successfully:

C:\>java -jar selenium-server-standalone-3.8.1.jar -role hub
16:05:07.614 INFO - Selenium build info: version: '3.8.1', revision: '6e95a6684b'
16:05:07.616 INFO - Launching Selenium Grid hub
2018-05-28 16:05:08.690:INFO::main: Logging initialized @1490ms to org.seleniumhq.jetty9.util.log.StdErrLog
16:05:08.707 INFO - Will listen on 4444
2018-05-28 16:05:08.823:INFO:osjs.Server:main: jetty-9.4.7.v20170914
2018-05-28 16:05:08.867:INFO:osjs.session:main: DefaultSessionIdManager workerName=node0
2018-05-28 16:05:08.868:INFO:osjs.session:main: No SessionScavenger set, using defaults
2018-05-28 16:05:08.871:INFO:osjs.session:main: Scavenging every 660000ms
2018-05-28 16:05:08.876:INFO:osjsh.ContextHandler:main: Started o.s.j.s.ServletContextHandler@1672fe87{/,null,AVAILABLE}
2018-05-28 16:05:11.946:INFO:osjs.AbstractConnector:main: Started ServerConnector@1816a71f{HTTP/1.1,[http/1.1]}{0.0.0.0:4444}
2018-05-28 16:05:11.948:INFO:osjs.Server:main: Started @4748ms
16:05:11.948 INFO - Nodes should register to http://localhost:4444/grid/register/
16:05:11.949 INFO - Selenium Grid hub is up and running

POM.XML

       <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass> ???? </mainClass>
                        </manifest>
                    </archive>
                </configuration>
       </plugin>

what I need to declare in mainclass, If I am using @TestNG and its without static main class method.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Ishita Shah
  • 3,955
  • 2
  • 27
  • 51

3 Answers3

2

Selenium Grid is usually used for remote executions so that your local machine doesn't have to have browsers installed (true in the case of build machines which are usually headless linux boxes) or so that you can use your local machine for something else (true in the case of your local laptop/desktop when you are running tests in development mode).

Binding the Selenium Grid within your project (such that every time you build and run tests in your project a selenium grid is spun off) is not such a good idea.

If you are still looking for starting a Selenium Grid locally (starting a Selenium Grid remotely is not only hackish but also requires a lot of workarounds and is susceptible to a lot of failures) first and then having your UI tests point at it and then executed, you can do it in the following ways:

  • By spinning off a Selenium Grid via a maven plugin.

For doing this you would need to leverage the maven-antrun-plugin. Below is a typical configuration of how the antrun-plugin would look like (It spins off a hub with 2 nodes wired to it ) Source for this info was this thread

<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.7</version>
        <executions>
          <execution>
            <phase>process-test-classes</phase>
            <configuration>
              <target>
                <java classname="org.openqa.grid.selenium.GridLauncherV3"
                  classpathref="maven.test.classpath"
                  failonerror="true"
                  fork="false">
                  <arg line="-role hub"/>
                </java>
                <java classname="org.openqa.grid.selenium.GridLauncherV3"
                  classpathref="maven.test.classpath"
                  failonerror="true"
                  fork="false">
                  <arg line="-role node"/>
                </java>
                <java classname="org.openqa.grid.selenium.GridLauncherV3"
                  classpathref="maven.test.classpath"
                  failonerror="true"
                  fork="false">
                  <arg line="-role node -port 6666"/>
                </java>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
</build>

The above plugin configuration assumes that selenium-server is added as a test dependency i.e., something like below

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-server</artifactId>
  <version>3.12.0</version>
  <scope>test</scope>
</dependency>

If you have a compile time dependency on selenium-server then please change

classpathref="maven.test.classpath"

To

classpathref="maven.compile.classpath"

For more details on classpathref please refer to the official documentation.

Caveat:

Since now the Selenium Grid basically registers beans for JMX, you may get an error such as the one below

08:28:09.529 INFO [GridLauncherV3$3.launch] - Launching a Selenium Grid node on port 6666
java.security.AccessControlException: access denied ("javax.management.MBeanTrustPermission" "register")
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)

In case you get such an error, please make sure that you open up the file named java.policy found under %JAVA_HOME%\jre\lib\security\ and within the grant { section add permission javax.management.MBeanTrustPermission "register"; (Source for this info was this thread)

Now you can very well run your TestNG tests by running mvn clean test (And when you do that, you will notice a selenium grid being spun off locally)

  • By spinning off a Selenium Grid via a TestNG listener/configuration method.

Here you would basically be spinning off a selenium hub and node either via a TestNG listener or via a configuration method and then have your tests just point at the locally spun off grid and run against it.

The below sample shows a test that runs against a locally spun off grid (via a configuration method)

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.grid.selenium.GridLauncherV3;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

public class AnotherSample {
  private RemoteWebDriver driver;

  @BeforeSuite
  public void beforeSuite() throws InterruptedException {
    new Thread(
            () -> {
              String[] args = {"-role", "hub"};
              GridLauncherV3.main(args);
            })
        .start();
    new Thread(
            () -> {
              String[] args = {"-role", "node"};
              GridLauncherV3.main(args);
            })
        .start();
    // Lets wait for 10 seconds for the Hub and the node to be up and running
    TimeUnit.SECONDS.sleep(10);
  }

  @BeforeClass
  public void setup() throws MalformedURLException {
    driver =
        new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), new ChromeOptions());
  }

  @Test
  public void testMethod() {
    driver.get("http://www.google.com");
    System.err.println("Title = " + driver.getTitle());
  }

  @AfterClass
  public void cleanup() {
    if (driver != null) {
      driver.quit();
    }
  }
}
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • it gives error like : Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-antrun-plugin:1.8:run (execution: default, phase: process-test-classes) – Ishita Shah May 29 '18 at 05:03
  • @IshitaShah - That is usually a problem with eclipse. Please try out one of the recommendations as provided in [this](https://stackoverflow.com/q/6352208/679824) Stackoverflow question. That should get you past the problem. On a side note, please try running from the command line using Maven or switch to an IDE such as IntelliJ – Krishnan Mahadevan May 29 '18 at 06:28
1

You are using maven-jar-plugin which is used to create jar artifact for a normal java project.

In order to run testNG annotated java method classes , you require maven-surefire-plugin. Remove the maven-jar-plugin and replace it with following plugin block.

             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <skip>false</skip>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
  • I have defined that also, org.apache.maven.plugins maven-surefire-plugin 2.21.0 But, that also not working and throwing same error. – Ishita Shah May 28 '18 at 11:10
  • So your error is coming after executing : `java -jar org\seleniumhq\selenium\selenium-server\3.12.0\selenium-server-3.12.0.jar` , right? – Manmohan_singh May 28 '18 at 11:13
  • @IshitaShah - Please take a look at my answer. I believe that is what you are looking for. – Krishnan Mahadevan May 29 '18 at 03:37
  • same error, Let me know if you are using maven + testng + selenium grid so can look into – Ishita Shah May 29 '18 at 05:30
  • @IshitaShah - What do you mean by same error ?Can you please elaborate. And yes, I am making use of Selenium along with TestNG and maven. – Krishnan Mahadevan May 29 '18 at 14:33
  • @KrishnanMahadevan Same error make sense with "No main manifest attribute" – Ishita Shah May 30 '18 at 04:42
  • @IshitaShah - We are going around in circles. If you really want the selenium grid to be spun off before your testng code runs, then you should just adopt the solution that I shared. I am still not able to understand what jar are you referring to. Can you please create a sample maven project, include within it whatever I have shared and try running it? When using a maven plugin such as `antrun-plugin` you dont need to be depending on an external jar, because they required classes are already available in our classpath via maven dependencies and we are just calling the relevant `main()` method – Krishnan Mahadevan May 31 '18 at 03:17
-1

Without your detailed usecase I am not sure about your exact requirement. But,

  • To start the Selenium Server you need to issue the command as:

    java -jar selenium-server-standalone-3.12.0.jar
    
  • To start the Selenium Grid Hub you need to issue the command as:

    java -jar selenium-server-standalone-3.12.0.jar -role hub
    
  • If you are using DefaultSelenium (or the RemoteWebDriver implementation)you still need to start a Selenium Server and there are two possible ways to achieve it as follows:

    • The best way is to download the selenium-server-standalone.jar from the Selenium Downloads page and just use it.

    • Alternatively, you can also embed the Selenium Server into your own project by adding the following dependency to your pom.xml:

      <dependency>
          <groupId>org.seleniumhq.selenium</groupId>
          <artifactId>selenium-server</artifactId>
          <version>3.12.0</version>
      </dependency>  
      

    Now you can create a Selenium Server instance yourself and start it.

Note: Be aware, that the selenium-server artifact has a dependency to the servlet-api-3.x.y artifact, which you should exclude, if your project will be run inside a web application container.

Here you will find the Maven Information on Selenium.


Update

If you are trying to run it from the CLI you need to provide the command:

java -jar selenium-server-standalone-3.12.0.jar

The -jar option works only if the JAR file is an executable JAR file which essentially means it must have a manifest file with a Main-Class attribute in it.

You can find more details in Packaging Programs in JAR Files to create an executable JAR.

If the JAR isn't following these rules it is not an executable jar.

If it's not an executable JAR then you need to run the program as follows:

java -cp selenium-server-standalone-3.12.0.jar com.somepackage.SomeClass

where com.somepackage.SomeClass is the Class that contains the main method to run the program.

You can find a similar discussion in Can't execute jar- file: “no main manifest attribute”.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • I wants to run selenium grid hub, With my project framework Maven + TestNG. I have added selenium-server 3.12 dependency in POM.XML and When I call this selenium-server jar from our maven project, its gives me above error. Hope this make clear idea. I am having Doubt, to call java -jar selenium-server-standalone-3.12.0.jar -role hub Do we need to externally download this Jar for Selenium Hub ? As its already imported in Maven project, So I am calling it directly from Maven project. – Ishita Shah May 28 '18 at 11:45
  • @IshitaShah Edit and update your main question with this information and checkout my updated answer and let me know the status – undetected Selenium May 28 '18 at 12:03
  • Same Error, I don't know What I am doing incorrectly. But I am calling Selenium Standalone server jar from maven project, As maven project is importing automatically so we don't need to download and thus calling it. – Ishita Shah May 28 '18 at 12:47
  • @IshitaShah As you are _calling Selenium Standalone server jar from maven project_ did you add the dependency to the **servlet-api-3.x.y** artifact? – undetected Selenium May 28 '18 at 12:59
  • Yes, Added and Still same error : http://prntscr.com/jnlnkz http://prntscr.com/jnlo2z – Ishita Shah May 28 '18 at 13:12
  • @IshitaShah Checkout my answer update and let me know status. Update the question with all the relevant information for a quick analysis – undetected Selenium May 28 '18 at 13:15
  • Thank you so much @debanjanB for your Guide. But, there is no main method in my any class because of TestNG. Still its needed for single time calling I will create dummy class with main method. I will implement it and will update here. If there is any document for same, Please share. Unfortunately I couldn't find any doc for Selenium Grid + Maven + TestNG – Ishita Shah May 28 '18 at 13:39
  • @IshitaShah Can you specifically let me know where you are exactly stuck? I am sure I have provided all the relevant details/links of documentations/discussions you may require to solve this question. – undetected Selenium May 28 '18 at 13:44
  • I think i am stuck on jar calling, The jar which I am calling in not having correct manifest attribute in the file. And also the cause of main method. I need some one help who is having exactly same set up (Maven + TestNG + Selenium Grid) – Ishita Shah May 29 '18 at 05:33