获取"未找到类路径"使用TestNG在Selenium中执行测试时出错

时间:2017-05-25 06:28:17

标签: java selenium automation testng

以下是我的代码:

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运行我的Testsuite时。我收到Class path not found之类的例外情况。我已经完成了清理项目,重新安装了TestNG。但没有运气。

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

2 个答案:

答案 0 :(得分:0)

您需要添加一个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>

保存更改后。右键单击项目&gt;运行AS&gt; TestNG诉讼

希望这会有所帮助

答案 1 :(得分:0)

如果您尝试将testng.xml作为TestNG suite运行并且classpath标记

下的<class>被错误地描述,则会发生这种情况

说您在testng.xml中有以下类标记:

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

</classes>

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