2

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?

Erik Lumme
  • 5,202
  • 13
  • 27
kscherrer
  • 5,486
  • 2
  • 19
  • 59
  • 1
    Should your test actually be using the real login though? I think you should be mocking these kinds of things? – Jay Mar 19 '19 at 09:00
  • That is good input, I haven't thought of that. I would have to make use of `@Profile` annotation to use a mocked AuthenticationProvider when running the application for integration tests. I will think that over, thanks. – kscherrer Mar 19 '19 at 09:15
  • @Jay that kind of depends whether this is unit testing or system/regression/stress/performance testing. Since the question mentions Test Bench I tend to think it's the latter, but only the OP can say. If so, then he probably needs a _"provisioning/setup"_ step which ensures the minimum required configuration is in place, possibly including the creation/authorization of an user for the automated test suite. – Morfic Mar 19 '19 at 09:54

1 Answers1

4

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

Morfic
  • 15,178
  • 3
  • 51
  • 61