How to Create a Compatible JAR File for Selenium Tests? - java

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.

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.

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.

Related

Eclipse : Used testNG 7 without built with maven ? ClassPath issue

I'm writting some testCases on Windows in java using eclipse IDE, version : 2019-06 (4.12.0), selenium WebDriver 4.0.0, testNG 7.0.0-beta7 and PageFactory POM.
I can't build my test case in eclipse or in cmd line, because eclipse doesn't recognized my import of testNg libraries. TestNG is well installed on my project.
I'm supposed that is a class path issue. I would like to know if it's because i'm not using Maven (I don't have to used it for the moment), or if there is some issues with the latest version of testNG lib ?
My java test using testNG.xml worked perfeclty in eclipse with my testNg version: 6.14. I tried to execute it with my cmd line, I had a NullPointerError error. There was a bug with this exception in the previous versions of testNG: https://github.com/cbeust/testng/pull/1811 and they fixed it on the latest version (7.0.0).
I installed the new version of testNG, and test everything I found on the web, but nothing worked. I can't build my test anymore.
CreateSingleOrder_BondsPage.java
import org.openqa.selenium.Keys;
public class CreateSingleOrder_BondsPage {
WebDriver driver;
public CreateSingleOrder_BondsPage(WebDriver driver) {
// TODO Auto-generated constructor stub
this.driver = driver;
}
#FindBy(id="M1_3") WebElement tabOrderManagement;
public void hoverTabOrderManagement() {
Actions action = new Actions(driver);
action.moveToElement(tabOrderManagement).perform(); //move
to the element
}
}
CreateSingleOrder_BondsTest.java
import org.testng.annotations.Test;
public class CreateSingleOrder_BondsTest extends BasicTest { /*BasicTest
contains initialization of driver in #BeforeMethod and #AfterMethod also
*/
#Test
public void buyBonds() throws Exception {
CreateSingleOrder_BondsPage createBonds =
PageFactory.initElements(driver, CreateSingleOrder_BondsPage.class);
Thread.sleep(1000); //loading data
createBonds.hoverTabOrderManagement();
}
}
I have these errors :
for my import in my java class.
The import org.testng cannot be resolved
when I runned my TestNG.xml as "TestNG Suite"
An internal error occurred during: "Launching Test2 TAP_TestNG.xml".
java.lang.NullPointerException
in the tab "Problems" of eclipse
Invalid classpath container: 'TestNG' in project 'Test2 TAP' - Test2
TAP -Build path - Build Path Problem
If anyone can help me with the idea of using maven for the class path, if it's could be a solution or other solutions I can use to run my test again would be very helpful.
Thanks for your help.
This could be due to some trivial issues. A couple of checks:
Have you installed the TestNG plugin for eclipse? If not go to marketplace and do that
Link for TestNG- Eclipse
Make sure the build path for your project has TestNG libraries. If not go to build path and click on Add Library. This will allow you to add TestNG libraries.
This has nothing to do with Maven and should work independently as well.
You can also refer TestNG-Eclipse doc
I believe it is happening due to multiple versions of testng in your .m2 folder. There are 2 solutions I can think of
Delete all the items in TestNG folder and recompile the project.
Delete TestNG versions which you don't require and then recompile the project.

Java main within Tomcat project in Eclipse

I have a running and working Tomcat Project and I want to add a normal Java project into it for better and faster testing. No pages to get fast database outputs and such.
In the WEB-INF/src folder I have added a package with contains my main class and includes some of the other controllers from the project.
EDIT: Moved sources to the /src (as suggested) folder but still the same error.
Right now it is a simple println in a main class. The problem is that eclipse tells me it cannot find the main clas or load it. (right click on the main class file -> runs as -> Java Application)
package mytomcatproj.maintest
import mytomcatproj.othercontroller
public class MyMainClass {
public static void main(String[] args) {
String overval="te";
String overres= othercontroller.getWord(overval);
System.out.println("Test" + overres);
}
}
How can I get the project to run?
Thank you for you help.

Small TestNG app won't run correctly

