简体   繁体   中英

webdriver DataProvider TestNG

All I have problems with my code in TestNG. Need to Annotate Parameters to the method.

1) Generation Random Stings

     public String RandomName(){ 

     ArrayList<String> alphaNum = new ArrayList<String>();

       for (char i = 'A';i<= 'Z';i++){
           String s = new String();
           s +=i;
           alphaNum.add(s);

     }      

       for (char i = 'a';i<= 'z';i++){
           String s = new String();
           s +=i;
           alphaNum.add(s);

     }

       String[] RandomEnteredField = new String[10];
       for (int i = 0; i < RandomEnteredField.length; i++) {
           RandomEnteredField[i] = alphaNum.get((int)(Math.random()*alphaNum.size()));

     }

       String result = "";
       for(int i = 0; i < RandomEnteredField.length; i++)
       {
           result += RandomEnteredField[i];
       }
       System.out.println(result);
       return result;
      }

2) In test class I save it like global String

@Test(priority =0)
public void CreateAcount() throws Exception {
    objFifthIssue = new TheFifthIssue(driver);          
    objFifthIssue.ClickOnMyAccount();       
    String rn = objFifthIssue.RandomName(); // add new argument to EnterRegistrationForm and also 2 below
    EmailRandom = objFifthIssue.RandomName();
    Password = objFifthIssue.RandomName();
    objFifthIssue.EnterRegistrationForm(rn,EmailRandom,Password);
    objFifthIssue.VerifyRegistrationDone();     
}

3) I would like saved EmailRandom and Password to set in another test class like parameters

@Test(priority =2, dataProvider="LogInOUt")
public void LogoutLogIN(String EmailRandom, String Password) throws Exception {
    System.out.println(EmailRandom + Password);
    objSixIssue = new SixIssue(driver); 
    objSixIssue.LogoutLogIN(EmailRandom,Password);      
}

4) In DataProvider I try to

@DataProvider(name = "LogInOUt")
 public static Object[][] Generation() {    
    return new Object[] { { EmailRandom }, { Password }};    
}  

You have an interesting return in your Dataprovider there:

public static Object[][] Generation() {    
  return new Object[] { { EmailRandom }, { Password }};    
}

I assume what you're trying to do here is this:

public static Object[][] Generation() {    
  return new Object[][] { { EmailRandom }, { Password }};    
}

Now it's actually returning the correct datatype. However , this is not the correct way to utilize Selenium DataProviders. ( the above code won't work )

DataProviders, as you know, are two-dimensional arrays. This is how you're doing it:

Array 1: {argument 1}
Array 2: {argument 2}

This is how it's supposed to work:

Array 1: {argument 1, argument 2}
Array 2: {argument 1, argument 2}

The first dimension of the array iterates on each time it runs the method. It's two-dimensional so that you can run a different set of arguments with the same method over and over. Does that make sense?

Example: You want to run the method meet(Person, Person) three times. The first time you run it, you want Bob and John to meet. The second time, you want Laura and Sarah to meet. The third time, you want Bob and Sarah to meet. You would use this DataProvider on the meet() method:

public static Object[][] provider() {
  return new Object[][] {
    {Bob, John},
    {Laura, Sarah},
    {Bob, Sarah}
  };
}

Get it?

So now, if we look at your code, it appears you're only trying to pass one set of arguments with the DataProvider. Since it has to be a two-dimensional array, though, we can just put all the arguments in the first array. Like so:

@DataProvider(name = "LogInOUt")
public static Object[][] Generation() {
  return new Object[][] {{ EmailRandom, Password }};
}

Let me know if anything I said didn't make sense :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM