Best Kotest code snippet using io.kotest.core.spec.style.scopes.BehaviorSpecRootScope.given
Test2Clients.kt
Source:Test2Clients.kt
...12import kotlinx.coroutines.delay13import org.openqa.selenium.Dimension14import org.openqa.selenium.Keys15import org.openqa.selenium.Point16private var givenSignalingServerNextId = 017fun BehaviorSpecRootScope.givenSignalingServer(test: suspend GivenScope.(server: ApplicationEngine) -> Unit) {18 given("signaling server (${givenSignalingServerNextId++})") {19 val signalServer = embeddedServer(Netty, port = port) { module(testing = true) }20 signalServer.start()21 test(signalServer) // signaling server can be stopped there22 signalServer.stop() // if it wasn't stopped, stop it here23 }24}25enum class Tab(val posX: Int, val posY: Int) {26 FIRST(100, 50),27 SECOND(1000, 50),28}29private val tabs = mutableMapOf<Tab, SelenideDriver>()30suspend fun GivenScope.andClientTab(tab: Tab, test: suspend GivenScope.(LoginPage) -> Unit) {31 and("client tab ($tab)") {32 val driver = tabs.getOrPut(tab) {33 createDriver().also {34 it.open("about:blank") // open before changing window to avoid exceptions35 it.webDriver.manage().window().apply {36 position = Point(tab.posX, tab.posY)37 size = Dimension(400, 400)38 }39 }40 }41 val client = openClient(driver)42 test(client)43 driver.open("about:blank")44 }45}46class Test2Clients : BehaviorSpec({47 // test logging in with the same name48 givenSignalingServer {49 andClientTab(Tab.FIRST) { loginPage1 ->50 andClientTab(Tab.SECOND) { loginPage2 ->51 `when`("I login to the first tab") {52 val connectionPage1 = loginPage1.loginAs("user1")53 and("I login to the second tab with the same name") {54 loginPage2.loginAs(connectionPage1.userName)55 then("error should be shown") {56 loginPage2.driver.switchTo().alert().accept()57 }58 }59 }60 }61 }62 }63 // test text sending64 givenSignalingServer {65 andClientTab(Tab.FIRST) { loginPage1 ->66 andClientTab(Tab.SECOND) { loginPage2 ->67 `when`("I login to the first tab") {68 val connectionPage1 = loginPage1.loginAs("user1")69 and("I login to the second tab") {70 val connectionPage2 = loginPage2.loginAs("user2")71 and("I call the first tab from the second tab") {72 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)73 val collaborationPage1 = connectionPage1 shouldBeConnectedTo collaborationPage2.userName74 and("I input text from the second tab") {75 val text1 = "my message, ${System.currentTimeMillis()} ms"76 collaborationPage2.input(text1)77 then("chat in the second tab should contain text") {78 collaborationPage2.text.shouldHave(exactValue(text1))79 }80 then("chat in the first tab should contain text") {81 collaborationPage1.text.shouldHave(exactValue("$text1|"))82 }83 }84 }85 }86 }87 }88 }89 }90 // test text sending with stopped signaling server91 givenSignalingServer { server ->92 andClientTab(Tab.FIRST) { loginPage1 ->93 andClientTab(Tab.SECOND) { loginPage2 ->94 `when`("I login to the first tab") {95 val connectionPage1 = loginPage1.loginAs("user1")96 and("I login to the second tab") {97 val connectionPage2 = loginPage2.loginAs("user2")98 and("I call the first tab from the second tab") {99 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)100 val collaborationPage1 = connectionPage1 shouldBeConnectedTo collaborationPage2.userName101 and("The signaling server stops") {102 server.stop()103 delay(1500)104 and("I input text from the second tab") {105 val text1 = "my message, ${System.currentTimeMillis()} ms"106 collaborationPage2.input(text1)107 then("chat in the second tab should contain text") {108 collaborationPage2.text.shouldHave(exactValue(text1))109 }110 then("chat in the first tab should contain text") {111 collaborationPage1.text.shouldHave(exactValue("$text1|"))112 }113 }114 }115 }116 }117 }118 }119 }120 }121 // test text addition from both tabs122 givenSignalingServer {123 andClientTab(Tab.FIRST) { loginPage1 ->124 andClientTab(Tab.SECOND) { loginPage2 ->125 `when`("I login to the first tab") {126 val connectionPage1 = loginPage1.loginAs("user1")127 and("I login to the second tab") {128 val connectionPage2 = loginPage2.loginAs("user2")129 and("I call the first tab from the second tab") {130 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)131 val collaborationPage1 = connectionPage1 shouldBeConnectedTo collaborationPage2.userName132 and("I input text from the second tab") {133 val text1 = "my message, ${System.currentTimeMillis()} ms"134 collaborationPage2.input(text1)135 and("I add text from the first tab") {136 val text2 = "\nmy message 2, ${System.currentTimeMillis()} ms"137 collaborationPage1.input(text2)138 then("chat in the second tab should contain old text and appended text") {139 collaborationPage2.text.shouldHave(exactValue("$text1$text2|"))140 }141 then("chat in the first tab should contain old text and appended text") {142 collaborationPage1.text.shouldHave(exactValue("$text1|$text2"))143 }144 }145 }146 }147 }148 }149 }150 }151 }152 // test reconnection153 givenSignalingServer {154 andClientTab(Tab.FIRST) { loginPage1 ->155 andClientTab(Tab.SECOND) { loginPage2 ->156 `when`("I login to the first tab") {157 val connectionPage1 = loginPage1.loginAs("user1")158 and("I login to the second tab") {159 val connectionPage2 = loginPage2.loginAs("user2")160 and("I call the first tab from the second tab") {161 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)162 and("I disconnect on the second tab") {163 collaborationPage2.disconnect()164 and("I call the first tab from the second tab") {165 @Suppress("NAME_SHADOWING")166 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)167 val collaborationPage1 =168 connectionPage1 shouldBeConnectedTo collaborationPage2.userName169 and("I input text from the second tab") {170 val text1 = "my message, ${System.currentTimeMillis()} ms"171 collaborationPage2.input(text1)172 then("chat in the second tab should contain text") {173 collaborationPage2.text.shouldHave(exactValue(text1))174 }175 then("chat in the first tab should contain text") {176 collaborationPage1.text.shouldHave(exactValue("$text1|"))177 }178 }179 }180 }181 }182 }183 }184 }185 }186 }187 // test text cursor moving on one side seen from other side188 givenSignalingServer {189 andClientTab(Tab.FIRST) { loginPage1 ->190 andClientTab(Tab.SECOND) { loginPage2 ->191 `when`("I login to the first tab") {192 val connectionPage1 = loginPage1.loginAs("user1")193 and("I login to the second tab") {194 val connectionPage2 = loginPage2.loginAs("user2")195 and("I call the first tab from the second tab") {196 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)197 val collaborationPage1 = connectionPage1 shouldBeConnectedTo collaborationPage2.userName198 and("I input text from the second tab") {199 val text1 = "my message, ${System.currentTimeMillis()} ms"200 collaborationPage2.input(text1)201 and("I place cursor to the start of textarea") {202 collaborationPage2.input(Keys.PAGE_UP)203 then("chat in the second tab should contain correct text") {204 collaborationPage2.text.shouldHave(exactValue(text1))205 }206 then("chat in the first tab should contain correct text") {207 collaborationPage1.text.shouldHave(exactValue("|$text1"))208 }209 }210 }211 }212 }213 }214 }215 }216 }217 // test text addition from both tabs with cursor sync218 givenSignalingServer {219 andClientTab(Tab.FIRST) { loginPage1 ->220 andClientTab(Tab.SECOND) { loginPage2 ->221 `when`("I login to the first tab") {222 val connectionPage1 = loginPage1.loginAs("user1")223 and("I login to the second tab") {224 val connectionPage2 = loginPage2.loginAs("user2")225 and("I call the first tab from the second tab") {226 val collaborationPage2 = connectionPage2.connect(connectionPage1.userName)227 val collaborationPage1 = connectionPage1 shouldBeConnectedTo collaborationPage2.userName228 and("I input text from the second tab") {229 val text1 = "my message, ${System.currentTimeMillis()} ms"230 collaborationPage2.input(text1)231 and("I place cursor to the start of textarea") {232 collaborationPage2.input(Keys.PAGE_UP)...
BehaviorSpecRootScope.kt
Source:BehaviorSpecRootScope.kt
...4typealias BehaviorSpecRootContext = BehaviorSpecRootScope5/**6 * A context that allows tests to be registered using the syntax:7 *8 * given("some test")9 * xgiven("some disabled test")10 */11@Suppress("FunctionName")12interface BehaviorSpecRootScope : RootScope {13 /**14 * Adds a top level [BehaviorSpecGivenContainerScope] to this spec.15 */16 fun Given(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, false, test)17 /**18 * Adds a top level [BehaviorSpecGivenContainerScope] to this spec.19 */20 fun given(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, false, test)21 /**22 * Adds a top level disabled [BehaviorSpecGivenContainerScope] to this spec.23 */24 fun xgiven(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, true, test)25 /**26 * Adds a top level disabled [BehaviorSpecGivenContainerScope] to this spec.27 */28 fun xGiven(name: String, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) = addGiven(name, true, test)29 private fun addGiven(name: String, xdisabled: Boolean, test: suspend BehaviorSpecGivenContainerScope.() -> Unit) {30 addContainer(31 TestName("Given: ", name, true),32 disabled = xdisabled,33 null34 ) { BehaviorSpecGivenContainerScope(this).test() }35 }36}...
behaviorSpec.kt
Source:behaviorSpec.kt
...4import io.kotest.core.factory.build5import io.kotest.core.spec.DslDrivenSpec6import io.kotest.core.spec.style.scopes.BehaviorSpecRootScope7/**8 * Creates a [TestFactory] from the given block.9 *10 * The receiver of the block is a [BehaviorSpecTestFactoryConfiguration] which allows tests11 * to be defined using the 'behavior-spec' style.12 */13fun behaviorSpec(block: BehaviorSpecTestFactoryConfiguration.() -> Unit): TestFactory {14 val config = BehaviorSpecTestFactoryConfiguration()15 config.block()16 return config.build()17}18class BehaviorSpecTestFactoryConfiguration : TestFactoryConfiguration(), BehaviorSpecRootScope19abstract class BehaviorSpec(body: BehaviorSpec.() -> Unit = {}) : DslDrivenSpec(), BehaviorSpecRootScope {20 init {21 body()22 }...
given
Using AI Code Generation
1class BehaviorSpecExample : BehaviorSpec({2given("given") {3and("and") {4}5and("and") {6}7and("and") {8}9and("and") {10}11and("and") {12}13and("and") {14}15and("and") {16}17and("and") {18}
given
Using AI Code Generation
1class BehaviorSpecExample : BehaviorSpec({2Given("a calculator") {3When("I add two numbers") {4Then("the result should be correct") {5And("I should be happy") {6}7}8}9})10import io.kotest.core.spec.style.BehaviorSpec11import io.kotest.matchers.shouldBe12class BehaviorSpecExample : BehaviorSpec({13Given("a calculator") {14When("I add two numbers") {15Then("the result should be correct") {16}17And("I should be happy") {18}19}20}21})22import io.kotest.core.spec.style.BehaviorSpec23import io.kotest.matchers.shouldBe24class BehaviorSpecExample : BehaviorSpec() {25init {26Given("a calculator") {27When("I add two numbers") {28Then("the result should be correct") {29}30And("I should be happy") {31}32}33}34}35}36import io.kotest.core.spec.style.BehaviorSpec37import io.kotest.matchers.shouldBe38import io.kotest.core.spec.style.TestContext39class BehaviorSpecExample : BehaviorSpec() {40override fun context(): TestContext {41return TestContext("a calculator")42}43override fun context(): TestContext {44return TestContext("I add two numbers")45}46override fun context(): TestContext {47return TestContext("the result should be correct")48}49override fun context(): TestContext {50return TestContext("I should be happy")51}52}53}54import io.kot
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!