Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

java - Error executing testng.xml through CLI : Could not find or load main class org.testng.TestNG

Before I get into it, I'm a newbie with selenium and so on. I have looked up all the answers to this issue and still haven't been able to fix the problem. My %claspath% and so on are all correct. The jar files are in the correct folders and such. I'm totally lost to why this isn't working. My guess is that I'm doing something silly and an expert would spot the error quickly.

I am able to run the following test within Eclipse, it opens firefox and runs the test with no issues. If I run the test from cmd or Jenkins I get the following error: Error: Could not find or load main class org.testng.TestNG


I have replaced actual information below with dummy data:

Selenium class

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 structre This is my project structure:

textng.xml

<suite name="SampleSuite">

    <test name="LearningJenkins">

        <classes>

            <class name="package1.login"></class>

        </classes>

    </test>

</suite>

run.bat

cd C:Userskeating99workspaceFdimTests

java -cp C:Userskeating99workspaceFdimTestslib*;C:Userskeating99workspaceFdimTestsin org.testng.TestNG testng.xml

I am certain I have set up the selenium test perfectly. I have checked the class path and project path and all seems to be OK.

Any help at all would be a great help. Again have have looked up all the other answers with no luck whatsoever. Thanks in advance for any help people :)

UPDATE


C:Userskeating99workspaceFdimTests>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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simplest way to execute your testng.xml is as follows :

  • Get your Project Location from your IDE i.e. Eclipse, through Windows Explorer go to the Project Location and create a directory lib .
  • Dump all the libraries (Selenium jars, TestNG) in the lib directory.
  • From Project Directory, through CLI provide the following classpath:

    C:Userskeating99workspaceFdimTests>set classpath=C:Userskeating99workspaceFdimTestsin;C:Userskeating99workspaceFdimTestslib*;
    
  • Now, execute testng.xml as follows :

    C:Userskeating99workspaceFdimTests>java org.testng.TestNG testng.xml
    
  • Observe the Testcase gets executed.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...