如何只对一个Test方法执行AfterMehtod

时间:2017-11-16 13:36:57

标签: java automation testng

我的代码中必须使用Test个方法,而我只想执行AfterMehtod个方法。任何人都知道这是怎么做到的?

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

@AfterMethod旨在让您在每次测试后执行相同的代码块时更轻松,而不必在每次测试中复制它。因此,如果您只需要在一次测试后执行它,只需将其嵌入到@Test方法中并删除@AfterMethod带注释的方法。

答案 1 :(得分:0)

有两种方法可以使用TestNG中的public static void main(String[] args) { int[][] array = fill(); System.out.println(biggest(array)); } 进行操作。有关TestNG中原生注射的可能组合的更多详细信息,请参阅here

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

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

这是一个显示所有这些内容的示例。

标记注释如下所示

@AfterMethod

现在您的测试类看起来如下

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target(java.lang.annotation.ElementType.METHOD)
public @interface NeedsSpecialSetup {
}

请注意我们仅为方法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() + "()"); } } 添加了注释@NeedsSpecialSetup,以表明我们只需要为testMethod1()执行@AfterMethod

这是输出

testMethod1()