Search code examples
authenticationdependency-injectionvaadinvaadin-flowvaadin-testbench

Dependency Injection in a TestBenchTestCase


I am starting to create integration tests for my Vaadin Flow application using TestBench, and one of the things I want to test is a successful login. In order to test the login with valid credentials, I need to provide my credentials. But I really want to avoid writing my credentials hardcoded into the testcase.

Therefore, I would like to make use of the @Value annotation to inject my username and pwd from my settings.xml but in order to do that, my Test class needs to be a spring-managed bean.

Is there a way to make my TestBenchTestCase a Spring-managed Bean? Or is there a better way to achieve my goal? I would believe that performing a successful login is ultimately used at the start of almost all integration test cases with TestBench?


Solution

  • Replying solely to the question, you can use @TestPropertySource(locations="...") & @RunWith(SpringRunner.class), below you can find a full (naive nonetheless) sample (also a small intro).

    However, depending on your end goal (unit testing, regression, system, stress, etc) you may want to reconsider your approach, like having an initial "setup" section that provisions the system with whatever data is required to run the whole suite, possibly including the creation and authorization of dedicated user accounts to be used.

    1) Code

    package com.example;
    
    import com.vaadin.testbench.TestBenchTestCase;
    import com.vaadin.testbench.elements.TextFieldElement;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.test.context.TestPropertySource;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import static org.hamcrest.Matchers.is;
    import static org.junit.Assert.assertThat;
    
    @RunWith(SpringRunner.class)
    @TestPropertySource(locations = "my-custom-config.properties")
    public class SpringTestbenchTest extends TestBenchTestCase {
    
        @Value("${input.text:unknown}")
        private String text;
    
        @Before
        public void setUp() throws Exception {
            System.setProperty("webdriver.chrome.driver", "D:\\Kit\\Selenium\\chromedriver_win32\\chromedriver.exe");
            setDriver(new ChromeDriver());
        }
    
        @After
        public void tearDown() throws Exception {
            getDriver().quit();
        }
    
        @Test
        public void shouldTypeTextInInputBox() {
            // open the browser
            getDriver().get("https://demo.vaadin.com/sampler/#ui/data-input/text-input/text-field");
    
            // wait till the element is visible
            WebDriverWait wait = new WebDriverWait(getDriver(), 5);
            TextFieldElement textBox = (TextFieldElement) wait.until(ExpectedConditions.visibilityOf($(TextFieldElement.class).first()));
    
            // set the value and check that its caption was updated accordingly
            textBox.setValue(text);
            assertThat(textBox.getCaption(), is(Math.min(text.length(), 10) + "/10 characters"));
        }
    }
    

    2) src/test/resources/com/example/my-custom-config.properties

    input.text=vaadin
    

    3) Result

    result