This document discusses how Java 8 features can improve test automation. It covers lambda expressions, streams, and other Java 8 features that enable more declarative programming and reduce code. Examples show filtering, mapping, and reducing collections of data in more concise ways using streams. The document also discusses Java interfaces, preconditions for testing, entity-driven testing with Java Driver Interface, and new features in Java 9.
4. 4CONFIDENTIAL
• “How does Java 8 exert hidden power on Test
Automation?” (https://goo.gl/uT4XfN)
• “Test UI with JDI: easy, simple and good”
(http://goo.gl/Mwta77, https://goo.gl/1kxZS2)
• “Java Tip of the Week #1 – Lombok”
(https://goo.gl/Toxyeh)
INSPIRED WITH
6. 6CONFIDENTIAL
public Function<Integer, String> toStr = i -> i.toString();
public void print(int num, Function<Integer, String> func) {
func.apply(num);
}
print(22, i -> (i / 10)+”decades”);
print(3, toStr);
print(-30, i -> { if (i >0) return i.toString();
return –i; } );
FUNCTION AS VARIABLE
7. 7CONFIDENTIAL
{3, 1, 4, 5, 2, 8, 10, 12, 0, 4, 2, 1, 10, -1}
STREAM
• Even numbers > 3
• Only unique values
• Sorted by Ascending
• Cut 2 lowest and highest values
• Wrap rest of numbers with “( n )”
• And print
“(8)(10)”
8. 8CONFIDENTIAL
//32 strings
public String printResultsJava7(Integer[] strings) {
// Filter
List<Integer> filtered = new ArrayList<>();
for (int s : strings)
if (s > 3 && s % 2 == 0)
filtered.add(s);
//Distinct
List<Integer> distinct = new ArrayList<>();
for (int s : filtered)
if (!distinct.contains(s))
distinct.add(s);
// Sort
Object[] sortedArray = distinct.toArray();
Arrays.sort(sortedArray);
// Trim
List<Integer> trimBounds = new ArrayList<>();
for (int i = 0; i < sortedArray.length; i++)
if (i > 0 || i < sortedArray.length)
trimBounds.add((int)sortedArray[i]);
// Map
List<String> print = new ArrayList<>();
for (int s : trimBounds)
print.add("(" + s + ")");
// Reduce
String result = "";
for (String s : print)
result += s + ";";
return result;
}
JAVA 7
// 6 strings
public String printResultsJava7(Integer[] strings) {
List<Integer> filtered = filter(strings);
List<Integer> distinct = distinct(filtered);
Integer[] sortedArray = sort(distinct);
List<Integer> trimBounds = trim(sortedArray);
List<String> print = print(trimBounds);
return reduce(print);
}
9. 9CONFIDENTIAL
public String Filter(Integer[] strings) {
return stream(strings)
.filter(el -> el > 3 && el % 2 == 0)
.distinct()
.sorted()
.skip(1).limit(1)
.map(el -> "(" + el + ")")
.reduce((str, el) -> el + ";").get();
}
JAVA 8
12. 12CONFIDENTIAL
public String getMessage(int httpStatus) {
switch(httpStatus) {
case 404:
return “Page not found”;
case 500:
return “Internal Server error”;
default:
return “Unknown status code”;
}
}
SWITCH
13. 13CONFIDENTIAL
public WebElement getVisibleElement(By locator) {
List<WebElement> elements = driver.findElements(locator);
switch(elements) {
case elements.size() == 1 && elements.get(0).isDisplayed():
return elements.get(0),
case elements.size() > 0:
throw new RuntimeException(
format("Find %s elements instead of 1", els.size()));
default:
throw new RuntimeException("Visible elements not found")
); }
SWITCH IMPOSSIBLE
14. 14CONFIDENTIAL
public WebElement getVisibleElement(By locator) {
List<WebElement> elements = driver.findElements(locator);
return Match(elements).of(
Case(els -> els.size() == 1 && els.get(0).isDisplayed(),
els -> els.get(0)),
Case(els -> els.size() > 0,
els -> { throw new RuntimeException(
format("Find %s elements instead of 1", els.size())); }),
Case($(),
() -> { throw new RuntimeException("Visible elements not found") })
); }
JAVASLANG
18. 18CONFIDENTIAL
PRECONDITIONS
public class States() {
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
}
public class States() {
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
}
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
public static void isLoggedIn() {
logger.info("Move to condition: " + condition);
if (loggedIn()) return;
logIn();
if (!loggedIn())
throw new Exception(“Can’t log In”);
else
logger.info(“User logedIn succesfullly");
};
}
19. 19CONFIDENTIAL
ENTITY TABLES
@Data
public class Entity {
public String Номер;
public String Тип;
public String Пользователь;
public String Описание;
}
public class Row {
public Text Номер;
public Dropdown Тип;
public Link Пользователь;
public Description Описание;
}
20. 20CONFIDENTIAL
Row user = UsersTable.firstRow(row ->
textOf(row.description).contains(“tasks”) &&
textOf(row.type).equals(“Admin”));
user.description.choosen.select();
ENTITY TABLES
CheckboxColumn name
25. 25CONFIDENTIAL
• Incredible flexibility
• Greatly reduce code
amount
PROS N’ CONS
• Learn lambda staff
• Change your and your teammates
mind
• Sometimes more complex debug
38. 38CONFIDENTIAL
EDT: FILL AND SUBMIT
Provide List<User> for test
0. Have DefaultUser in DB
1. Login with DefaultUser
39. 39CONFIDENTIAL
EDT: FILL AND SEND
Provide List<User> for test
0. Have DefaultUser in DB
1. Login with DefaultUser
2. Submit Contact Us Form for DefaultUser
40. 40CONFIDENTIAL
EDT: EXTRACT
Provide List<User> for test
0. Have DefaultUser in DB
1. Login with DefaultUser
2. Submit Contact Us Form for DefaultUser
3. Get Act. Opening from Vacancy table
41. 41CONFIDENTIAL
EDT: VALIDATE
Provide List<User> for test
0. Have DefaultUser in DB
1. Login with DefaultUser
2. Submit Contact Us Form for DefaultUser
3. Get Act. Opening from Vacancy table
4. Assert Act. Opening equals to Exp. Opening
ExpectedActual
43. 43CONFIDENTIAL
Learn language features
Learn modern tools and frameworks
Use popular approaches
Do your work better (write tests faster, more
stable, with better quality etc.)
Have fun with your work!
CONCLUSIONS
44. 44CONFIDENTIAL
• Modularity
• New HTTP Client (Http2.0 & WebSocket)
• Improved Process API
• Language try-new, anonymous Diamond, private in
interfaces, immutable set, list-optional to stream
• Improved logging…
JAVA 9
http://www.baeldung.com/new-java-9
27.07.2017