TestNG setUpMethod() 问题

TestNG setUpMethod() Issues

我在个别测试用例中通过不同环境时遇到问题。我试过在这些测试用例中覆盖 setUpMethod() 但没有成功,我不确定如何继续。任何帮助将不胜感激。

protected WebDriver driver;
protected Actions actions;
protected WebDriverWait wait;
protected String url;



@BeforeMethod
@Parameters("env")
public void setUpMethod(@Optional String env){
    System.out.println("env = " + env);

    //if env variable is null use default url
    if(env==null){
        url=ConfigurationReader.getProperty("url");
    }else{
        url=ConfigurationReader.getProperty(env+"_url");
    }
    //if it is not null, choose env based on value

    driver = Driver.getDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    actions = new Actions(driver);
    wait = new WebDriverWait(driver,10);

    driver.get(url);

}

@AfterMethod
public void tearDownMethod(){

    driver.quit();

}

通过 @Parameter 尝试的选项不会扩展,因为您不能使用 TestNG 套件 xml 文件将多个参数传递给同一 class 中的多个方法。

总而言之,您可以使用两个选项来执行此操作。

选项:1 - 注释驱动方法

假设与 @Test 方法关联的 URL 没有改变,那么我们可以通过自定义注释将此信息嵌入到 @Test 方法本身之上。

示例如下。

自定义注释

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

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

  /**
   * @return - The URL to use
   */
  String url();
}

使用自定义注释的示例测试class

import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class AnnotationDrivenSample {

  @BeforeMethod
  public void beforeMethod(ITestResult testResult) {
    String url = "http://example.org";
    Environment env = testResult.getMethod().getConstructorOrMethod().getMethod()
        .getAnnotation(Environment.class);
    if (env != null) {
      url = env.url();
    }
    System.err.println(testResult.getMethod().getQualifiedName() + "() will use the URL : " + url);
  }

  @Test
  @Environment(url = "http://example.net")
  public void a() {
    System.err.println("A() executed");
  }

  @Test
  @Environment(url = "http://example.com")
  public void b() {
    System.err.println("B() executed");
  }
}

这是输出:

com.rationaleemotions.Whosebug.qn66807918.AnnotationDrivenSample.a() will use the URL : http://example.net
A() executed
com.rationaleemotions.Whosebug.qn66807918.AnnotationDrivenSample.b() will use the URL : http://example.com
B() executed

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

**选项:2 - 注释驱动方法与数据提供者相结合**

在这种方法中,假设您的 @Test 方法由数据提供者提供支持,而实际 URL 来自数据提供者检索的数据源。在那种情况下,我们注释接收 url 的参数,然后解析它。

这是一个示例:

自定义注释

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

@Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
@Documented
/**
 * This is a marker annotation that indicates that the parameter is to be given a special treatment.
 */
public @interface UrlInfo {}

这里是示例测试class

import java.lang.reflect.Parameter;
import org.testng.ITestResult;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class DataProviderDrivenSample {

  @BeforeMethod
  public void beforeMethod(ITestResult testResult) {
    String url = "http://example.org";
    Parameter[] parameters = testResult.getMethod().getConstructorOrMethod().getMethod()
        .getParameters();
    for (int i = 0; i < parameters.length; i++) {
      UrlInfo urlInfo = parameters[i].getAnnotation(UrlInfo.class);
      if (urlInfo != null) {
        url = testResult.getParameters()[i].toString();
        break;
      }
    }
    System.err.println(testResult.getMethod().getQualifiedName() + "() will use the URL : " + url);
  }

  @Test(dataProvider = "dp")
  public void testMethod(@UrlInfo String url, int anotherParameter) {
    System.err.println("testMethod is running ");
  }

  @DataProvider(name = "dp")
  public Object[][] getTestData() {
    return new Object[][]{
        {"http://example.net", 100},
        {"http://example.com", 100},
    };
  }

}

这是输出

com.rationaleemotions.Whosebug.qn66807918.DataProviderDrivenSample.testMethod() will use the URL : http://example.net
testMethod is running 
com.rationaleemotions.Whosebug.qn66807918.DataProviderDrivenSample.testMethod() will use the URL : http://example.com
testMethod is running 

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