Any Functional test suite would involve a fair amount of Negative Test scenarios in it. Usually the expectation here is that whenever a user is doing something which is not supposed to do, he should be stopped and should be shown appropriate error message informing why he cannot proceed any further.
So, how do we accomplish such scenarios scripting in Selenium? I have tried to make use of Java’s Exception handing feature here.
UseCase:
- New User setup is in process
- In the screen where user phone number and Password has to be entered, we get a error message if we enter invalid details
For the purpose of scripting, i considered two scenarios here”
Scenario 1: User enters invalid detail for phone number
Script Alogrithm:
- Create a custom exception class called invalid phone number
- Data validation on the app happens when user clicks Next button on the screen
- hence, I’ve a separate method which just clicks on the Next button and then starts searching for Error message objects on the screen, one after another
- Finally, i start throwing exceptions
- Throw exception for each error message separately. For example – phone number exception. Throw this if only phone number is wrong
- Throw exception for multiple error messages in one shot. For example – if both phone number and password is invalid, throw one common exception for it.
As you can see, point#4 above has to be designed keeping in mind what types of negative scenarios you are automating. You may have create multiple exceptions depending on how many errors each screen may throw. And also you need to keep in mind the multiple error messages that appears on screen, when user inputs wrong data in more than one field on the screen, you need to create separate exception for each kind of combination. Also, you need modify your Test class accordingly, where you catch these exceptions
package testNGTests;
import java.io.File;
import java.lang.reflect.Method;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Reporter;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import exceptionPackage.PasswordAndPhoneException;
import sam.pomEnrollSupportPersonDetails;
import sam.pomEnterSupportPersonDetails_Exception_SAM;
import sam.pomHomeSAM;
import sam.pomLoginSAM;
import sam.pomPersonnelSupportSAM;
import sam.pomPreviewSupportPersonSAM;
import utilityPackage.utilityClass;
public class CreateSAMPersonnelNegativeTest_InvalidPhonePassword {
WebDriver driver;
pomLoginSAM l;
pomHomeSAM h;
pomPersonnelSupportSAM p;
pomEnrollSupportPersonDetails enr;
pomEnterSupportPersonDetails_Exception_SAM ent;
pomPreviewSupportPersonSAM pr;
String userIDCreated;
int ChromeInvokeCount;
int IEInvokeCount;
@BeforeTest
public void setup(){
ChromeInvokeCount=0;
//Launch Chrome Browser and navigate to SAM
Reporter.log("Opened Chrome Browser");
File file = new File("C:/Selenium/JarFiles/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
driver = new ChromeDriver();
driver.get("https://sunview-aitc.suntrust.com/s1gcb/logon/csr");
//Login to SAM here itself, so that login step doesnt get iterated
l= new pomLoginSAM(driver);
l.loginToSAM("chromeUser", "password2");
}
@Test (dataProvider = "dp")
public void chromeTest(String FN, String LN, String userID) throws Exception{
ChromeInvokeCount++;
Reporter.log("Chrome Test Iteration number "+ChromeInvokeCount);
CreatePersonnel (FN,LN,userID,"Chrome");
}
@DataProvider (name = "dp")
public String[][] dataFeeder(Method m) throws Exception{
String[][] TestData = null;
String filePathChrome = "C:\\Selenium\\TestData\\SAMCreatePersonnelChromePhnNumExcep.xlsx";
utilityClass u = new utilityClass();
TestData = u.excelDataReader(filePathChrome);
return TestData;
}
public void CreatePersonnel (String FN, String LN, String userID,String Browser) throws Exception {
//* Assign class variables to respective object instance
h= new pomHomeSAM(driver);
p = new pomPersonnelSupportSAM(driver);
enr = new pomEnrollSupportPersonDetails(driver);
ent = new pomEnterSupportPersonDetails_Exception_SAM(driver);
pr = new pomPreviewSupportPersonSAM(driver);
Reporter.log("Logged into SAM in "+Browser+" Browser");
//Click Personnel tab
h.navigateToPersonnelTab();
//Click on Enroll support person link
p.clickenrollSupportPersonLink();
//Enter the ID and Name of support personnel
enr.enrollSupportPerson(FN, LN, userID);
//Enter the details of support personnel. leave phn number and password blank
ent.enterSupportPersonDetails("", "");
/*Click Preview button. 2 Error messages are thrown at same time.
* One, the phone number
* Two, the blank password error
*/
try{
ent.clickPreviewButton();
}catch(PasswordAndPhoneException b){
ent.enterValidPhoneNumber("4444444444");
ent.enterValidPassword("password1");
}
//click preview again now
try{
ent.clickPreviewButton();
}catch(PasswordAndPhoneException k){
Reporter.log("PhoneNumber/password is not valid 2nd time too. Check Test case");
}
waitForSomeTime();
//Support Person details summary
pr.previewSupportPerson();
//Go to Home tab to accept Next TC
h.clickHomeTab();
Reporter.log("Created user "+userID+"in "+Browser+" Successfully");
}
public void waitForSomeTime(){
try {
Thread.sleep(8000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Inside the Function that clicks Next button
public void clickPreviewButton() throws BlankPhoneNumberException,BlankPasswordException,PasswordAndPhoneException {
previewBtn = driver.findElement(By.name("actionAddPreview"));
//Click preivew button
previewBtn.click();
/*code below will search for error messages now
* Note that, order in which you search error messages ,should be the order in
* which the error message will come on screen, when you violate all field-validation rules.
* If you follow the order, it helps during test case design.
*/
//Search for Phone number error message text
List <WebElement> phoneNumberErrorMSg = driver.findElements(By.xpath("//li[contains(text(), 'Customer Service Phone is required.')]"));
//search for blank password error text. (Not confirm password)
List <WebElement> passwordBlankErrorMSg = driver.findElements(By.xpath("//li[contains(text(), 'Password is required.')]"));
//if both Phone and password error message is there then, throw this exception
if (passwordBlankErrorMSg.size()>0 && phoneNumberErrorMSg.size()>0){
throw new PasswordAndPhoneException();
}
//If only Phone error message is present, throw this exception
if (phoneNumberErrorMSg.size()>0){
throw new BlankPhoneNumberException();
}
//If error message is present, throw exception
if (passwordBlankErrorMSg.size()>0){
throw new BlankPasswordException();
}
}