【问题标题】:Getting "Class path not found" Error while executing test in Selenium using TestNG使用 TestNG 在 Selenium 中执行测试时出现“找不到类路径”错误
【发布时间】:2017-05-25 06:28:17
【问题描述】:

下面是我的代码:

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;

public class NewTest {
    public WebDriver driver;
    String driverPath ="F:\\AutomationTesting\\geckodriver-v0.10.0-win64\\geckodriver.exe";
  @Test
  public void main() {

      driver.findElement(By.id("account")).click();

      // Find the element that's ID attribute is 'log' (Username)

      // Enter Username on the element found by above desc.

      driver.findElement(By.id("log")).sendKeys("testuser_1");

      // Find the element that's ID attribute is 'pwd' (Password)

      // Enter Password on the element found by the above desc.

      driver.findElement(By.id("pwd")).sendKeys("Test@123");

      // Now submit the form. WebDriver will find the form for us from the element

      driver.findElement(By.id("login")).click();

      // Print a Log In message to the screen

      System.out.println(" Login Successfully, now it is the time to Log Off buddy.");

      // Find the element that's ID attribute is 'account_logout' (Log Out)

      driver.findElement(By.id("account_logout"));

  }
  @BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.gecko.driver",driverPath);
      driver = new FirefoxDriver();
      //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception

      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      //Launch the Online Store Website

      driver.get("http://www.onlinestore.toolsqa.wpengine.com");

  }

  @AfterMethod
  public void afterMethod() {
      driver.quit();
  }

}

当我尝试使用 TestNG 运行我的测试套件时。我得到了一个像Class path not found 这样的异常。我已经完成了清理项目,重新安装了 TestNG。但没有运气。

我正在使用Selenium webdriver 3.0Gecko driverTestNG。 我该如何解决这个问题?

【问题讨论】:

  • 在类路径中找不到类:错误
  • 请打印错误的整个堆栈跟踪
  • 你是如何执行测试的?
  • 请添加你得到的错误日志
  • 在类路径中找不到类:NewTest 在 org.testng.xml.XmlClass.loadClass(XmlClass.java:81) 在 org.testng.xml.XmlClass.init(XmlClass.java:73) 在org.testng.xml.XmlClass.(XmlClass.java:59)

标签: java selenium automation testng


【解决方案1】:

你需要添加一个 testing.xml 文件来执行你所有的测试

只需在项目根目录中添加一个testing.xml 文件,然后将下面的内容粘贴到其中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="FirstTestSuit">
      <test name="FunctionlTest">
         <classes>      
                      <class name="package.subpackage.TestClassName1"/>                             
                      <class name="package.subpackage.TestClassName2"/> 
         </classes> 
  </test>
</suite>

保存更改后。右键单击项目 > 运行 AS > TestNG Suit

希望这会有所帮助

【讨论】:

    【解决方案2】:

    当您尝试以 TestNG suite 运行 testng.xml 并且您的 classpath&lt;class&gt; 标记下被错误地描述时,就会发生这种情况

    假设您的testng.xml 中有以下类标签:

    <classes>
      <class name="packageName.yourClassName"/>
    
    </classes>
    

    您需要指定正确的路径,其中包含您包含的类的包名称。

    【讨论】:

      猜你喜欢
      • 2011-11-27
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 2021-09-15
      相关资源
      最近更新 更多