-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from spine-examples/ui-testing
Implement UI testing for Login page
- Loading branch information
Showing
18 changed files
with
372 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,4 @@ | |
# | ||
|
||
server.address=localhost | ||
server.port=50051 | ||
server.port=4242 |
111 changes: 111 additions & 0 deletions
111
desktop/src/test/kotlin/io/spine/examples/pingh/desktop/LoginPageUiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Copyright 2024, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.examples.pingh.desktop | ||
|
||
import androidx.compose.ui.test.ComposeUiTest | ||
import androidx.compose.ui.test.ExperimentalTestApi | ||
import androidx.compose.ui.test.SemanticsNodeInteractionsProvider | ||
import androidx.compose.ui.test.assertIsEnabled | ||
import androidx.compose.ui.test.assertIsNotEnabled | ||
import androidx.compose.ui.test.onNodeWithTag | ||
import androidx.compose.ui.test.performClick | ||
import androidx.compose.ui.test.performTextClearance | ||
import androidx.compose.ui.test.performTextInput | ||
import androidx.compose.ui.test.runComposeUiTest | ||
import io.spine.examples.pingh.desktop.given.DelayedFactAssertion.Companion.awaitFact | ||
import io.spine.examples.pingh.desktop.given.delay | ||
import kotlin.test.Test | ||
import kotlin.time.Duration.Companion.seconds | ||
import org.junit.jupiter.api.DisplayName | ||
|
||
@DisplayName("Login page should") | ||
@OptIn(ExperimentalTestApi::class) | ||
internal class LoginPageUiTest : UiTest() { | ||
|
||
private val username = "MykytaPimonovTD" | ||
|
||
private val SemanticsNodeInteractionsProvider.loginButton | ||
get() = onNodeWithTag("login-button") | ||
|
||
private val SemanticsNodeInteractionsProvider.usernameInput | ||
get() = onNodeWithTag("username-input") | ||
|
||
private val SemanticsNodeInteractionsProvider.submitButton | ||
get() = onNodeWithTag("submit-button") | ||
|
||
private val SemanticsNodeInteractionsProvider.noResponseMessage | ||
get() = onNodeWithTag("no-response-message") | ||
|
||
@Test | ||
internal fun `have login button enabled only when a valid username is entered`() = | ||
runComposeUiTest { | ||
runApp() | ||
loginButton.assertIsNotEnabled() | ||
usernameInput.performTextInput("()+$") | ||
awaitFact { loginButton.assertIsNotEnabled() } | ||
usernameInput.performTextClearance() | ||
usernameInput.performTextInput(username) | ||
awaitFact { loginButton.assertIsEnabled() } | ||
} | ||
|
||
@Test | ||
internal fun `have submit button disabled after it is clicked, if no code has been entered`() = | ||
runComposeUiTest { | ||
runApp() | ||
toVerificationPage() | ||
submitButton.assertIsEnabled() | ||
noResponseMessage.assertDoesNotExist() | ||
submitButton.performClick() | ||
awaitFact { | ||
submitButton.assertIsNotEnabled() | ||
noResponseMessage.assertExists() | ||
} | ||
} | ||
|
||
@Test | ||
internal fun `have submit button become available again 5 seconds after unsuccessful click`() = | ||
runComposeUiTest { | ||
runApp() | ||
toVerificationPage() | ||
submitButton.performClick() | ||
delay(5.seconds) | ||
awaitFact { | ||
submitButton.assertIsEnabled() | ||
noResponseMessage.assertDoesNotExist() | ||
} | ||
} | ||
|
||
private fun ComposeUiTest.toVerificationPage() { | ||
usernameInput.performTextInput(username) | ||
awaitFact { loginButton.assertIsEnabled() } | ||
loginButton.performClick() | ||
awaitFact { | ||
loginButton.assertDoesNotExist() | ||
submitButton.assertExists() | ||
} | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
desktop/src/test/kotlin/io/spine/examples/pingh/desktop/UiTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024, TeamDev. All rights reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Redistribution and use in source and/or binary forms, with or without | ||
* modification, must retain the above copyright notice and the following | ||
* disclaimer. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
package io.spine.examples.pingh.desktop | ||
|
||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.test.ComposeUiTest | ||
import androidx.compose.ui.test.ExperimentalTestApi | ||
import io.spine.examples.pingh.testing.client.IntegrationTest | ||
import org.junit.jupiter.api.AfterEach | ||
|
||
/** | ||
* Abstract base for UI tests that require a server to run and | ||
* a client application connected to it. | ||
*/ | ||
internal abstract class UiTest : IntegrationTest() { | ||
|
||
private var state: AppState? = null | ||
|
||
@AfterEach | ||
internal fun shutdownChannel() { | ||
state?.app?.close() | ||
} | ||
|
||
/** | ||
* Launches the Pingh application for testing and sets application state for this test case. | ||
*/ | ||
@OptIn(ExperimentalTestApi::class) | ||
protected fun ComposeUiTest.runApp() { | ||
setContent { | ||
Theme { | ||
val settings = retrieveSystemSettings() | ||
state = remember { AppState(settings) } | ||
Window(state!!.window, state!!.app) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.