I have a method addNewUser which asks for users Username, Email and Password. Those will be stored in the excel sheet on its corresponding columns. Please see code below:
public class ExcelConfig {
public FileInputStream fis;
public FileOutputStream fos;
public XSSFWorkbook wb;
public XSSFSheet sh;
public XSSFRow row;
public XSSFCell cell;
int lastRow = 0;
ConfigReader cr = new ConfigReader();
public ExcelConfig(){
try {
fis = new FileInputStream(cr.getExcelPath());
wb = new XSSFWorkbook(fis);
}
catch (Exception e) {
System.out.println("Error at ExcelConfig " + e.getMessage());
}
}
public void addNewUser(String un, String em, String pw){
cell = row.createCell(0);
cell.setCellValue(un);
cell = row.createCell(1);
cell.setCellValue(em);
cell = row.createCell(2);
cell.setCellValue(pw);
}
}
Now, in other class, I called addNewuser method. See code below
public class NextStepTest {
WebDriver driver;
ConfigReader cf;
ExcelConfig ec;
UserDetails ud;
LandingPage lp;
RegisterPage rp;
String username;
String email;
String password;
@BeforeTest
public void setUp(){
cf = new ConfigReader();
ec = new ExcelConfig();
ud = new UserDetails();
driver = cf.getChromeDriver();
driver.get(cf.getAppUrl());
}
@Test
public void register(){
username = cf.getUsernameFromProperty();
email = cf.getEmailFromProperty();
password = ud.getPassword();
try{
lp = new LandingPage(driver);
lp = PageFactory.initElements(driver, LandingPage.class);
lp.locateRegisterLink();
lp.clickRegisterLink();
rp = new RegisterPage(driver);
rp = PageFactory.initElements(driver, RegisterPage.class);
rp.locateUsernameField();
rp.registerAccount(username, email, password);
ec.getSheet(0);
ec.getNextRow();
ec.addNewUser(username, email, password); // here I call addNewUser method that now has username, email and password values
ec.closeFile();
rp.logout();
lp.locateLoginLink();
}
catch(Exception e){
System.out.println("Error under NextStepTest.java => " + e.getMessage());
}
}
Now, what I want to ask is that is it possible to make addNewUser method parameters dynamic? I know that this scenario is project dependent. Some project may require only username, email and password in this case to be added on excel. What if in the other future project , it will require to add an account type? So the parameters will now become 4. Or other projects will only require email and a password. Should I update addNewUser method parameters every time? I'm still a starter in this language. Any help will do. Thanks everyone!
addNewUsershould accept some kind ofNewUserParametersobject. You can build such an object with whatever parameters you need in the given context, and then pass it to your method. See also builder pattern