繁体   English   中英

@AftertTest完成后如何运行测试

[英]How to run test only after the @AftertTest is finished

我正在与TestNG一起学习Java中的Selenium Webdriver。 我正在使用Google登录页面进行测试。

我在按顺序运行测试用例时遇到麻烦。 我想做的是:

  1. 运行@BeforeTest
  2. 运行测试编号1(成功登录)
  3. 运行@AfterTest(关闭浏览器和驱动程序)
  4. 等待,直到AfterTest方法完成,然后运行测试编号2(登录失败)

但是我经历的是:

  1. 运行@BeforeTest
  2. 运行测试编号1(成功登录)
  3. 在完成测试编号1之后立即运行测试编号2(浏览器未关闭,并且登录状态保持先前的测试状态)
  4. AfterTest运行

我花了2天,但不知道怎么做。 我的代码如下:

================================================== ===============

package TestNG;

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.*;

public class GoogleAccountLogin {

    WebDriver driver;

    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.gecko.driver","F:\\path\\geckodriver.exe");
        driver=new FirefoxDriver();
    }

    @Test(priority=0)
    public void LoginSuccessful() throws InterruptedException { 

    // Go to google account
        driver.manage().window().maximize();
        driver.get("https://accounts.google.com");
        Thread.sleep(3000);

    // Check if the page is correct
        String currentTitle = driver.getTitle();
        Assert.assertEquals(currentTitle, "Sign in - Google Accounts");

    // Enter email and submit

        WebElement email = driver.findElement(By.id("Email"));
        email.clear();
        email.sendKeys("validemail");
        WebElement Next = driver.findElement(By.id("next"));
        Next.click();
        Thread.sleep(1000);

    // Enter password

        WebElement password = driver.findElement(By.id("Passwd"));
        password.clear();
        password.sendKeys("validpassword");
        WebElement Login = driver.findElement(By.id("signIn"));
        Login.click();  
        Thread.sleep(5000);

    // Check if login successful
        currentTitle = driver.getTitle();
        Assert.assertEquals(currentTitle, "My Account");
    }

    @Test(priority=1)
    public void LoginFailInvalidEmail() throws InterruptedException {

    // Go to google account
        driver.manage().window().maximize();
        driver.get("https://accounts.google.com");
        Thread.sleep(3000);

    // Check if the page is correct
        String currentTitle = driver.getTitle();
        Assert.assertEquals(currentTitle, "Sign in - Google Accounts");

    // Enter email and submit    
        WebElement email = driver.findElement(By.id("Email"));
        email.clear();
        email.sendKeys("falseemail");
        WebElement Next = driver.findElement(By.id("next"));
        Next.click(); 
        Thread.sleep(1000);

    // Check error message and login state    
        String errorMess = driver.findElement(By.id("errormsg_0_Email")).getText();
        Assert.assertEquals(errorMess, "Sorry, Google doesn't recognize that email.");
        currentTitle = driver.getTitle();
        Assert.assertEquals(currentTitle, "Sign in - Google Accounts");
    }

    @AfterTest
    public void tearDown() { 
        driver.close();
        driver.quit();
    }

}

TestNG文档

@BeforeTest :带注释的方法将在运行属于该标签内的类的任何测试方法之前运行。

@AfterTest :带注释的方法将在标签内部所有属于类的测试方法运行后运行。

如果要在每个测试方法之前和/或之后运行某些程序,则需要使用@BeforeMethod@AfterMethod

@BeforeMethod :带注释的方法将在每个测试方法之前运行。

@AfterMethod :带注释的方法将在每个测试方法之后运行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM