9. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm – OOP & functional
1. What is Kotlin? _9
10. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm
• Pragmatic
1. What is Kotlin? _10
11. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm
• Pragmatic
• Democratic
1. What is Kotlin? _11
12. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm
• Pragmatic
• Democratic
• Safe
1. What is Kotlin? _12
13. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm
• Pragmatic
• Democratic
• Safe
• Concise
1. What is Kotlin? _13
Brevity is the sister of talent.
- Anton Chekhov -
14. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm
• Pragmatic
• Democratic
• Safe
• Concise
• Interoperable
1. What is Kotlin? _14
15. Kotlin is...
—
• JVM language
• Statically typed
• Multi-paradigm
• Pragmatic
• Democratic
• Safe
• Concise
• Interoperable
1. What is Kotlin? _15
16. Kotlin 1.0
—
• 15 Feb 2016 – backward compatibility
1. What is Kotlin? _16
17. Kotlin 1.0
—
• 15 Feb 2016
• kotlinc, Intellij IDEA (plugin), Eclipse (plugin), NetBeans (plugin)
1. What is Kotlin? _17
18. Kotlin 1.0
—
• 15 Feb 2016
• kotlinc, Intellij IDEA (plugin), Eclipse (plugin), NetBeans (plugin)
• Open source https://github.com/jetbrains/kotlin
1. What is Kotlin? _18
23. Where Kotlin is used
—
• JetBrains products
1. What is Kotlin? _23
24. Where Kotlin is used
—
• JetBrains products
• Android
1. What is Kotlin? _24
25. Where Kotlin is used
—
• JetBrains products
• Android
• Server & Desktop
1. What is Kotlin? _25
26. Where Kotlin is used
—
• JetBrains products
• Android
• Server & Desktop
• Gradle 3.0 https://github.com/gradle/gradle-script-kotlin
1. What is Kotlin? _26
27. Where Kotlin is used
—
• JetBrains products
• Android
• Server & Desktop
• Gradle 3.0
• (Web) Test Automation
1. What is Kotlin? _27
29. Test Automation: why Kotlin?
—
• Syntax similar to Java
• Clean test code
Conciseness
Easy to read == easy to understand
Collective code ownership, effective code review
1. What is Kotlin? _29
30. Test Automation: why Kotlin?
—
• Syntax similar to Java
• Clean test code
Conciseness
Easy to read == easy to understand
Collective code ownership, effective code review
• Safe test code
1. What is Kotlin? _30
31. What’s going on here
—
• What is Kotlin?
• Problems Kotlin solves
• Demo
Excuse me, sir, do you have a moment to talk about tests in Kotlin? _31
32. Kotlin features
—
• Extension functions
• Data classes
• Lambda functions
• Smart cast
• Type inference
• Null safety
2. Problems Kotlin solves _32
33. 1. Problem: Maps and Lists in Java
—
// Java
Map<Integer, Credentials> users = new HashMap<>();
users.put(1, new Credentials("vasya", "123456"));
users.put(2, new Credentials("johny", "qwerty"));
users.put(3, new Credentials("admin", "admin"));
List<Integer> responseCodes = new ArrayList<>();
responseCodes.add(200);
responseCodes.add(302);
2. Problems Kotlin solves _33
34. 1. Solution: Kotlin collections
—
// Kotlin
val users = mapOf(
1 to Credentials("vasya", "123456"),
2 to Credentials("johny", "qwerty"),
3 to Credentials("admin", "admin"))
val responseCodes = listOf(200, 302)
2. Problems Kotlin solves _34
35. 1. Collections: Map
—
// Kotlin
val users = mapOf(
1 to Credentials("vasya", "123456"),
2 to Credentials("johny", "qwerty"),
3 to Credentials("admin", "admin"))
Map<Integer, Credentials>
new
users.put(4, Credentials("...", "..."))
2. Problems Kotlin solves _35
36. 1. Collections: Traversing a map
—
// Java
for (Map.Entry<Integer, Credentials> pair : users.entrySet()) {
System.out.println(pair.getKey() + "->" + pair.getValue());
}
// Kotlin
for ((id, creds) in users) {
println("$id->$creds")
}
2. Problems Kotlin solves _36
40. 2. Problem: framework can’t do what you need it to do
—
// Java
public static List<WebElement> findElements(WebDriver driver,
String selector) {
return driver.findElements(By.cssSelector(selector));
}
findElements(driver, "a");
2. Problems Kotlin solves _40
41. 2. Problem: framework can’t do what you need it to do
—
2. Problems Kotlin solves _41
47. 3. Problem: small classes are not small
—
// Java
public class Credentials {
private final String username;
private final String password;
}
2. Problems Kotlin solves _47
48. 3. Problem: small classes are not small
—
// Java
public class Credentials {
private final String username;
private final String password;
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
}
2. Problems Kotlin solves _48
49. 3. Problem: small classes are not small
—
// Java
public class Credentials {
private final String username;
private final String password;
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
}
2. Problems Kotlin solves _49
50. 3. Problem: small classes are not small
—// Java
public class Credentials {
private final String username;
private final String password;
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
@Override
public String toString() {
return username + '/' + password;
}
}
2. Problems Kotlin solves _50
51. 3. Problem: small classes are not small
—// Java
public class Credentials {
private final String username;
private final String password;
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
@Override
public String toString() {
return username + '/' + password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Credentials that = (Credentials) o;
if (username != null ? !username.equals(that.username) : that.username != null) return false;
return password != null ? password.equals(that.password) : that.password == null;
}
@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
return 31 * result + (password != null ? password.hashCode() : 0);
}
}
2. Problems Kotlin solves _51
52. 3. Problem: 27 lines
—// Java
public class Credentials {
private final String username;
private final String password;
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
@Override
public String toString() {
return username + '/' + password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Credentials that = (Credentials) o;
if (username != null ? !username.equals(that.username) : that.username != null) return false;
return password != null ? password.equals(that.password) : that.password == null;
}
@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
return 31 * result + (password != null ? password.hashCode() : 0);
}
}
2. Problems Kotlin solves _52
53. 3. Solution: direct to Kotlin – 18 lines
—// Java
public class Credentials {
private final String username;
private final String password;
public Credentials(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() { return username; }
public String getPassword() { return password; }
@Override
public String toString() {
return username + '/' + password;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Credentials that = (Credentials) o;
if (username != null ? !username.equals(that.username) : that.username != null)
return false;
return password != null ? password.equals(that.password) : that.password == null;
}
@Override
public int hashCode() {
int result = username != null ? username.hashCode() : 0;
return 31 * result + (password != null ? password.hashCode() : 0);
}
}
2. Problems Kotlin solves _53
// Kotlin
class Credentials {
private val username: String
private val password: String
constructor(username: String, password: String) {
this.username = username
this.password = password
}
fun getUsername(): String { return username }
fun getPassword(): String { return password }
override fun toString() = username + '/' + password
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Credentials
return username == other.username && password == other.password
}
override fun hashCode() = 31 * username.hashCode() + password.hashCode()
}
54. 3. Solution: remove getters – 16 lines
—// Kotlin
class Credentials {
val username: String
val password: String
constructor(username: String, password: String) {
this.username = username
this.password = password
}
//fun getUsername(): String { return username }
//fun getPassword(): String { return password }
override fun toString() = username + '/' + password
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other?.javaClass != javaClass) return false
other as Credentials
return username == other.username && password == other.password
}
override fun hashCode() = 31 * username.hashCode() + password.hashCode()
}
2. Problems Kotlin solves _54
72. // Kotlin
val results
= (driver as JavascriptExecutor).executeScript("...")
if (results is List<*>) {
val list = results.filterIsInstance<String>()
}
6. Solution: Kotlin smart cast
—
2. Problems Kotlin solves _72
73. // Kotlin
val results
= (driver as JavascriptExecutor).executeScript("...")
if (results is List<*>) {
val list = results.filterIsInstance<String>()
}
6. Solution: Kotlin smart cast
—
2. Problems Kotlin solves _73
80. 7. Solution: Kotlin type inference
—
// Java
final int i = 0;
final String s = "abc";
final List<String> list = new ArrayList<>();
// Kotlin
val i = 0
val s = "abc"
val list = listOf("a", "b", "c")
2. Problems Kotlin solves _80
91. Why not Groovy?
—
• Not statically typed: runtime bugs
• Not statically typed: performance
• Not statically typed: IDE support
• No null safety
2. Problems Kotlin solves _91
92. What’s going on here
—
• What is Kotlin?
• Problems Kotlin solves
• Demo
Excuse me, sir, do you have a moment to talk about tests in Kotlin? _92
93. Example test project in Kotlin
—
• JUnit http://junit.org/junit4/
• Html Elements https://github.com/yandex-qatools/htmlelements
• Allure Framework https://github.com/allure-framework/
• Gradle https://gradle.org/
• gradle-docker-plugin https://github.com/bmuschko/gradle-docker-plugin
3. Demo _93