jmix-ui-tests
jmix-ui-tests
Jmix UI test suite
Dependencies
- JUnit 5 (https://junit.org/junit5/)
- Selenide (https://ru.selenide.org/)
- Testcontainers (https://www.testcontainers.org/)
- SpringBoot (https://spring.io/projects/spring-boot)
- Liquibase (https://www.liquibase.org/)
- Jmix Masquerade (https://github.com/Haulmont/jmix-masquerade)
Description
There are two packages in
:
- contains UI tests for testing Jmix projectsui
- contains UI tests for testing Jmix Sampler projectsampler
Properties
- Use
property to define the base host of the main application for selenide browsers in ui package;jmix.tests.ui.baseHost - Use
property to define the base url for selenide browsers in sampler package;jmix.tests.sampler.baseUrl
SpringBoot tests
All tests in UI package are based on Spring Boot tests. This means that you do not need to separately launch the main
application. The application instance is launched along with the tests. The test application instance runs on a random port,
using
the setting. This way you can run multiple tests at the same time, as
well as run tests while the main application is running.
In order to have JUnit 5 library support, you need to add the SpringBootExtension in your tests.
Tests in sampler package are not based on Spring Boot tests. Therefore, they need to be launched only when the Jmix Sampler application is already running.
Selenide browser containers
Test browser containers are used to instantiate web browsers. Creation of browser containers is fast, so it's actually
quite feasible to have a totally fresh browser instance for every test.
There are two browser containers in
:
- ChromeExtension - a junit5 extension that
creates a test container with Chrome browser for each test:
@ExtendWith(ChromeExtension)class UiTest {@Testvoid test() {...}}
- FirefoxExtension - a junit5 extension that
creates a test container with Firefox browser for each test:
@ExtendWith(FirefoxExtension)class UiTest {@Testvoid test() {...}}
Debugging browser in test container
Each test browser container contains VNC recorder for debugging tests. You can find the following line in the logs when starting the test:
INFO 5504 --- [Test worker] i.j.t.extension.BrowserExtension : VNC recorder url: vnc://localhost:32903, password='secret'
Then open the page from the URL in your browser and enter the password provided.
Locally installed browser drivers
Please note that you need to download one of the latest versions of the web driver
depending on the browser you want to use to testing. For Chrome browser this is
chromedriver, for Firefox this is
geckodriver. Also, you need to
remove the use of
annotation for test class.
Locally installed Chrome browser driver
If you run your tests in Chrome browser, you need to edit standard test configuration for the test project in IntelliJ. To do so, click the Select Run/Debug Configuration button and select Edit Configurations in the drop-down list. In the VM options field, add the following:
- UI package
-Pselenide.browser=chrome
-Pwebdriver.chrome.driver=<your_path>/chromedriver.exe
-Pjmix.tests.ui.baseHost=http://localhost
- Sampler package
-Pselenide.browser=chrome
-Pwebdriver.chrome.driver=<your_path>/chromedriver.exe
-Pjmix.tests.sampler.baseUrl=http://localhost:8080/sampler
where
is the path to the chrome driver on your computer.
After that select the simple test or the test class you want to run, right click on it and select Debug option.
To run the tests using Gradle, run the following task in the terminal:
- UI package
gradle testUi -Pselenide.browser=chrome -Pwebdriver.chrome.driver=<your_path>/chromedriver.exe -Pjmix.tests.ui.baseHost=http://localhost
- Sampler package
gradle testSampler -Pselenide.browser=chrome -Pwebdriver.chrome.driver=<your_path>/chromedriver.exe -Pjmix.tests.sampler.baseUrl=http://localhost:8080/sampler
where
is the path to the chrome driver on your computer.
Locally installed Firefox browser driver
If you run your tests in Firefox browser, you need to edit standard test configuration for the test project in IntelliJ. To do so, click the Select Run/Debug Configuration button and select Edit Configurations in the drop-down list. In the VM options field, add the following:
- UI package
-Pselenide.browser=firefox
-Pwebdriver.gecko.driver=<your_path>/geckodriver.exe
-Pjmix.tests.ui.baseHost=http://localhost
- Sampler package
-Pselenide.browser=firefox
-Pwebdriver.gecko.driver=<your_path>/geckodriver.exe
-Pjmix.tests.sampler.baseUrl=http://localhost:8080/sampler
where
is the path to the firefox driver on your computer.
After that select the simple test or the test class you want to run, right click on it and select Debug option.
To run the tests using Gradle, run the following task in the terminal:
- UI package
gradle testUi -Pselenide.browser=firefox -Pwebdriver.gecko.driver=<your_path>/geckodriver.exe -Pjmix.tests.ui.baseHost=http://localhost
- Sampler package
gradle testSampler -Pselenide.browser=firefox -Pwebdriver.gecko.driver=<your_path>/geckodriver.exe -Pjmix.tests.sampler.baseUrl=http://localhost:8080/sampler
where
is the path to the firefox driver on your computer.
Database containers
The database containers are used from the
library. For example, in order to add a container with a
database, you need to add two dependencies:
- a PostgreSql database implmentation;testRuntime 'org.postgresql:postgresql:42.2.16'
- a test container implementation.testImplementation 'org.testcontainers:postgresql:1.14.3'
A fresh database is created for each test class. In order to determine what data should be initialized in the database, liquibase contexts are used. The context for main application is "base", it must always be added to the list of contexts for the application to work correctly.
To create UI test that uses a test dataset in addition to the base dataset, you need to follow these steps:
- Create an extension to define database test container:
class PostgreSQLExtension extends SpringExtension {
private PostgreSQLContainer postgreSQLContainer
@Override
void beforeAll(ExtensionContext context) throws Exception {
postgreSQLContainer = new PostgreSQLContainer()
.withDatabaseName("postgres-test-db")
.withUsername("test")
.withPassword("pass")
postgreSQLContainer.start()
getApplicationContext(context).getBean(JmixLiquibase).afterPropertiesSet()
}
@Override
void afterAll(ExtensionContext context) throws Exception {
postgreSQLContainer.stop()
}
}
- Create a Spring Initializer or
file to define datasource properties:application.properties
class TestContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"main.datasource.jdbcUrl=jdbc:tc:postgresql:9.6.12:///postgres-test-db",
"main.datasource.username=test",
"main.datasource.password=pass",
"jmix.data.dbmsType=postgres",
"jmix.liquibase.dropFirst=true"
).applyTo(applicationContext.getEnvironment())
}
}
- Create a changeSet and define a context for it:
<changeSet id="4" author="jmix-ui-tests" context="test-role">
<insert tableName="SEC_ROLE_ENTITY">
<column name="ID" value="cc9e420a-2b7a-4c42-8654-a9027ee14083"/>
<column name="VERSION" value="1"/>
<column name="NAME" value="test-selenium"/>
<column name="CODE" value="test-selenium"/>
</insert>
</changeSet>
In the above example, we have created a change set with the
context.
4. Create a Spring Boot test, define
property inside
annotation and define
the initializer class inside
annotation:
@ExtendWith([
SpringBootExtension,
PostgreSQLExtension
])
@SpringBootTest(classes = JmixUiTestsApplication,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = ['jmix.liquibase.contexts=base,test-role'])
@ContextConfiguration(initializers = TestContextInitializer)
class UserUiTest extends BaseUiTest {
@Test
void test() {
...
}
}
As a result, your tests will have data from two contexts:
and
.
Run tests
- Tests in UI package - gradle testUI
- Tests in Sampler package - gradle testSampler
- All tests gradle test
Sample test in UI package
@ExtendWith([
SpringBootExtension,
ChromeExtension
])
@SpringBootTest(classes = JmixUiTestsApplication,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
properties = ['jmix.liquibase.contexts=base,test-role'])
@ContextConfiguration(initializers = PostgreSQLContextInitializer)
class UserUiTest extends BaseUiTest {
@Test
@DisplayName("Creates new user")
void createNewUser() {
loginAsAdmin()
$j(MainScreen).with {
sideMenu.openItem(USER_BROWSE)
.createUser()
}
$j(UserEditor).with {
usernameField.setValue('newUsername')
passwordField.setValue('qO4Hn6o')
confirmPasswordField.setValue('qO4Hn6o')
addBtn.click()
$j(RoleModelLookup).with {
roleModelsTable.getCell(withText('test-selenium'))
.click()
lookupSelectAction.click()
}
windowCommitAndClose.click()
}
$j(UserBrowse).usersTable
.getCell(withText('newUsername'))
.shouldBe(visible)
}
}
Sample test in Sampler package
@ExtendWith(ChromeExtension)class ButtonSamplerUiTest extends BaseSamplerUiTest {
@Test @DisplayName("Checks that user can click on simple button") void clickOnSimpleButton() { openSample('button-simple')
$j(Button.class, 'helloButton') .shouldHave(caption('Say Hello!')) .click()
$j(Notification.class) .shouldHave(caption('Hello, world!')) }}