2016-02-19 66 views
0

// Login.java文件,我已經執行了這個文件,其中我給了TestG註解但結果顯示沒有方法執行。而執行該代碼,並導致測試NG產生沒有這樣的方法執行使用測試NG註釋

import java.util.concurrent.TimeUnit; 

import org.testng.*; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 
import org.openqa.selenium.By; 
import org.openqa.selenium.firefox.FirefoxDriver; 

public class Login { 

public static FirefoxDriver _webBrowser; 
public String _url; 

@Test(priority = 1, enabled = true) 
public Login(FirefoxDriver driver, String url) { 
    _webBrowser = driver; 
    _url = url; 
} 



@Test(priority = 2) 
private boolean openBrowser() { 
    boolean isSuccess = false; 
    try { 
     _webBrowser.manage().window().maximize(); 
     _webBrowser.get(_url); 
    _webBrowser.manage().timeouts().implicitlyWait(120,TimeUnit.SECONDS); 
     isSuccess = true; 
    } catch (Exception ex) { 

    } 
    return isSuccess; 
} 

    @Test(priority = 3) 
public boolean doLogin(String username, String password) { 
    boolean isSuccess = false; 
    try { 
     if (openBrowser()) { 

    Login module = new Login(_webBrowser, "http://180.211.114.147:97/Account/Login"); 
      module.doLogin("devrana","dev123"); 

      _webBrowser.findElement(By.id("UserName")).sendKeys(username); 
      _webBrowser.findElement(By.id("Password")).sendKeys(password); 
      _webBrowser.manage().timeouts().implicitlyWait(120,  TimeUnit.SECONDS); 
      _webBrowser.findElement(By.id("btnLogin")).submit(); 

      String tmp = _webBrowser.getCurrentUrl(); 
      if (tmp.equals("http://180.211.114.147:97/#/app/dashboard")) 
       System.out.println("Login success!!"); 
      else 
       System.out.println("Fail to login.."); 
     } 
    } catch (Exception ex) { 

    } 
    return isSuccess; 
} 

} 

控制檯輸出顯示測試1/1方法:0(194ms)

Default suite 
Total tests run: 0, Failures: 0, Skips: 0 
=============================================== 

[TestNG] Time taken by [email protected]: 1 ms 
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 7 ms 
[TestNG] Time taken by [email protected]: 35 ms 
[TestNG] Time taken by [email protected]: 4 ms 
[TestNG] Time taken by org.test[email protected]: 126 ms 
[TestNG] Time taken by [email protected]: 20 ms 

回答

0

我不知道你爲什麼使用@Test方法回報。在testng中,我們使用Assertions進行交叉檢查,而不是返回通過或失敗的布爾值。

public class Example { 


public static WebDriver driver; 

@Test(priority=1) 
public void GoStack(){ 
    driver=new FirefoxDriver(); 
    driver.get("http://stackoverflow.com/"); 
    Assert.assertEquals(driver.getTitle(), "Stack Overflow"); //if it is pass then GoStack test will pass 
} 


@Test(priority=2) 
public void LoginTitle(){ 
    driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); 
    driver.findElement(By.className("login-link")).click(); 
    Assert.assertEquals(driver.getTitle(), "Log In - Stack Overflow"); 
} 

@Test(priority=3) 
public void Dashborad(){ 
    driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS); 
    driver.findElement(By.id("email")).sendKeys("email id here"); 
    driver.findElement(By.id("password")).sendKeys("password here"); 
    driver.findElement(By.id("submit-button")).click(); 
    Assert.assertEquals(driver.getTitle(), "title after login here"); 
} 

} 

請參考testng文檔瞭解更多詳情。您可以使用@BeforeTest執行狀開瀏覽器的一些行動意味着某種前提條件,也是我們可以利用@AfterTest爲後置條件很像在關閉瀏覽器等。

謝謝你, 穆拉利

相關問題