I created a Java Project in Eclipse and ran some TestNG files successfully. Now I've created a new Java Project and find out my TestNG files won't run as expected.
So, to troubleshoot, I created a very simple test2.class file shown below for my first (previous) Java Project and the same file for my new java Project (top line changed to reflect package name). When I execute this file as a TestNG app, it runs fine in my first project (prints out 'Test' in the console window). However, when I run it in the same way in my new project, nothing prints out and I don't see any errors.
I checked my Java Build Paths and they are the same for both. So, obviously I am missing something in my new java project???
package com.selftechy.seltests;
import org.testng.annotations.Test;
public class Test2 {
#Test
public void Test() {
System.out.println("Test");
}
}
Just a shot in the sky...
If this is your actual code listed, it has a compilation error because of the missing '}' in the method. So the TestNG execution does not even start.

How to run java file exported from selenium IDE through command prompt in windows

Can some one tell me how can i run the java file which was exported from selenium IDE through command prompt.
I have used the following command: "java -jar selenium-server.jar -htmlSuite "*firefox" "http://www.google.com" "C:\mytestsuite\mytestsuite.html" "C:\mytestsuite\results.html"
Able to launch selenium functional test runner but nothing is executed there.
The converted tests are JUnit tests. So you should have two processes:
Your selenium server process:
java -jar lib/selenium-server-standalone-2.28.0.jar
Your JUnit test runner
"java -cp /usr/share/java/junit.jar org.junit.runner.JUnitCore [test class name] "
If you have several test classes, it might be better to create a TestSuite with the Suite annotation:
#RunWith(Suite.class)
#SuiteClasses({
MyTestClass1.class,
MyTestClass2.class})
public class TestSuite {
...
If you are using Spring, you can setup config containing selenium server address, browser, ...
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "classpath:my/package/seleniumConfig.xml"})
public abstract class SeleniumTestSuite {
The java test cases exported don't compile because they require at least the Selenium library to compile and may need junit or TestNG as well to actually run. I really suggest that you do this from within eclipse.
You can get eclipse 32 or 64 bit here. Then create a new Java project by going File-> New -> Java Project. You can get the Selenium client zip here. You need to get the client jar (called selenium-java-2.31.0.jar) out of this zip and put it in the lib directory of your new Java project in eclipse. You may have to create the lib directory and then right-click the jar file in the lib directory and "add to build path".
Put the java code that was generated by the Selenium IDE into the src directory of the new Java project in eclipse. (you may need to create the appropriate packages under src etc). Then right-click the test case java file that you want and select "Run As ... Junit". This should run it for you. If you get compile or run errors you can update your question above.
This is the solution:
Test is your Class mande by selenium
import org.junit.runner.JUnitCore;
import com.example.tests;
public static void main(String[] args) {
Result result = JUnitCore.runClasses(Test.class);
for (Failure failure : result.getFailures()) {
System.out.println(failure.toString());
}
}

Selenium->Automation planning->How to Execute Majority of the TestCases at a time?

iam learning selenium inorder to reproduce it on my application.so,please help me by answering my questions.1)how to execute bulk of testcases at a time by using automation tool (selenium 2)?)how to start my application to test with automation tool selenium rc?
To use selenium API, you need to download the needed .jar files from here
Once you add the needed .jar files to your projects classpath, you are ready to start doing testing.
Here a very simple hello world application example that can help you understand selenium tests. (As you see there is no call to main or anithing similar, the tests will run automatically when the application is launched)
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import junit.framework.TestCase;
public class HelloSeleniumTest extends TestCase {
private Selenium browser;
public void setUp() {
browser = new DefaultSelenium("localhost",
4444, "*firefox", "http://www.google.com");
browser.start();
}
public void testGoogle() {
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");
browser.click("btnG");
browser.waitForPageToLoad("5000");
assertEquals("hello world - Google Search", browser.getTitle());
}
public void tearDown() {
browser.stop();
}
}
Before you run the app, you should start the RC server from the console. It is very simple, just:
1- Go to the Selenium-Server folder using the console (The place where are the files that you downloaded)
2- execute java -jar selenium-server.jar
Once is running, go back to your programming IDE and run the application
Also you have the possibility of downloading the Selenium plugin for firefox, that will create for you the java code when you navigate the pages so your testing will go faster.
This is how it looks like:
If something still unclear, visit this link, it is very well explained.
Download demo with sample test and try with it

Categories