Authentication TestBenchTestCase中的依赖项注入

Authentication TestBenchTestCase中的依赖项注入,authentication,dependency-injection,vaadin,vaadin-flow,vaadin-testbench,Authentication,Dependency Injection,Vaadin,Vaadin Flow,Vaadin Testbench,我开始使用TestBench为我的Vaadin Flow应用程序创建集成测试,我想要测试的事情之一是成功登录。为了使用有效凭据测试登录,我需要提供凭据。但是我真的想避免将我的凭证硬编码到测试用例中 所以,我想使用从我的settings.xml注入我的用户名和pwd,但为了做到这一点,我的测试类需要是一个spring管理的bean 有没有办法使我的TestBenchTestCase成为Spring管理的Bean?还是有更好的方法来实现我的目标?我相信执行成功的登录最终会在几乎所有与TestBenc

我开始使用TestBench为我的Vaadin Flow应用程序创建集成测试,我想要测试的事情之一是成功登录。为了使用有效凭据测试登录,我需要提供凭据。但是我真的想避免将我的凭证硬编码到测试用例中

所以,我想使用从我的settings.xml注入我的用户名和pwd,但为了做到这一点,我的测试类需要是一个spring管理的bean


有没有办法使我的TestBenchTestCase成为Spring管理的Bean?还是有更好的方法来实现我的目标?我相信执行成功的登录最终会在几乎所有与TestBench的集成测试用例开始时使用?

仅回答这个问题,您可以使用
@TestPropertySource(locations=“…”)
&
@RunWith(SpringRunner.class)
,下面您可以找到一个完整的(尽管很简单)示例(也是一个小的示例)

但是,根据您的最终目标(单元测试、回归、系统、压力等),您可能需要重新考虑您的方法,比如有一个初始“设置”部分,为系统提供运行整个套件所需的任何数据,可能包括创建和授权要使用的专用用户帐户

1)代码

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)结果


您的测试是否应该使用真实登录?我觉得你应该嘲笑这些东西?这是很好的输入,我没想到。在运行应用程序进行集成测试时,我必须使用
@Profile
注释来使用模拟的AuthenticationProvider。我会仔细考虑的,谢谢。这取决于这是单元测试还是系统/回归/压力/性能测试。由于问题提到了测试台,我倾向于认为是后者,但只有OP可以说。如果是这样,那么他可能需要一个“配置/设置”步骤,以确保最低限度的配置到位,可能包括为自动化测试套件创建/授权用户。