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.