2017-03-07 83 views
1

Selenium - TestNG代碼不適用於我。第一個TestNG代碼不起作用

package firsttestngpackage; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.*; 

定義TestNG的類

public class FirstTestNGFile { 
public String baseURL = "http://newtours.demoaut.com/"; 
public WebDriver driver = new FirefoxDriver();  
@Test 

定義方法

public void verifyHomePageTitle() { 
    driver.get(baseURL); 
    String expectedTitle = "Welcome: Mercury Tours"; 
    String actualTitle = driver.getTitle(); 
    Assert.assertEquals(actualTitle, expectedTitle); 
    driver.quit();    
} 
} 

**我而用TestNG面臨以下錯誤**

org.testng.TestNGException: 
Cannot instantiate class firsttestngpackage.FirstTestNGFile 
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:31) 
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:410) 
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:323) 
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:126) 
+0

可以添加完整的堆棧跟蹤? –

+0

你可以分享整個FirstTestNGFile,或者至少是它的構造函數嗎? – juherr

+0

嗨juherr,如果你在線,我可以與你分享我的屏幕? –

回答

1

你應該嘗試實例司機@BeforeSuite爲 -

public class FirstTestNGFile { 
    public String baseURL = "http://newtours.demoaut.com/"; 
    public WebDriver driver;  

    @BeforeSuite 
    public void SetBrowser(){ 
     driver = new FirefoxDriver(); 
    } 

    @Test 
    public void verifyHomePageTitle() { 
     driver.get(baseURL); 
     ... 
     driver.quit();    
    } 
} 
+0

非常感謝!!!! –

+0

System.setProperty(「webdriver.chrome.driver」,「C:\\ Users \\ user2 \\ Downloads \\ chromedriver_win32(2)// chromedriver.exe」); –

+0

我在@BeforeSuite中添加了這一行 –