31
loading...
This website collects cookies to deliver better user experience
loginAs(username, password)
to login with given username and password, testSuccessfulLogin(username,password)
which tests only one thing that login should be successful with valid username/password.calculateDeliveryCharge(Customer customer, OrderInfo orderInfo)
which calculates delivery charge when customer and order information is passed. When we pass Customer
object to calculateDeliveryCharge()
method with order information, it should return delivery charge. Note that we are providing free delivery to VIP Customers. So, when we pass VipCustomer
object for customer argument to calculateDeliveryCharge()
method with order information, the program/code should work properly without changing its behaviour and shouldn’t throw any exceptions.isEmailReceived()
.firstName, lastName, emailAddress, phoneNumber, deliveryAddress, billingAddress, getPurchaseHisotry(), getOrdersInProgress()
. But isEmailReceived()
method only needs to know firstName, lastName, emailAddress
fields and doesn’t need to now all the other fields/methods of Customer class.isEmailReceived(Customer customer, string subject, string body)
: We are exposing whole Customer class in isEmailReceived()
method and then one can easily access/change details like phoneNumber, deliveryAddress, billingAddress
in isEmailReceived()
method. We want to minimize the risk to Customer object by passing only details that are required to the clients/methods. We can achieve this by below implementation by using an Emailable
interface which represents a contact that can be emailed.interface Emailable {
String getFirstName();
String getLastName();
String getEmailAddress();
}
class Customer implements Emailable {
String firstName, lastName, emailAdddress;
String phoneNumber, deliveryAddress, billingAddress;
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getEmailAddress() {
return emailAdddress;
}
}
class EmailHelper {
public boolean isEmailReceived(Emailable recipient, String subject, String body) {
// Logic to check whether recipient recieved email or not
// You can use recipient.getFirstName(), recipient.getLastName(), recipient.getEmailAddress() to get recipient details
// When you pass Customer object to this method, only Emailable interface methos are available here and no other details are exposed from Customer Object
// Customer object is safe from any changes
return false;
}
}
isEmailReceived()
method with any implementation that implements Emailable interface and respective input object for recipient argument is safe from any changes/actions. I hope now you have understood the power of ISP.public class LoginPage {
private WebDriver driver;
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void loginAs(String username, String password) {
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.name("login")).click();
}
}