Export Selenium TestNG script from Eclipse to runnable jar - eclipse

I have a script written in Selenium using the TestNG framework. This script executes via an xml file. Since we do not have main method in this, how can I export this script to a runable JAR file?

I marked this question as a dupe (and this code snippet comes from the dupe question), but basically you could write a public static void main method that scans for tests or loads a existing testng.xml file. Then, you just need to use something such as Maven Shade plugin to package it.
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] { test_start.class });
testng.addListener(tla);
testng.run();
}
I used WebDriverManager project to manage my driver libraries. I would have the .jar load those externally.

Related

Could not find main method from given launch configuration

I've a simple Java project that works when I execute it at Eclipse environment. But when I try to export it to a Runnable Jar, I get the following error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: JavaSwing/src.main.java/com/cansoft/GUIProgram.java
Exported with compile warnings: JavaSwing/src.main.java/com/util/Util.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
I read other posts which suggest to create a MANIFEST.MF file specifying the main-class which I did. It is placed at MyProjectFolder/META-INF/MANIFEST.MF and it contains the following information:
Manifest-Version: 1.0
Class-Path: resources
main-class: com.cansoft.GUIProgram
My main class is as follows:
public class GUIProgram {
private JFrame folderCreationSubappFrame;
private Color color;
private String home;
private final static Logger LOG_MONITOR = Logger.getLogger("com.cansoft");
public static void main(String[] args) {
try {
new GUIProgram();
} catch (Exception e) {
LOG_MONITOR.log(Level.INFO,e.getMessage());
}
}
public GUIProgram() throws InterruptedException, SecurityException, IOException {
home = System.getProperty("user.home") + File.separator + "Documents";
startLogSystem();
if(isFirstRun()) {
showWelcomeFrame();
} else {
initialize();
}
} .... More and more code
Does anybody know what am I missing? Any help much appreciated.
Thank you.
It is not enough to create the manifest file, you need to explicitly choose it in the Eclipse jar export dialog.
Answer to Comment
If you use "runnable jar", make sure that you chose the correct launch configuration and that the launch configuration successfully runs when chosing "Run As" -> "Run Configurations" -> "Java Application" -> Your Configuration -> "Run"
I finally find out where the problem was, it was quite simple btw. I had created my GUIProgram within a src.main.java package, but that package was created (my bad) as resources instead of folders, so Eclipse was smart enought to run it but when trying to generate the JAR which expected a correct java project structure, it was failing because truly there were not GUIProgram java class at src path (src was not folder type but resources).
Hope I succeed explaining.

How to package XML with runnable Jar?

I have a Java project with many testNG classes. In order to call the suite of test classes you need to run an XML file. I wrote a class in the same package as the XML which runs said XML. I'm exporting the project as a runnable jar that uses the main as the launch configuration.
public class mainTest {
public static void main(String[] args) {
TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
List<String> suites = Lists.newArrayList();
File file = new File("src\\xml\\whole.xml");
suites.add(file.getAbsolutePath());
testng.setTestSuites(suites);
testng.run();
}
}
My export steps:
Right click and export on the entire project
Select Runnable JAR File
Select "mainTest" as my launch configuration Export to Desktop
When I run this class directly in Eclipse, the XML is called fine. However, when I export the entire project as a runnable jar, I get this error.
It seems the jar is looking for the XML in the same directory as the jar (where it doesn't exist), but shouldn't the XML already be included inside the jar?

How to update JUnit5 jupiter.api_5.3.1 to jupiter.api_5.4.2?

i want to know that if "MethodOrderer" class is available in JUnit5 Library FOR ECLIPSE or not, because i am unable to find it.
If not, how can i shift jupiter.api_5.3.1 to jupiter.api_5.4.2 in eclipse JUnit5 library?
Will be thankful to see your reply.
I downloaded JUnit5 jar file from "https://search.maven.org/artifact/name.remal.tools.test/junit5/1.26.97/jar" and this jar does have "MethodOrderer" class but when i add this to project dependency and run the testclass, eclipse shows up this error "No tests found with test runner 'JUnit5'."
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
#TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class JunitCalculatorV4 {
#BeforeAll
static void setUpBeforeClass() throws Exception {
System.out.println("Before All");
}
#AfterAll
static void tearDownAfterClass() throws Exception {
System.out.println("After All");
}
#Test
#Order(1)
void addTest() {
System.out.println("Add test");
}
#Test
#Order(2)
void divideTest() {
System.out.println("Divide Test");
}
}
Actually this annotation #TestMethodOrder(MethodOrderer.OrderAnnotation.class) is from jupiter.api_5.4.2 which i added as an external jar, and that might be causing conflict with the existing JUnit5 library.
My problem would be solved if the JUnit5 library is updated as a whole, or atleast the jarfile inside the library is updated.
Project > Properties: Java Build Path, tab Libraries:
You are using Eclipse 2018-12 (4.10) with JUnit 5.3.1 instead of Eclipse 2019-03 (4.11) with JUnit 5.4.0 (the screenshot shows JAR file names containing _5.3.1.v20181005- instead of _5.4.0.v20190212-).
Please upgrade.

Selenium-java-3.0.0-beta2 jar files are not working in eclipse

I'm new to selenium, I've tried to automate a simple task, but the build path option is not working in eclipse. I've attached few files here. Please help me to run this code.
Here is the code for that:
package seleniumTutorial;
public class SeleniumGoogleMain {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
driver.findElement(By.linkText("Gmail")).click();
driver.findElement(By.id("Email")).sendKeys("jssjohny");
driver.findElement(By.name("signIn")).click();
}
}
Here is the download list for selenium :
These are the jar files I've included, but it's not working still :
Thanks in advance.

Not able to create executable jar for webdriver+TestNg project which does not contain main method in any class

I am learning webdriver and I have created one project with TestNg.
I have several classes in my package under src folder.
No class contains public static void main(....). i.e[Entry Point]
My question is :
Can we create Runnable / Executable jar file through eclipse for projects like this[project without main method]. I searched on many sites but unfortunately didnt get solution.
Please suggest me some links OR The way by which we can do this.
To create a jar file of the TestNG without main method you have to create another class which contain main method.
Suppose you have a TestNG file name as Sample.java, in the same package create one more class as ExecutableRar and write the below code :
public class ExecutableRar {
public static void main(String[] args) {
TestNG testng = new TestNG();
Class[] classes = new Class[]{Sample.class};
testng.setTestClasses(classes);
testng.run();
}
Now you can run this class as a Java Application. Then right click on the package --> Export --> Java --> Runnable jar File --> select ExecutableRar in launch configuration --> Browse the file location and enter the name of the file in Export Destination --> Finish.
Please let me know if you are having any issues.