org.testng.TestNGException:无法实例化类com.Testform

时间:2018-03-28 06:10:13

标签: java selenium-webdriver testng

我使用Selenium-Java编写了以下代码。然后我将项目转换为TestNG。但是当我运行该程序时,我得到了以下异常。

但是当我以java应用程序运行它时程序运行成功

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css" rel="stylesheet" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

    </head>
    <body>
        <div class="form-group">
            <label for="test" class="col-sm-3 control-label">Test</label>
            <div class="col-sm-3">
                <input type="text" class="form-control" id="test" placeholder="Enter A Value">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-6 col-sm-3">
                <button type="button" id="go" class="btn btn-primary">Go</button>
            </div>
        </div>

        <!--Modal if input is empty-->
        <div class="modal fade" id="#myModal">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>

                        </button>
                        <h4 class="modal-title">Modal title</h4>

                    </div>
                    <div class="modal-body">
                        <p>One fine body&hellip;</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>

        <!--modal if there is some text--->
        <!--Modal-->
        <div class="modal fade" id="#myModal1">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>

                        </button>
                        <h4 class="modal-title">HI</h4>

                    </div>
                    <div class="modal-body">
                        <p>One fine body&hellip;</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>
        <script type="text/javascript">
            var test1 = $('#test').val();

            $('#go').click(function () {
                if (test1 === "") {
                    $('#\\#myModal').modal('show');
                } else {
                    $('#\\#myModal1').modal('show');
                }

            });
        </script>
        <!-- /.modal -->
        <!--End Modal-->
    </body>
</html>

“这是我的testng.xml文件”

public class Testform {
    WebDriver driver = new FirefoxDriver();
@Test
  public void openBrowser() {
     // System.setProperty("webdriver.gecko.driver", "E://Rijo/DLS/geckodriver.exe");
        //DesiredCapabilities dc = DesiredCapabilities.firefox();
        //dc.setCapability("marionette", true);

  }
@Test
  public void registerForm() {
    // Get the WebElement corresponding to the first name(TextField)        
      WebElement firstname = driver.findElement(By.id("name_3_firstname")); 
        WebElement lastname = driver.findElement(By.id("name_3_lastname")); 
            firstname.isDisplayed();
            firstname.isEnabled();           
            firstname.sendKeys("Zumo");
            lastname.sendKeys("Car");

        WebElement radioBtn = driver.findElement(By.name("radio_4[]"));
            radioBtn.click();   

        WebElement check1 = driver.findElement(By.name("checkbox_5[]"));
            check1.click();

        Select dropdownctry = new Select(driver.findElement(By.name("dropdown_7")));
            dropdownctry.selectByVisibleText("India");
        Select dropdownmnth = new Select(driver.findElement(By.name("date_8[date][mm]")));
            dropdownmnth.selectByVisibleText("1");
        Select dropdownday = new Select(driver.findElement(By.name("date_8[date][dd]")));
            dropdownday.selectByVisibleText("10");
        Select dropdownyear = new Select(driver.findElement(By.name("date_8[date][yy]")));
            dropdownyear.selectByVisibleText("1997");

        WebElement phonenumber = driver.findElement(By.id("phone_9"));
            phonenumber.sendKeys("918877664659");   
        WebElement username = driver.findElement(By.id("username"));
            username.sendKeys("Testngtest123"); 
        WebElement email = driver.findElement(By.id("email_1"));    
            email.sendKeys("rroommff@rr.tt");
        WebElement textarea = driver.findElement(By.name("description"));   
            textarea.sendKeys("Selenium");

      // Get the WebElement corresponding to the Password Field     
      WebElement password = driver.findElement(By.name("password"));                            
            password.sendKeys("Test@test1");
      WebElement confirmpwd = driver.findElement(By.id("confirm_password_password_2"));                         
        confirmpwd.sendKeys("Test@test1");  
  }
@Test
  public void closeForm() {
      WebElement submit = driver.findElement(By.name("pie_submit"));
        submit.click();
  }
}

“我得到以下例外。”

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test thread-count="5" name="Test">
    <classes>
      <class name="com.Testform"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

“有人可以帮我解决这个问题吗?”

1 个答案:

答案 0 :(得分:0)

所以是的,真正的错误它在底部那里写着“引起:java.lang.IllegalStateException:必须设置驱动程序可执行文件的路径......”

如果你有注释掉的东西,你实际上至少需要第一行 - 之前 - 你实例化一个新的驱动程序。在您的构造函数中执行此操作或与您已有的相似但注释掉。 DC行为调用我不用于我的Firefox实例。

    WebDriver driver;

    Testform() {
        System.setProperty("webdriver.gecko.driver", "E://Rijo/DLS/geckodriver.exe");
        driver = new FirefoxDriver();
    }