Java – execute TestNG. Java via cli Error in XML: unable to find or load main class org testng. TestNG
Before I entered it, I was a novice in selenium, and so on I looked up all the answers to the question and still couldn't solve it My%claspath% and so on are correct The jar file is located in the correct folder I'm completely lost why it doesn't work My guess is that I'm doing something stupid and the experts will soon find out the mistake
I can run the following test in eclipse. It has no problem opening Firefox and running the test If I run tests from CMD or Jenkins, I get the following error: error: unable to find or load the main class org testng. TestNG
I replaced the following actual information with virtual data:
Selenium
package package1; import static org.junit.Assert.fail; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.Alert; import org.openqa.selenium.By; import org.openqa.selenium.NoAlertPresentException; import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class login { private WebDriver driver; private String baseUrl; private boolean acceptNextAlert = true; private StringBuffer verificationErrors = new StringBuffer(); @Before public void setUp() throws Exception { System.setProperty("webdriver.gecko.driver","C:\\gecko\\geckodriver.exe"); driver = new FirefoxDriver(); baseUrl = "http://example.com"; driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); } @Test public void testLogin() throws Exception { driver.get(baseUrl + "index.PHP"); driver.findElement(By.id("email")).clear(); driver.findElement(By.id("email")).sendKeys("myemail@example.ie"); driver.findElement(By.id("password")).clear(); driver.findElement(By.id("password")).sendKeys("mypassword"); driver.findElement(By.xpath("//input[@value='Login »']")).click(); } @After public void tearDown() throws Exception { driver.quit(); String verificationErrorString = verificationErrors.toString(); if (!"".equals(verificationErrorString)) { fail(verificationErrorString); } } private boolean isElementPresent(By by) { try { driver.findElement(by); return true; } catch (NoSuchElementException e) { return false; } } private boolean isAlertPresent() { try { driver.switchTo().alert(); return true; } catch (NoAlertPresentException e) { return false; } } private String closeAlertAndGetItsText() { try { Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else { alert.dismiss(); } return alertText; } finally { acceptNextAlert = true; } } }
Project structure
textng. xml
<suite name="SampleSuite"> <test name="LearningJenkins"> <classes> <class name="package1.login"></class> </classes> </test> </suite>
Run bat
cd C:\Users\keating99\workspace\FdimTests java -cp C:\Users\keating99\workspace\FdimTests\lib*;C:\Users\keating99\workspace\FdimTests\bin org.testng.TestNG testng.xml
I'm sure I've set up the selenium test perfectly I checked the classpath and project path, and everything seemed ok
Any help would be a great help Mention all the other answers again, without any luck Thank anyone who helps us first:)
UPDATE
C:\Users\keating99\workspace\FdimTests>java org.testng.TestNG testng.xml Exception in thread "main" java.lang.NoClassDefFoundError: bsh/EvalError at org.testng.TestRunner.<init>(TestRunner.java:99) at org.testng.SuiteRunner$DefaultTestRunnerFactory.newTestRunner(SuiteRunner.java:508) at org.testng.SuiteRunner.init(SuiteRunner.java:142) at org.testng.SuiteRunner.<init>(SuiteRunner.java:106) at org.testng.TestNG.createSuiteRunner(TestNG.java:1116) at org.testng.TestNG.createSuiteRunners(TestNG.java:1103) at org.testng.TestNG.runSuitesLocally(TestNG.java:955) at org.testng.TestNG.run(TestNG.java:900) at org.testng.TestNG.privateMain(TestNG.java:1182) at org.testng.TestNG.main(TestNG.java:1146) Caused by: java.lang.ClassNotFoundException: bsh.EvalError at java.net.urlclassloader.findClass(UnkNown Source) at java.lang.ClassLoader.loadClass(UnkNown Source) at sun.misc.Launcher$AppClassLoader.loadClass(UnkNown Source) at java.lang.ClassLoader.loadClass(UnkNown Source) ... 10 more
Solution
Execute TestNG The simplest way to XML is as follows:
>Get the project location from the IDE, that is, eclipse, go to the project location through windows explorer and create a directory library. > Dump all libraries (selenium jar, TestNG) to the Lib directory. > From project directory, provide the following classpath through cli:
C:\Users\keating99\workspace\FdimTests>set classpath=C:\Users\keating99\workspace\FdimTests\bin;C:\Users\keating99\workspace\FdimTests\lib\*;
>Now, execute TestNG as follows xml:
C:\Users\keating99\workspace\FdimTests>java org.testng.TestNG testng.xml
>Observe that the testcase is executed