Skip to content
This repository has been archived by the owner on May 4, 2022. It is now read-only.

Commit

Permalink
webdriver session example
Browse files Browse the repository at this point in the history
with scenario and implementation to realize scenarios with two independent browser windows
  • Loading branch information
boris779 committed Oct 22, 2019
1 parent f90d242 commit 243a897
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,24 @@ object WebDriverSessionStore {
if (!store.containsKey(sessionName)) {
store[sessionName] = WebDriverSession(sessionName)
}

return store[sessionName]!!
}

fun remove(sessionName: String) {
store.remove(sessionName)?.webDriver?.quit()
}

fun quitAll() {
val keys = store.keys
keys.forEach{
store[it]?.webDriver?.quit()
}
}

fun getIfExists(sessionName: String): WebDriverSession? {
if (store.containsKey(sessionName)) {
return store[sessionName]!!
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ import logger
open class AbstractStepDefs(protected val testDataContainer: TestDataContainer) : En {

private val log by logger()
private lateinit var currentSessionID: String

fun getWebDriverSession(): WebDriverSession {
if (currentSessionID == null) {
testDataContainer.setTestData("session.id", testDataContainer.getTestId())
currentSessionID = testDataContainer.getTestId()
}
return getWebDriverSession(currentSessionID)
}

fun getWebDriverSession(sessionID: String): WebDriverSession {

val webDriverSession = WebDriverSessionStore.get(testDataContainer.getTestId())
val webDriverSession = WebDriverSessionStore.get(sessionID)

if (testDataContainer.needsInitializing()) {
if (webDriverSession.webDriver is RemoteWebDriver) {
Expand All @@ -32,6 +41,8 @@ open class AbstractStepDefs(protected val testDataContainer: TestDataContainer)

testDataContainer.setTestData("initialized", true)
}
currentSessionID = sessionID
testDataContainer.setTestData("session.id", currentSessionID)
return webDriverSession
}

Expand Down
15 changes: 8 additions & 7 deletions src/test/kotlin/at/co/boris/secuton/step_definitions/Hooks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class Hooks(private val testDataContainer: TestDataContainer) {
testDataContainer.setTestData("browser.type", System.getProperty("browser", "no browser set"))
testDataContainer.setTestData("browser.version", System.getProperty("browser.version", "no version set"))
testDataContainer.setTestData("initialized", false)
testDataContainer.setTestData("baseurl", System.getProperty("baseUrl", "no base Url given"))

// to check if it runs on Jenkins or local
val jobname = System.getenv("JOB_NAME")
Expand Down Expand Up @@ -54,17 +55,17 @@ class Hooks(private val testDataContainer: TestDataContainer) {

@After
fun afterScenario(scenario: Scenario) {
val testId = extractTestIdFromScenarioName(scenario.name)
//FIXME wenn die Session nicht existiert, wird hier eine erstellt, das sollte aber nicht gemacht werden!
val webDriverSession = WebDriverSessionStore.get(testId)

val testId = testDataContainer.getTestId()
val webDriverSession = WebDriverSessionStore.getIfExists(testDataContainer.getCurrentSessionId())

if (!scenario.isFailed) {
WebDriverSessionStore.remove(testId)
WebDriverSessionStore.quitAll()
return
}


if (webDriverSession.currentPage != null) {
if (webDriverSession?.currentPage != null) {
try {
val isMobile = (webDriverSession.webDriver as RemoteWebDriver).isMobile()
scenario.write("isMobile active for used webdriver: $isMobile")
Expand All @@ -78,13 +79,13 @@ class Hooks(private val testDataContainer: TestDataContainer) {

if (testDataContainer.isLocalRun()) {
val screenshot = (webDriverSession.webDriver as TakesScreenshot).getScreenshotAs(OutputType.FILE)
FileUtils.copyFile(screenshot, File(System.getProperty("user.dir") + "/target/error_selenium_$testId.png"))
FileUtils.copyFile(screenshot, File(System.getProperty("user.dir") + "/target/error_selenium_${testId}_${testDataContainer.getCurrentSessionId()}.png"))
} else {
scenario.embed((webDriverSession.webDriver as TakesScreenshot).getScreenshotAs(OutputType.BYTES), "image/png")
}

} finally {
WebDriverSessionStore.remove(testId)
WebDriverSessionStore.quitAll()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package at.co.boris.secuton.step_definitions

import assertk.assertThat
import assertk.assertions.endsWith
import assertk.assertions.isEqualTo
import at.co.boris.secuton.pageobjects.MainPage
import at.co.boris.secuton.pageobjects.PageUrls
import at.co.boris.secuton.pageobjects.TeamPage

class SessionSteps(testDataContainer: TestDataContainer) : AbstractStepDefs(testDataContainer) {


init {
When("the start page for {string} is loaded") { sessionID: String ->
getWebDriverSession(sessionID).gotoUrl(PageUrls.HOME, MainPage::class, testDataContainer)
}

When("{string} is activated") { sessionID: String ->
getWebDriverSession(sessionID)
}
When("{string} opens the team site") { sessionID: String ->
getWebDriverSession(sessionID).gotoUrl(PageUrls.TEAM,TeamPage::class, testDataContainer)
}
Then("{string} should be still on start page") { sessionID: String ->
assertThat(getWebDriverSession(sessionID).webDriver.currentUrl).isEqualTo((testDataContainer.getTestData("baseurl") as String) + "/")
}

Then("the active session should contain {int} window") { expCount: Int ->
assertThat(getWebDriverSession().webDriver.windowHandles.size).isEqualTo(expCount)
}

And("{string} should be still on team site") { sessionID: String ->
assertThat(getWebDriverSession(sessionID).webDriver.currentUrl).endsWith(PageUrls.TEAM.subUrl + "/")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package at.co.boris.secuton.step_definitions

import io.cucumber.core.api.Scenario
import java.util.*


class TestDataContainer {
Expand All @@ -23,6 +24,17 @@ class TestDataContainer {
testDataMap[key] = value
}

fun getTestData(key: String): Any? {
if (testDataMap.containsKey(key)) {
return testDataMap[key]!!
}
return null
}

fun getCurrentSessionId(): String {
return testDataMap["session.id"] as String
}

private fun getBrowser(): String {
return testDataMap["browser"] as String
}
Expand Down
14 changes: 10 additions & 4 deletions src/test/resources/features/peso.feature
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
@boris
Feature: [peso] Example Feature

Background:
Scenario: [PES-01 [peso]
Given the start page is loaded

Scenario: [HWD-01 [peso]
Then the peso logo should be displayed

Scenario: [HWD-02 [peso]
Scenario: [PES-02 [peso]
Given the start page is loaded
When the team site is opened
Then should "Boris Wrubel" be part of the Core team

Scenario: [PES-03 [peso]
Given the start page for "User_1" is loaded
And the start page for "User_2" is loaded
Then the active session should contain 1 window
When "User_2" opens the team site
Then "User_1" should be still on start page
And "User_2" should be still on team site

0 comments on commit 243a897

Please sign in to comment.