@Test方法不等待selenium代码来完成它的执行

时间:2015-07-13 05:27:28

标签: java selenium testing automation testng

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中找到了元素并放在了beforemethod之前。当我传递有效的用户名和密码时,@ After方法不等待Web驱动程序登录,它的显示测试执行完成。

2 个答案:

答案 0 :(得分: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);
    }

}

答案 1 :(得分:1)

点击登录按钮后,网页需要花时间更改标题。因此,您感觉@afterMethod没有等待完成登录操作。

如果凭据无效,点击登录按钮后,网页标题不会改变,您会觉得@after Method没有等待登录发生。

点击登录按钮后等待2秒钟,您的脚本将按预期工作:

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