Java 硒化锡表示法

Java 硒化锡表示法,java,selenium,testng,Java,Selenium,Testng,我有这段代码,如何更改参数的条件(如果“test1”? 我不知道如何通过@AfterMethod中的参数引入值“test1”来调用这个类 public class Transformer implements IAnnotationTransformer { @Override public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method

我有这段代码,如何更改参数的条件(如果“test1”? 我不知道如何通过@AfterMethod中的参数引入值“test1”来调用这个类

public class Transformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod){      
       if (testMethod.getName().equals("test1")){
            System.out.println("Disable " + testMethod.getName());
            annotation.setEnabled(false);
        }
    }
}

如果我理解的没错,你想要这样的东西:

public class Transformer implements IAnnotationTransformer {
  String name;

  // create constructor with 'testName' as parameter
  public Transformer(String testName){
    this.name = testName;
  }

  @Override
  public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod){
    if (testMethod.getName().equals(name)){
      System.out.println("Disable " + testMethod.getName());
      annotation.setEnabled(false);
    }
  }
}
Transformer transformer = new Transformer("testName");
transformer.transform(...);
public class Transformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod, String testName){      
       if (testMethod.getName().equals(testName)){
            System.out.println("Disable " + testMethod.getName());
            annotation.setEnabled(false);
        }
    }
}
你可以这样称呼这个类:

public class Transformer implements IAnnotationTransformer {
  String name;

  // create constructor with 'testName' as parameter
  public Transformer(String testName){
    this.name = testName;
  }

  @Override
  public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod){
    if (testMethod.getName().equals(name)){
      System.out.println("Disable " + testMethod.getName());
      annotation.setEnabled(false);
    }
  }
}
Transformer transformer = new Transformer("testName");
transformer.transform(...);
public class Transformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod, String testName){      
       if (testMethod.getName().equals(testName)){
            System.out.println("Disable " + testMethod.getName());
            annotation.setEnabled(false);
        }
    }
}
编辑:或者您可以这样做:

public class Transformer implements IAnnotationTransformer {
  String name;

  // create constructor with 'testName' as parameter
  public Transformer(String testName){
    this.name = testName;
  }

  @Override
  public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod){
    if (testMethod.getName().equals(name)){
      System.out.println("Disable " + testMethod.getName());
      annotation.setEnabled(false);
    }
  }
}
Transformer transformer = new Transformer("testName");
transformer.transform(...);
public class Transformer implements IAnnotationTransformer {

    @Override
    public void transform(ITestAnnotation annotation, Class testClass,Constructor testConstructor, Method testMethod, String testName){      
       if (testMethod.getName().equals(testName)){
            System.out.println("Disable " + testMethod.getName());
            annotation.setEnabled(false);
        }
    }
}
附言:我也发现了类似的情况。也许它会有用:

TestNG testNG = new TestNG();
testNG.setAnnotationTransformer(new Transformer("TestName"));
testNG.setTestClasses(new Class[] {YourTestClass.class});
testNG.run();

希望有帮助。

我不知道如何调用这个类,从@AfterMethod中的一个参数中引入值“test1”
没有得到你想要的。请提供您拥有的代码和更详细的解释我拥有所有带有注释“@Test(enabled=false)”的测试,我只想启用一个测试,给出执行testng.xml的方法的名称。为什么您要引用@AfterMethod?如果我认为您需要在before方法中启用/禁用测试。您是否正在设置一个testng.xml文件,在其中注册您的转换器?但是,当我写入:Transformer Transformer=new Transformer(“testName”);transform(注释、testClass、testConstructor、testMethod);什么是参数注释,testClass,testConstructor和testMethod?什么是参数注释,testClass,testConstructor和testMethod我不知道。这是您的代码;)。PS我还添加了第二个建议,请看。@BillyMartinez我添加了其他信息,请看。