0

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!

5
  • Let your method accept a vararg: String ... args. And please learn Java a bit. Commented Feb 23, 2017 at 9:35
  • @EgorZhuk Bit will not be enough.. :D Commented Feb 23, 2017 at 9:35
  • Maybe your addNewUser should accept some kind of NewUserParameters object. 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 Commented Feb 23, 2017 at 9:38
  • Possible duplicate stackoverflow.com/questions/9349418/… Commented Feb 23, 2017 at 9:39
  • You may just pass your parameters as a User object and inside it you can pass whatever parameter you want. If you want to add more properties you can do so by adding those to your User class. Commented Sep 20, 2018 at 11:03

4 Answers 4

1

What if in the other future project , it will require to add an account type?

then in the future you will need to overload the method

public void addNewUser(String un, String em, String pw){

}

//only username
public void addNewUser(String un){

}

//with all the info + token
public void addNewUser(String un, String em, String pw, String token){

}

the only thing you need to be aware is: those are all Strings... so you need to know in forehand what string is to which parameter...

Sign up to request clarification or add additional context in comments.

Comments

1

You can use map as parameter.this will help you pass parameter dynamically.

 public void addNewUser(Map<String, Object> mapInput){
    Iterator entries = mapInput.entrySet().iterator();
    int i=0;
    while (entries.hasNext()) {
        Entry thisEntry = (Entry) entries.next();
        String value = (String)thisEntry.getValue();
        cell = row.createCell(i);
        cell.setCellValue(value);
        i++;
    }
}

Comments

0

You could create an Interface AddNewUser, that has a method createUser that returns for example the created XSSFRow row.

Then you create an implementation of that interface ,e.g. AddUserWithEmail, that gets the specific values in constructor and then in createUser method adds the user and email to the row.

If you have another customer who needs different values in table you just create a new variant of the AddNewUser interface and use it instead.

Comments

0

Yes it is possible. By using thi variable arguments parameter :

public void foo(String... strings){
    // method body
}

so with this functions you can call :

foo("name");
foo("name", "account");
foo("name", "account", "age");

etc...

And usings this arguments like this:

public void foo(String... strings) {
        if (0 < strings.length()) {
            String name = strings[0];
        }
        if (1 < strings.length()) {
            String account = strings[1];
        }
        if (2 < strings.length()) {
            String account = strings[2];
        }
        // etc
    }

Hope this helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.