Java 如何仅对一种试验方法执行AfterMethod

Java 如何仅对一种试验方法执行AfterMethod,java,automation,testng,Java,Automation,Testng,我必须在我的代码中测试方法,我只想为一个执行aftermethod。有人知道怎么做吗 这是我的密码: package Demo; import java.util.concurrent.TimeUnit; import library.Utility; import org.openqa.selenium.By; import org.testng.ITestResult; import org.testng.annotations.AfterMethod; import org.test

我必须在我的代码中测试
方法,我只想为一个执行
aftermethod
。有人知道怎么做吗

这是我的密码:

package Demo;

import java.util.concurrent.TimeUnit;

import library.Utility;

import org.openqa.selenium.By;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import Pages.custom_actions_page;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

public class Custom_Actions extends start {

    ExtentReports report;
    ExtentTest logger;
    String driverPath = "D:\\geckodriver-v0.16.1-win64\\geckodriver.exe";

    @Test()
    public void signin() throws Exception {

        // To Locate the Username field
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.id("username")).sendKeys("admin");

        // To locate the Password field
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.id("password")).sendKeys("admin123");

        // Click on Login button
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.id("submit")).click();

    }

    @Test(dependsOnMethods = { "signin" })
    public void create_custom_action() {
        report = new ExtentReports("D:/Reports/Report.html");

        logger = report.startTest("Create Custom Action");

        new custom_actions_page(driver).submit();

        new custom_actions_page(driver).admin();

        new custom_actions_page(driver).custom_ac();

        new custom_actions_page(driver).createnew();

        new custom_actions_page(driver).nameAs("fortesting").descriptionAs(
                "description");

        new custom_actions_page(driver).category();

        new custom_actions_page(driver).assetsubtype();

        new custom_actions_page(driver).assettype();

        new custom_actions_page(driver).flintnameAs("hello:example.rb");

        new custom_actions_page(driver).submit_butto();

        new custom_actions_page(driver).Save_Button();

        logger.log(LogStatus.PASS, "Custom Action Created Successfully");
    }

    @AfterMethod()
    public void tearDown(ITestResult result) {

        // Here will compare if test is failing then only it will enter into if
        // condition
        if (ITestResult.FAILURE == result.getStatus()) {
            try {

                Utility.captureScreenshot(driver, "CustomActionFail.png");

            } catch (Exception e) {
                System.out.println("Exception while taking screenshot "
                        + e.getMessage());
            }
        }

        report.endTest(logger);
        report.flush();
        driver.close();
    }}

@AfterMethod
的设计目的是,当您需要在每次测试后执行相同的代码块时,无需在每次测试中重复它,您的生活就会变得更轻松。因此,如果您只需要在一次测试之后执行它,只需将它嵌入到
@test
方法中,并删除
@AfterMethod
注释的方法。

在TestNG中使用称为
本机注入的方法有几种。有关TestNG中本机注入的可能组合的更多详细信息,请参阅

最简单的方法是从带注释的
@AfterMethod
方法中检查即将执行的
@Test
方法的名称,如果它与需要特殊执行的方法的名称匹配,则继续,否则跳过执行
@AfterMethod
。 但这是一种原始的方法,因为如果您将测试方法的名称重构为其他名称,那么您的方法就会被破坏

另一种方法是基本上使用标记接口,并执行与上述相同的逻辑

下面是一个示例,它展示了所有这一切

标记注释如下所示

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class SampleTestClass {

    @NeedsSpecialSetup
    @Test
    public void testMethod1() {
        System.err.println("Executing testMethod1()");
    }

    @Test
    public void testMethod2() {
        System.err.println("Executing testMethod2()");
    }

    @AfterMethod
    public void afterMethod(Method method) {
        NeedsSpecialSetup needsSpecialSetup = method.getAnnotation(NeedsSpecialSetup.class);
        if (needsSpecialSetup == null) {
            //Don't execute this setup because the method doesn't have the
            //special setup annotation.
            return;
        }
        System.err.println("Running special setup for " + method.getName() + "()");
    }
}
import java.lang.annotation.Retention;
导入java.lang.annotation.Target;
@保留(java.lang.annotation.RetentionPolicy.RUNTIME)
@目标(java.lang.annotation.ElementType.METHOD)
公共@接口需要特殊设置{
}
现在,您的测试类将如下所示

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.lang.reflect.Method;

public class SampleTestClass {

    @NeedsSpecialSetup
    @Test
    public void testMethod1() {
        System.err.println("Executing testMethod1()");
    }

    @Test
    public void testMethod2() {
        System.err.println("Executing testMethod2()");
    }

    @AfterMethod
    public void afterMethod(Method method) {
        NeedsSpecialSetup needsSpecialSetup = method.getAnnotation(NeedsSpecialSetup.class);
        if (needsSpecialSetup == null) {
            //Don't execute this setup because the method doesn't have the
            //special setup annotation.
            return;
        }
        System.err.println("Running special setup for " + method.getName() + "()");
    }
}

请注意,我们如何仅为方法
testMethod1()
添加注释
@NeedsSpecialSetup
,以指示我们需要仅为
testMethod1()
执行
@AfterMethod

这是输出

Executing testMethod1()

Running special setup for testMethod1()
Executing testMethod2()

===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

提示:保持包名小写,类名以大写字母开头!(开始)。@ValentinGrégoire-谢谢你的提示。@ValentinGrégoire-嘿,你能帮我解决一个问题吗?在上面的代码中,我有两个测试方法,我只想为其中一个运行AfterMethod。你知道怎么做吗?谢谢。在问题发布后完全改变它不是一个好的做法。如果您想问另一个问题,请创建一个新问题。当情况发生巨大变化时,跟踪问题和回答会变得非常困难。@JeffC-对于由此造成的不便,我深表歉意。下次会小心的。很有趣。因为我认为在默认情况下,任何发布问题的人都应该能够接受回答,而不管他们的声誉如何