2015-09-27 22 views
-1

什麼是錯誤的設置環境的代碼無法運行該代碼成立之後TestNG的不運行

親切指導它

package tc1; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 

import org.openqa.selenium.WebDriver; 

import org.openqa.selenium.chrome.ChromeDriver; 

import org.openqa.selenium.firefox.FirefoxDriver; 

import org.testng.annotations.AfterClass; 

import org.testng.annotations.BeforeClass; 

import org.testng.annotations.Test; 

public class TC001 { 

    WebDriver driver; 

    @BeforeClass 

    public void launchBrowser(){  

     FirefoxDriver driver = new FirefoxDriver(); 

     driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS); 
    } 

    @Test 

    public void login(){ 

     driver.get("http://www.meritnation.com/testprep"); 

     driver.findElement(By.className("login-link")).click(); 
     //Thread.sleep(50000); 

    } 

    @AfterClass 

    public void browserClose(){ 

     driver.quit(); 
    } 
} 

有顯示零遊程和零故障運行TestNG的輸入代碼here

[TestNG的]運行: C:\用戶\用戶\應用程序數據\本地\ TEMP \ TestNG的-蝕 - 1256807320 \ TestNG的-customsuite.xml

===============================================默認的測試

測試運行:0,失敗:0,跳過:0

============================ ===================默認套房

總測試運行:0,失敗:0,跳過:0

[TestNG的]時間採取[FailedReporter通過= 0失敗= 0跳過= 0]:3 ms

[TestNG]時間採取 [email protected]:1毫秒

[TestNG的]採取[email protected]時間:

[TestNG的]時間採取的56毫秒

[email protected]:26 毫秒

[TestNG的]時間採取 [email protected]:9毫秒

[TestNG的]採取 org.testng時間.reporters.SuiteHTMLReporter @ 6bf2d08e:66 ms

+0

你是如何引發的考驗嗎? –

回答

0

簡短的回答是,我認爲你需要改變@Before@BeforeClass

而且我不知道什麼進口使用(我只是一味地沒有按Ctrl + Shift +在eclipse O),或者是什麼網站你要,但我想我會在給你一個工人階級給一個鏡頭,所以我希望這有助於:

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.AfterClass; 
import org.testng.annotations.BeforeClass; 
import org.testng.annotations.Test; 

public class TC001 { 

     // WebDriver driver; 
     public static WebDriver driver = new FirefoxDriver(); 

      @BeforeClass 
      public void launchBrowser() { 



       driver.navigate().to("https://www.google.com"); 
      // FirefoxDriver driver = new FirefoxDriver(); 

      // driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS); 
      } 

      @Test 
      public void login() { 

       driver.findElement(By.className("login-link")).click(); 

       //Thread.sleep(50000); 
      } 
      @AfterClass 
      public void browserClose() { 

       driver.quit(); 
      } 


} 

更新: 當我跑到你的代碼是失敗,失敗1。由於您見狀,0運行,我會問

  1. 你能嘗試在Eclipse「清潔工程」(或任何你的IDE 是)。

  2. 嘗試關閉重新啓動IDE

    • 確保您已安裝TestNG的在你的IDE,如果你能真正 得到它的運行,那麼我們進入到下一個問題。

    你正在與司機做一些不尋常的事情。你聲明2個不同的東西作爲你的「驅動程序」。
    - 嘗試做public static WebDriver driver = new FirefoxDriver();像我有我的回答,

    • @beforeclass方法取出FirefoxDriver線,

    • @beforeclass.

還可以使用driver.navigate
+0

還有一件事,我刪除了超時,因爲我不想等那麼久,因爲它失敗。隨意取消註釋。 – jason12459

+0

我仍然面臨同樣的問題。測試運行:0,失敗:0,跳過:0 – shruti

+0

與JUnit不同,TestNG被設計爲在'@ Before/AfterClass'方法中使用非'static'屬性。 – juherr

0

驅動程序已經宣佈兩次。 @BeforeClass方法正在創建一個本地的FirefoxDriver實例,它不能在@Test方法中訪問。在創建FirefoxDriver()的實例的情況下,在@BeforeClass中刪除'FirefoxDriver'。換句話說,用@BeforeClass方法替換FirefoxDriver driver = new FirefoxDriver();driver = new FirefoxDriver();

所以,它看起來有點像這樣:

public class TC001 { 

WebDriver driver; 

@BeforeClass 

public void launchBrowser(){  

    driver = new FirefoxDriver(); 

    driver.manage().timeouts().implicitlyWait(50000, TimeUnit.SECONDS); 
} 

//剩下的代碼,因爲它是