Skip to content

Commit

Permalink
Some docs
Browse files Browse the repository at this point in the history
Some null checks
  • Loading branch information
QAutomatron committed Jul 4, 2018
1 parent b143926 commit ce4b504
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/main/kotlin/com/qautomatron/kaper/core/data/Direction.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.qautomatron.kaper.core.data

/**
* Swipe directions
*/
enum class Direction {
UP, DOWN, LEFT, RIGHT
}
21 changes: 21 additions & 0 deletions src/main/kotlin/com/qautomatron/kaper/core/driver/DriverManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import org.openqa.selenium.remote.RemoteWebDriver
import java.net.URL
import java.util.concurrent.ConcurrentHashMap

/**
* Driver manager - will create, remove driver
*/
class DriverManager {

private val container: MutableMap<Long, WebDriver> = ConcurrentHashMap(4)
Expand All @@ -47,11 +50,17 @@ class DriverManager {
return setDriver(newDriver)
}

/**
* Quit current driver and remove it from thread
*/
fun quitDriver() {
container[Thread.currentThread().id]?.quit()
container.remove(Thread.currentThread().id)
}

/**
* Check if driver is exist
*/
fun isDriverExist(): Boolean {
return container[Thread.currentThread().id] != null
}
Expand Down Expand Up @@ -159,19 +168,31 @@ class DriverManager {
val driverManager = DriverManager()
private val logger = KotlinLogging.logger {}

/**
* Get driver instance within project
*/
fun getDriver(): WebDriver {
return driverManager.getDriver()
}

/**
* Close session within project
*/
fun closeSession() {
logger.debug { "Will close session for thread ${Thread.currentThread().id}" }
return driverManager.quitDriver()
}

/**
* Driver exist check alias
*/
fun isDriverExist(): Boolean {
return driverManager.isDriverExist()
}

/**
* Get screenshot as bytes
*/
fun getScreenshot(): ByteArray {
return (getDriver() as RemoteWebDriver).getScreenshotAs(OutputType.BYTES)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.ExpectedCondition

/**
* Will check absence of element By locator
*/
class AbsenceOfElementLocated(private val locator: By) : ExpectedCondition<Boolean> {

override fun apply(driver: WebDriver?): Boolean? {
return try {
val elements = driver!!.findElements<WebElement>(locator)
elements.isEmpty()
val elements = driver?.findElements<WebElement>(locator)
elements?.isEmpty() ?: false
} catch (t: Throwable) {
throw Error(t)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.qautomatron.kaper.core.visibility

/**
* Element condition list
*/
enum class Condition {
VISIBLE, INVISIBLE, PRESENCE, ABSENCE
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.qautomatron.kaper.core.visibility
import org.openqa.selenium.*
import org.openqa.selenium.support.ui.ExpectedCondition

/**
* Will check invisibility of web element
*/
class InvisibilityOfElement(private val element: WebElement) : ExpectedCondition<Boolean> {

override fun apply(d: WebDriver?): Boolean? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.qautomatron.kaper.core.visibility
import org.openqa.selenium.*
import org.openqa.selenium.support.ui.ExpectedCondition

/**
* Will check invisibility of element By locator
*/
class InvisibilityOfElementLocated(private val locator: By) : ExpectedCondition<Boolean> {

override fun apply(driver: WebDriver?): Boolean? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import org.openqa.selenium.support.ui.ExpectedCondition

/**
* Will check presence of element By locator
*/
class PresenceOfElementLocated(private val locator: By) : ExpectedCondition<Boolean> {

override fun apply(driver: WebDriver?): Boolean? {
return try {
val elements = driver!!.findElements<WebElement>(locator)
!elements.isEmpty()
val elements = driver?.findElements<WebElement>(locator)
if (elements != null) {
!elements.isEmpty()
} else {
false
}
} catch (t: Throwable) {
throw Error(t)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package com.qautomatron.kaper.core.visibility
import org.openqa.selenium.*
import org.openqa.selenium.support.ui.ExpectedCondition

/**
* Will check element visibility by web element
*/
class VisibilityOfElement(private val element: WebElement) : ExpectedCondition<Boolean> {

override fun apply(d: WebDriver?): Boolean? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ package com.qautomatron.kaper.core.visibility
import org.openqa.selenium.*
import org.openqa.selenium.support.ui.ExpectedCondition

/**
* Will check element visibility By locator
*/
class VisibilityOfElementLocated(private val locator: By) : ExpectedCondition<Boolean> {

override fun apply(driver: WebDriver?): Boolean? {
return try {
val elements = driver!!.findElements<WebElement>(locator)
!elements.isEmpty() && elements[0].isDisplayed
val elements = driver?.findElements<WebElement>(locator)
if (elements != null) {
!elements.isEmpty() && elements[0].isDisplayed
} else {
false
}
} catch (e: StaleElementReferenceException) {
false
} catch (e: NoSuchElementException) {
false
} catch (e: ElementNotVisibleException) {
false
} catch (e: NullPointerException) {
false
} catch (t: Throwable) {
throw Error(t)
}
Expand Down
20 changes: 19 additions & 1 deletion src/main/kotlin/com/qautomatron/kaper/steps/AlertsSteps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@ package com.qautomatron.kaper.steps
import com.qautomatron.kaper.core.driver.getDriver
import org.openqa.selenium.NoAlertPresentException

/**
* Steps for handle alerts
*/
class AlertsSteps: Steps() {

/**
* Switch to alert and accept it
*/
fun acceptAlert() = driver.switchTo().alert().accept()

/**
* Switch to alert and dismiss it
*/
fun dismissAlert() = driver.switchTo().alert().dismiss()

/**
* Check alert present
*/
fun isAlertPresent(): Boolean {
return try {
driver.switchTo().alert()
Expand All @@ -18,6 +30,10 @@ class AlertsSteps: Steps() {
}
}

/**
* Check alert with timeout
* @param seconds timeout
*/
fun isAlertPresent(seconds: Int): Boolean {
for (i in 1..seconds) {
try {
Expand All @@ -30,6 +46,8 @@ class AlertsSteps: Steps() {
return false
}

/**
* Get alert text
*/
fun getAlertText(): String = driver.switchTo().alert().text

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@ package com.qautomatron.kaper.steps

import com.qautomatron.kaper.core.data.Direction

/**
* Steps for IOS screen interactions
*/
class IOSScreenSteps: Steps() {

/**
* Swipe to direction
* @param direction direction of swipe
*/
fun swipe(direction: Direction) {
val params = hashMapOf(
"direction" to direction.name.toLowerCase())
js.executeScript("mobile: swipe", params)
js?.executeScript("mobile: swipe", params)
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/com/qautomatron/kaper/steps/KeyboardSteps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ package com.qautomatron.kaper.steps

import io.appium.java_client.AppiumDriver

/**
* Keyboard interaction steps
*/
class KeyboardSteps: Steps() {

fun typeOnKeyboard(s: String) = (driver as AppiumDriver<*>).keyboard.sendKeys(s)
/**
* Type text on keyboard
*/
fun typeOnKeyboard(s: String) = (driver as? AppiumDriver<*>)?.keyboard?.sendKeys(s)

/**
* Hide keyboard and catch exception if it's already present on screen
*/
fun hideKeyboard() {
try {
(driver as AppiumDriver<*>).hideKeyboard()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/qautomatron/kaper/steps/Steps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import org.openqa.selenium.WebDriver

abstract class Steps(val driver: WebDriver = getDriver()) {

protected val js = driver as JavascriptExecutor
protected val js = driver as? JavascriptExecutor

init {
driver.resetImplicitTimeout()
Expand Down

0 comments on commit ce4b504

Please sign in to comment.