简体   繁体   中英

How to run test only after the @AftertTest is finished

I'm learning Selenium Webdriver in Java together with TestNG. I'm testing with Google Login page.

I'm having trouble with running test cases sequentially. What I want to do is:

  1. Run @BeforeTest
  2. Run Test number 1 (Login successfully)
  3. Run @AfterTest (close the browser and driver)
  4. Wait until the AfterTest method is finished then run Test number 2 (Login Fail)

But what I experienced is:

  1. Run @BeforeTest
  2. Run Test number 1 (Login successfully)
  3. Run Test number 2 right after the Test number 1 is finished (browser is not closed and login state is kept from previous test
  4. AfterTest is run

I spent 2 days but couldn't figure how. My code below:

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

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();
    }

}

From the TestNG documentation :

@BeforeTest : The annotated method will be run before any test method belonging to the classes inside the tag is run.

@AfterTest : The annotated method will be run after all the test methods belonging to the classes inside the tag have run.

If you want to run something before and/or after each test-method you need to use @BeforeMethod and @AfterMethod :

@BeforeMethod : The annotated method will be run before each test method.

@AfterMethod : The annotated method will be run after each test method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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