without marketing

Senior Test Automation Engineer

Sergey Pirogov

Speaker info

http://automation-remarks.com

fb.com/automationremarks

Backgroung

Kotlin

Gradle Meets Kotlin

Back at JavaOne 2015, during a lunch break we started chatting with Hans Dockter, CEO of Gradle. A couple of days after the conference, a few of us were at the Gradle offices talking about what would be the beginning of the collaboration between JetBrains and Gradle; to bring first-class tooling and support for a static language to Gradle.

Google Meets Kotlin

Google today announced that it is making Kotlin, a statically typed programming language for the Java Virtual Machine, a first-class language for writing Android apps.

Hype, hype, hype

Gartner curve

Kotlin vs Java

Kotlin features lost in Java

Null safety

var a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // ok

Extension functions

fun WebDriver.open(url: String) {
    get(url)
}

fun WebDriver.all(cssselector: String): MutableList<WebElement>? {
    return findElements(By.cssSelector(cssselector))
}

fun List<WebElement>.shouldHave(size: Int) {
    assert(this.size == size)
}
val driver = ChromeDriver()
driver.open("http://automation-remarks.com")
driver.all(".post").shouldHave(size = 9)
ChromeDriver().apply {
        open("http://automation-remarks.com")
        all(".post").shouldHave(size = 9)
}

Data class and property

data class User(val name: String, 
                val email: String, 
                val password: String)

val admin = User("Dima", "dima@ukr.net", "admin123")






driver.element("#password").value = admin.password

property access

String template

fun findCustomerById(id:Long):CustomerEntity{

    val query = """
                SELECT *
                FROM customer
                WHERE id = $id;
                """

    return findOne(CustomerEntity::klass, query)
}

Reified type

val beanHandler = BeanHandler<City>(City::class.java)
val city: City  = QueryRunner().query("SELECT * FROM city", beanHandler)
inline fun <reified T> QueryRunner.findOne(sql: String): T {
    return BeanHandler(T::class.java).run { query(sql, this) }
}
val city : City = QueryRunner().findOne("SELECT * FROM city")

Java Interoperability  

Kotlin is designed with Java Interoperability in mind. Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used from Java rather smoothly as well.

Kotlin vs Rest Assured

class GarageRestTest {

    @Test
    fun basicPingTest() {
        given()
            .`when`()
            .get("/garage")
            .then().statusCode(200);
    }
}

when is a keyword in Kotlin

Kotlin vs Selenide

 @Test fun usingDollarsWithBackticks() {
        `$`(By.name("q")).val("selenide")
        `$$`("#ires .g").shouldHave(size(10))
        `$`("#ires .g").shouldHave(text("Kotlin"));
 }

$ is a keyword in Kotlin

val is a keyword in Kotlin

Kotlin vs Assertion libraries

class testExample{
  @Test fun example(){
    assertThat(1, `is`(equalTo(1))
}

is a keyword in Kotlin

Maybe we need back to Java

Kotlin vs Rest Assured fixed

@Test
fun basicPingTest() {
   given()
      .When()
      .get("/garage")
      .then().statusCode(200);
}

fun RequestSpecification.When(): RequestSpecification {
        return this.`when`()
}

Kotlin vs Selenide fixed

 @Test fun usingDollarsWithBackticks() {
        get(By.name("q")).setValue("selenide")
        all("#ires .g").shouldHave(size(10))
        get("#ires .g").shouldHave(text("Kotlin"));
 }
 fun get(selector: String) : SelenideElement {
     return `$`(selector);
 }
    
 fun all(selector: String) : ElementsCollection {
     return `$$`(selector);
 }

Kotlin Assertions fixed

@Test fun example(){
    assertThat(1).isEqualTo(1)
}
assert {
    throw Exception("error")
}.throwsError {
    it.hasMessage("wrong")
}
// -> expected [message] to be:<["wrong"]> 
                         but was:<["error"]>

Kotlin replacements

Rest Assured -> Retrofit

Selenide -> Kirk

Hamcrest -> AssertK

Kirk pragmatic DSL

drive {
    to(::TodoPage) {
      addTasks("A", "B", "C")
      deactivateTask("A")
      counter.should(have.text("2"))
      goToCompletedTab()
      taskList.should(have.exactText("A"))
    }
}

Conclusion

  • Convert existing codebase is a bit painfull
  • Kotlin is a great language
  • Small amount of libraries
  • Potentially bright future

Thank you!

http://automation-remarks.com

fb.com/automationremarks

Kotlin without marketing

By Sergey Pirogov

Kotlin without marketing

  • 382
Loading comments...

More from Sergey Pirogov