What’s annoying in this test?
@Test
public void canPlaceOrderTest() {
HomePage homePage = new HomePage(driver);
homePage.open();
ResultsPage resultsPage = homePage.searchFor(keyword);
DetailsPage detailsPage = resultsPage.selectProduct(1);
detailsPage.addToCart();
Cart cartPage = detailsPage.goToCart();
Checkout checkoutPage = cartPage.checkout();
checkoutPage.updateOrderWithCreditCardInfo(
CARD_NUMBER, EXPIRY_DATE, CARD_TYPE);
checkoutPage.updateOrderWithShippingInfo(
DATE_TIME, SHIPPING_MESSAGE, SHIPPING_TYPE);
ConfirmationPage confirmationPage = checkoutPage.placeOrder();
Assert.assertTrue(confirmationPage.isOrderPlaced());
}
I don’t know how you like this but I am frequently annoyed about the fact that I have to say that
the type of homePage object is HomePage
the type of resultsPage object is ResultsPage
the type of detailsPage object is DetailsPage
so on
Wouldn’t it be nice if this can be avoided?
Yes, Alex, that would be so nice.
Also, the salmon from last night was pretty great too, do you have more?
That would be more than nice.
Coming back to code, we can remove all those annoying types as follows:
@Test
public void canPlaceOrderTest() {
var homePage = new HomePage(driver);
homePage.open();
var resultsPage = homePage.searchFor(keyword);
var detailsPage = resultsPage.selectProduct(1);
detailsPage.addToCart();
var cartPage = detailsPage.goToCart();
var checkoutPage = cartPage.checkout();
checkoutPage.updateOrderWithCreditCardInfo(
CARD_NUMBER, EXPIRY_DATE, CARD_TYPE);
checkoutPage.updateOrderWithShippingInfo(
DATE_TIME, SHIPPING_MESSAGE, SHIPPING_TYPE);
var confirmationPage = checkoutPage.placeOrder();
Assert.assertTrue(confirmationPage.isOrderPlaced());
}
Eclipse will figure out the type of the variable when compiling the code.
var only works in the latest versions of Java as it was introduced in Java 10.
Still, it is a pretty nice trick to use when it is obvious what the variable type is going to be.