While writing UI automation tests I come across forms with many fields and instead of passing numerous arguments to test methods I create classes to quickly set form data. So if there is a sign up form I create SignUpDataObjectClass which would have instance variable corresponding to form fields i.e.
public class SignUpFormObject() {
private username;
private password;
public SignUpFormObject setUsername(String username) {
this.username = username;
return this;
}
public String getUsername() {
return username;
}
public SignUpFormObject setPassword(String password) {
this.password = password;
return this;
}
public String getPassword() {
return password;
}
// Util method to set signup test data. Test method can use it on the fly
public static SignUpFormObject setSignUpData() {
SignUpFormObject dataObject = new SignUpFormObject();
dataobject.setUsername("test user").setPassword("password");
return dataobject;
}
}
And then I came across a scenario when it did not make sense to have single data object class for a form. The form is related to creation of Coupon and then there could be different categories of Coupon. For sake of brevity I would limit the number of fields here. So there would be two kinds of coupons – offer and printable. Both of them will have coupon name and one field specific to offer
(let’s consider it – offer expire date) and another for printable (let’s consider it – printable code)
So I decided to create Coupon abstract class which would have all common fields and then CouponOffer and CouponPrintable classes would extend Coupon class and add their own fields i.e.
public abstract class Coupon {
private String couponname;
public Coupon setCouponname(String couponName) {
this.couponname = couponname;
}
public String getCouponname() {
return couponname;
}
And CouponOffer class comes here –
public class CouponOffer extends Coupon {
private String offerExpireDate;
public CouponOffer setOfferExpireDate(String offerExpireDate) {
this.offerExpireDate = offerExpireDate;
return this;
}
public String getOfferExpireDate() {
return offerExpireDate;
}
// Util method to set coupon offer data
public static CouponOffer setCouponOfferData() {
CouponOffer couponOffer = new CouponOffer();
couponOffer.setCouponname("test coupon");
couponOffer.setOfferExpireDate("31/12/2013");
return couponOffer();
}
}
I am willing to know if using abstract class is bad approach here and there are better solutions available.
I think it’s a good solution.
Your class
CouponOfferjust adds theofferExpireDatefunctionality. Any other functionalities (attributes) ofCouponcan be reused by other implementations.And no need to repeat the trivial
Couponfields for every implementation