1. Web UI testing in Scala
Radim.pavlicek@gmail.com
@radimpavlicek
2. Why should I care?
●
●
●
●
●
regression errors
cross web browser (Firefox/IE/Chrome)
cross OS (Mac/Linux/Windows)
cross versions (browsers)
use cases documentation
12. Input parameters
object Settings {
val url = System.getProperty ("onlineUsers.url" )
val login = System.getProperty ("onlineUsers.login" )
val password = System.getProperty ("onlineUsers.password" )
val port = {
val tmp = System.getProperty ("onlineUsers.port" )
if (null == tmp)
new Integer(9090)
else new Integer(tmp)
}
}
14. WebDriver cont.
trait WebDriverAccess {
implicit val driver = WebDriverAccess.defaultDriver
}
implicit = if a parameter value is not supplied
then the compiler will search for an "implicit"
value defined within scope
15. Page objects pattern
●
●
●
●
model UI as services
not exposing internals
model just what is tested
public service methods return PageObjects
16. Wait
protected def waitUntil(f: => Boolean): Unit = {
new WebDriverWait(driver, 25, 50) until
(new ExpectedCondition[Boolean] {
def apply(input: WebDriver) = f
})
}
WebDriverWait(WebDriver driver, Clock clock, Sleeper sleeper, long timeOutInSeconds, long
sleepTimeOut)
waitUntil {
!driver.findElements(By.id(“ mainForm:isLoggedIn ”)).isEmpty }
17. PageObject implementation
abstract class Page[T <: Page[T]]( expectedId: String, val driver:
WebDriver) {
self: T =>
waitUntil {
!driver.findElements(By.id(expectedId)).isEmpty }
private val tmpId: By = By.id(expectedId)
try { driver.findElement(tmpId)
} catch {
case nsee: NoSuchElementException =>
Assert.fail( "ID not found:" +expectedId)
}
}
19. Use case
class UserManagementWebTest extends WebDriverAccess {
var homePage: HomePage = _
@Test
def shouldLandOnUserManagement {
val loginPage: LoginPage = LoginPage.open(
server = Settings.url, port = Settings.
port)
homePage = loginPage.login()
val mngPage: UserManagementPage = homePage.goToUserManagementPage()
assertThat(mngPage.text, containsString( "Create new User" ))
}