4

How can we use Selenium-Jupiter's @TestTemplate (to have it run with different browsers: https://bonigarcia.github.io/selenium-jupiter/#template-tests ) to combine it with @ParameterizedTest (for data-driven testing, e.g. try different credentials, defined in a source, on a login page)?

I have not been able to figure it out. Instead of being able to use Selenium-Jupiter's @TestTemplate to configure the browser scenario programmatically (seleniumExtension.addBrowsers and with Webdriver as the parameter type of the method templateTest) where the method would be executed twice (one using Chrome and the second using Firefox), I can only make it work with two @ParameterizedTest methods:

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.
Patryk Rudnicki
  • 755
  • 1
  • 8
  • 21
DieterR
  • 41
  • 2

1 Answers1

0

I think @TestTemplate and @ParameterizedTest cannot be combined in JUnit 5. Therefore, it cannot be used in Selenium-Jupiter either.

Boni García
  • 4,618
  • 5
  • 28
  • 44
  • I think I see what you mean: since you are using TestTemplate, usage of ParameterizedTest, and the associated annotations such as the CSV reader annotation, is not supported? – djangofan Mar 30 '20 at 16:01