Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何仅在@AfterTest完成后运行测试_Java_Selenium Webdriver_Testng - Fatal编程技术网

Java 如何仅在@AfterTest完成后运行测试

Java 如何仅在@AfterTest完成后运行测试,java,selenium-webdriver,testng,Java,Selenium Webdriver,Testng,我正在与TestNG一起学习Java中的SeleniumWebDriver。我正在测试谷歌登录页面 我无法按顺序运行测试用例。我想做的是: 运行@BeforeTest 运行测试1(成功登录) 运行@AfterTest(关闭浏览器和驱动程序) 等待后测试方法完成然后运行测试2(登录失败) 但我所经历的是: 运行@BeforeTest 运行测试1(成功登录) 在1号测试完成后立即运行2号测试(浏览器未关闭,登录状态与上一个测试保持一致) 后测运行 我花了2天时间,但不知道怎么做。我的代码如下: ==

我正在与TestNG一起学习Java中的SeleniumWebDriver。我正在测试谷歌登录页面

我无法按顺序运行测试用例。我想做的是:

  • 运行@BeforeTest
  • 运行测试1(成功登录)
  • 运行@AfterTest(关闭浏览器和驱动程序)
  • 等待后测试方法完成然后运行测试2(登录失败)
  • 但我所经历的是:

  • 运行@BeforeTest
  • 运行测试1(成功登录)
  • 在1号测试完成后立即运行2号测试(浏览器未关闭,登录状态与上一个测试保持一致)
  • 后测运行
  • 我花了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();
        }
    
    }
    
    从:

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

    @postertest:带注释的方法将在属于标记内类的所有测试方法运行后运行

    如果您想在每个测试方法之前和/或之后运行某些内容,则需要使用
    @beforethod
    @AfterMethod

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

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


    是的,没错。我已经测试过了,而且成功了。天哪,教程怎么说每次@Test>之前都会执行BeforeTest。