com.github.mvysny.kaributesting.v10.Notifications.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of karibu-testing-v10 Show documentation
Show all versions of karibu-testing-v10 Show documentation
Karibu Testing, support for browserless Vaadin testing in Kotlin
package com.github.mvysny.kaributesting.v10
import com.vaadin.flow.component.UI
import com.vaadin.flow.component.notification.Notification
import com.vaadin.flow.dom.Element
import kotlin.streams.toList
/**
* Returns the list of currently displayed notifications.
*/
fun getNotifications(): List {
// notifications are opened lazily. Make sure they are attached to the UI
testingLifecycleHook.awaitBeforeLookup()
val notifications = UI.getCurrent().children.toList().filterIsInstance()
testingLifecycleHook.awaitAfterLookup()
return notifications
}
/**
* Returns the notification text.
*/
fun Notification.getText(): String {
val f = Notification::class.java.getDeclaredField("templateElement").apply { isAccessible = true }
val e = f.get(this) as Element
return e.getProperty("innerHTML") ?: ""
}
/**
* Expects that given list of notifications is displayed. Also clears the notifications.
*/
fun expectNotifications(vararg texts: String) {
val notifications: List = getNotifications()
expectList(*texts) { notifications.map { it.getText() } }
clearNotifications()
}
/**
* Clears and removes all notifications from screen.
*/
fun clearNotifications() {
getNotifications().forEach { it.removeFromParent() }
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy