How to Create a Compatible JAR File for Selenium T

2020-07-19 04:03发布

问题:

I have created a script to perform few automated tests in Selenium for a website using Java in Eclipse.

My aim here is to create a JAR file for my automated tests so that when the file is simply executed, the tests will run on any other system configured with a Selenium environment. For that I have created a runnable JAR file from Eclipse by clicking on the Export option from the File menu. The name of the JAR file is Test MyWebsite.jar.

The source code of my Java classes is given below:

Main.java

package testproject.main;

import testproject.testmywebsite.*;


public class Main {

    public static void main(String[] args) {
        TestMyWebsite tmw = new TestMyWebsite();
        tmw.testMyWebsite();
        tmw.stopTest();
    }
}

TestMyWebsite.java

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import testproject.testmywebsite.tools.*;
import testproject.testmywebsite.login.*;

public class TestMyWebsite {

    private WebDriver driver;

    public TestMyWebsite() {
        setUp();
    }

    private void setUp() {
        // Create a new instance of the Chrome driver
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    public void testMyWebsite() {
        testLogin();
    }

    public void testLogin() {
        TestLogin tl = new TestLogin(driver);
        tl.testLogin();
    }

    public void stopTest() {
        //Close the browser
        driver.quit();
    }
}

TestLogin.java

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class TestLogin {

    private WebDriver;

    public TestLogin(WebDriver driver) {
        this.driver = driver;
    }

    public void testLogin() {
        //Perform the login test
    }
}

The problem here is that despite setting up and configuring the environment on other computers with the Selenium webdriver, copying the Test MyWebsite.jar in the third-party computer and running the file by double-clicking it, the tests do not run successfully. What happens is the Chrome browser opens momentarily and then closes without opening the 'MyWebsite's' URL. In other words, only the two lines of code in the Main.java class are executed. The JAR file is unable to find the other two associated classes in the Eclipse project. However when I execute the file in my computer the tests are perfectly run, which means that there is a problem with how I have created the JAR file. Please note that these are just three classes of the 12 classes I have created in 5 different packages in my Eclipse project to make understanding easier.

Can anyone please tell me where am I going wrong in creating my JAR file which will run both on my computer as well as on another computer configured with the Selenium environment? As a beginner in Selenium I had followed these instructions while creating my Selenium project in Eclipse. Replies at the earliest will be highly appreciated.

Thank you.

回答1:

Okay I found out the solution to the problem myself.

The problem lied in the way I was creating the Runnable JAR file in Eclipse. I was selecting the Extract required libraries into generated JAR option (selected by Eclipse by default) under the Library handling label on the first page. By doing so it was not packing the required external Selenium, TestNG and JRE System Library, JAR file libraries in my runnable JAR, hence it was not able to find the required classes. My JAR started working perfectly after selecting the second option, Package required libraries into generated JAR.



回答2:

For one thing, you should use a test runner, such as JUnit or TestNG, to run your tests. Another thing is to make sure you are using Maven and conforming to its 'standard directory layout'. By using Maven, you will be able to run the 'install' or 'package' goal/task to create the standalone 'executable jar'. Keep in mind that you will need to configure Maven in a unusual way , where you tell it to include all files under 'src/main/java' and 'src/test/java' into the same single .jar file. Also, you will need to configure Maven in such a way as to create a .jar that is actually executable.