2015-07-13 100 views
1
package demoActitime; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.AfterMethod; 
import org.testng.annotations.BeforeMethod; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

public class LoginActitime { 


    private String UN; 
    private String Pass; 
    private WebElement username; 
    private WebElement password; 
    private WebDriver driver = new FirefoxDriver(); 


    private String Url = "http://demo.actitime.com/"; 
    private String Urlvalid = "http://demo.actitime.com/user/submit_tt.do"; 
    private String expected = null; 
    private String actual = null; 

    private String xpathUsername = null; 
    private String xpathPassword = null; 
    private String xpathLogin = null; 


    @BeforeMethod 
    public void findElements() 
    { 



     driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
     driver.get(Url); 

     xpathUsername = "//input[@id='username']"; 
     xpathPassword = "//input[@type='password']"; 
     xpathLogin = "//a[@id='loginButton']/div"; 
    } 

    @AfterMethod 
    public void doTask() 
    { 

     System.out.println(expected); 
     driver.findElement(By.xpath(xpathUsername)).clear(); 
     driver.findElement(By.xpath(xpathPassword)).clear(); 
     driver.findElement(By.xpath(xpathUsername)).sendKeys(UN); 
     driver.findElement(By.xpath(xpathPassword)).sendKeys(Pass); 
     driver.findElement(By.xpath(xpathLogin)).click(); 

     actual = driver.getTitle(); 

     Assert.assertEquals(actual, expected); 
    // driver.quit(); 

    } 

    @Test(priority = 0) 
    public void LoginValidUNInvalidPass() 
    { 
     this.UN="admin"; 
     this.Pass="basheer"; 

     System.out.println("LoginValidUNInvalidPass"); 
     expected = "actiTIME - Login"; 
    } 
    @Test() 
    public void LoginValidUNValidPass() 
    { 
     this.UN="admin"; 
     this.Pass="manager"; 
     System.out.println("LoginValidUNValidPass"); 
     expected = "actiTIME - Enter Time-Track"; 

    } 

     @Test 
     public void LoginInValidUNInvalidPass() 
     { 
      this.UN="basheer"; 
      this.Pass="basheer"; 
      System.out.println("LoginInValidUNInvalidPass"); 
      expected = "actiTIME - Login"; 
     } 

     @Test 
     public void LoginInValidUNValidPass() 
     { 
      this.UN="basheer"; 
      this.Pass="manager"; 
      System.out.println("LoginInValidUNValidPass"); 
      expected = "actiTIME - Login"; 
     } 





} 

這是我的更新代碼。我已經刪除了初始化,找到aftermethod中的元素並放入before方法。當我傳遞有效的用戶名和密碼時,@After方法不等待Web驅動程序登錄,它的顯示測試執行完成。@測試方法不會等待硒碼完成其執行

+0

你裏面findElements代碼()方法,你必須implicitwait和其他的東西在裏面@AfterMethod,這不向右看。你會希望之前擁有它們。 –

+0

@ChandanNayak,我刪除了初始化驅動程序的代碼,隱式地等待並從After Method中找到該元素,並將它放入BeforeMethod中,但仍未達到我的目的。 –

+0

請把@ BeforeTest的顯示代碼/套房/法 – san1deep2set3hi

回答

1

嘗試下面的示例程序,它可能會幫助你。我已經重組了你的代碼,並使用@BeforeTest & @AfterTest。讓我知道它是否適合你。

package demoActitime; 

import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.AfterTest; 
import org.testng.annotations.BeforeTest; 
import org.testng.annotations.Test; 

public class LoginActitime { 
    //defining all required variables 
    private String UN = ""; 
    private String Pass = ""; 
    private WebElement username = null; 
    private WebElement password = null; 
    private WebElement login = null; 
    private WebDriver driver = null; 

    private String Url = "http://demo.actitime.com/"; 
    private String Urlvalid = "http://demo.actitime.com/user/submit_tt.do"; 
    private String expected = null; 
    private String actual = null; 

    private String xpathUsername = null; 
    private String xpathPassword = null; 
    private String xpathLogin = null; 

    @BeforeTest 
    public void findElements() 
    { 
     //initialising webdriver with url 
     driver = new FirefoxDriver(); 
     driver.manage().window().maximize(); 
     driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS); 
     driver.get(Url); 
     //initialising webelements 
     xpathUsername = "//input[@id='username']"; 
     xpathPassword = "//input[@type='password']"; 
     xpathLogin = "//a[@id='loginButton']/div"; 

     username = driver.findElement(By.xpath(xpathUsername)); 
     password = driver.findElement(By.xpath(xpathPassword)); 
     login = driver.findElement(By.xpath(xpathLogin)); 
    } 

    @AfterTest 
    public void doTask() 
    { 
     System.out.println(expected); 
     username.clear(); 
     password.clear(); 
    } 

    @Test(priority = 0) 
    public void invalidLogin() 
    { 
     this.UN="validUser"; 
     this.Pass="invalidPassword"; 

     username.sendKeys(UN); 
     password.sendKeys(Pass); 
     login.click(); 

     expected = "expected title"; 

     actual = driver.getTitle(); 
     Assert.assertEquals(actual, expected); 
    } 

    @Test(priority = 1) 
    public void validLogin() 
    { 
     this.UN="validUser"; 
     this.Pass="validPassword"; 

     username.sendKeys(UN); 
     password.sendKeys(Pass); 
     login.click(); 

     expected = "expected title"; 

     actual = driver.getTitle(); 
     Assert.assertEquals(actual, expected); 
    } 

} 
+0

它不是validLogin,其中在斷言開始比較預期的標題標題,硒執行完畢之前,甚至工作。 –

+0

它是否在爲'invalidLogin'工作? –

+0

實際上,憑藉有效憑據,硒無法登錄帳戶。在普通瀏覽器中輸入相同的憑據時工作正常。 –

1

點擊登錄按鈕後,網頁更改其標題的時間。因此,您感覺@afterMethod並未等待完成登錄操作。

無效憑證的情況下,點擊登錄按鈕後,網頁標題不發生變化,你得到的感覺,@After方法不是等待登錄的情況發生。

把一些2秒等待點擊登錄按鈕後,你的腳本將如預期:

driver.findElement(By.xpath(xpathLogin)).click(); 
Thread.sleep(2000); 
+0

謝謝@Vishal Jagtap,是解決我的問題:) –

+0

嗨,你能接受這個答案是否能解決你的問題:) –

+0

@NarasingaRao與其等待您也可以等待,你知道會出現在着陸元素頁面(在登錄後出現)並等待該元素出現。你不應該使用明確的等待。你永遠不知道需要多少時間才能到達目標頁面,這取決於不同的因素。 – Shamik