结合@TestTemplate与@ParameterizedTest吗?

时间:2018-10-26 09:44:32

标签: java junit5 selenium-jupiter

我们如何使用Selenium-Jupiter's @TestTemplate(使其在不同的浏览器上运行:https://bonigarcia.github.io/selenium-jupiter/#template-tests)与@ParameterizedTest结合使用(用于数据驱动的测试,例如,尝试使用来源,在登录页面上)?

我无法弄清楚。 不能使用Selenium-Jupiter's @TestTemplate以编程方式(seleniumExtension.addBrowsers并将Webdriver作为方法templateTest的参数类型)来配置浏览器方案,该方法将执行两次(一种使用Chrome,另一种使用Firefox),我只能使其与两种@ParameterizedTest方法一起使用:

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.extension.RegisterExtension;

import static org.junit.jupiter.params.provider.Arguments.arguments;

import io.github.bonigarcia.SeleniumExtension;

public class TestDDTparamNoTestTemplate {
    @RegisterExtension
    static SeleniumExtension seleniumExtension = new SeleniumExtension();

    @BeforeAll
    static void setup() {
        //to stop geckodriver logging: (this is for firefox, not for firefoxInDocker)
        System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");
    }

    //methodsource with factory method that must generate a stream of arguments:
    static Stream<Arguments> purposeEmailPasswordNameProvider() {
        return Stream.of(
                arguments("Login works", "alexsiminiuc3@gmail.com", "Password123!", "Alex"),
                arguments("Incorrect email", "alexsiminiuc2@gmail.com", "Password123!", "Alex"),
                arguments("Invalid email", "alexsiminiuc", "Password123!", "Alex")
        );
    }

    //CHROME
    @ParameterizedTest
    @MethodSource("purposeEmailPasswordNameProvider")          // I could also use a @CsvSource or a @CsvFileSource
    void testArgumentAggregMethodSourceChrome(ArgumentsAccessor arguments,ChromeDriver driver) {
        String purpose = arguments.getString(0);
        System.out.println("purpose is: " + purpose);
        User user = new User(arguments.getString(1),
                arguments.getString(2),
                arguments.getString(3));

        testCASE(driver, user, purpose);
    }

    //FIREFOX
    @ParameterizedTest
    @MethodSource("purposeEmailPasswordNameProvider")  
    void testArgumentAggregMethodSourceFirefox(ArgumentsAccessor arguments,FirefoxDriver driver) {
        String purpose = arguments.getString(0);
        System.out.println("purpose is: " + purpose);
        User user = new User(arguments.getString(1),
                arguments.getString(2),
                arguments.getString(3));

        testCASE(driver, user, purpose);
    }

    void testCASE(WebDriver driver, User user, String purpose){
        HomePage homePage = new HomePage(driver);
        homePage.open();
        if (purpose.equals("Login works")) {
        etc.

1 个答案:

答案 0 :(得分:0)

我认为@TestTemplate@ParameterizedTest在JUnit 5中不能合并。因此,它们也不能在Selenium-Jupiter中使用。