Hello all welcome back to the next one. And this one will learn a very interesting concept of testan G, and that's known as data providers. So let's, what I've done here is I've created a class called test mg underscore data providers on the left here. And that's this class. I don't have a lot of things here, just the class name. And I created one test method named it as test method one.
And it's printing two things here, input one and input two. And you can see here that the method takes two arguments string input one and string input two. That's the current setup. Now let's talk about data providers and when we need it, so we have seen that sn has parameters which can provide the values from the XML file, and we can instead of hard coding the values and test methods, we can provide them from XML file. But what if there is a scenario that is You want to test something multiple times with different set of data. Let's say first time you test this method with ABC and D EF.
And then you want to test it with CBE and XYZ. So basically, these are two different set of data. But you only have one test method, I'm pretty sure that you don't want to write different test methods for different data sets, right? You want to write one test method, which tests which test the functionality. So basically, test method should focus on the functionality of the application, then somehow, we should be able to pass different kind of inputs to the same test method. And we should be able to execute that test method with all those different set of input data.
So basically, let's say if I take an example of web automation, I have Login page, let's imagine and I want to test the login functionality with different data set. Once that is valid, I have a valid username and password. And this applies to rest case also, if we have REST API testing, or even for back end testing, because we log in pretty much everywhere. So we have different multiple data sets. One is valid set where we have a valid login and valid password. So my output should be I should be able to log in.
And then for that valid set, I may have different kinds of input like one is the username with the maximum available characters one username with the minimum available characters because a lot of times user name has limit, like the character could be between eight to 12. And the same thing is with password also, a lot of websites or rest API's have a limit of password that password could be between 10 to 20 characters. So we may have different data set. I may have one password With 10 characters, and then I have another password with 20 characters then another password I may have with special characters inside it. So it's pretty common to have different data sets for the same functionality. And what we want to understand is how do we test all those things with just one single method.
So here comes test NGS data provider. And now let's take a look at it, how we can do it. So what I'm doing here is one test method. And we already saw it accepts two inputs. So I'm gonna first write a method called public object arrays. So basically, it's a 2d array, and it returns a 2d array.
That's why we don't have a type as wide, we have a type as object array. And I'm going to name it as get data. We can name this method anything. So we are creating this method which is gonna give us the data for our test map. So since it returns an object to the array, we're gonna return a new object and 2d array. And inside it, I'm gonna write the arrays.
So let's see the first input. So we can provide different set of data, right? So input one, and input two is one set of data for them, both of them, let's provide two values. So I am providing BMW and M three. So let's say this is my first data set, BMW is input one, and M three is input. So that's how it goes sequentially.
And then I'll provide the second set of data. Maybe I want to run my test case with a different car value. So let's say Audi, and I'm going to provide a six. And let's take one more set of data. So this time I'm going to take maybe Ben And I don't know, maybe c 300. So C 300.
So now what we have here is, and we will end this written thing with a semicolon and we save the file and everything is fine. So what we have is, we have three rows here, right? So, three rows means we have three set of data. So this test case is gonna run three times with these data sets. So first data set is BMW M three, then the second one is already a six, and the third one is Benz c 300. Now how do we say that it's a data provider?
What we do here is we add the attribute here, we add the annotation. So at the rate data provider, so I'm going to type I type da and hit control space. And then I will select at the rate data provider from our dot test ng g dot annotations and I will given name to the data provider. And this name could be anything. Since I'm keeping this as inputs or data for my this method, I'm just giving it a name inputs. And you can actually give it inputs ABC, XYZ, whatever you like.
But we have to give a name to this data provider. So now we have a name. Now how does this add the rate test annotation or this test method knows that I need to use this get data method as my data input, so it knows with the help of the attributes, so inside at the rate test annotation, we'll use parentheses here, and we'll type data and so please notice these small here and then hit control space. And we'll use data provider equals two and then here we'll give the name so these two names should match. This method name can be anything but these two names should match. Once this is done, The method test method one knows that I need a data provider with the name inputs, and everything will be fine.
So let's right click and run this code file as test in G test and see the output. So what I have here is, let's first see this guy. So I had one method only test method one, and we can see that this method was run three times, right? First time it was run with the value BMW and M three. Second time it was run with the values already and asex. And third time it was run with the values Benz and C 300.
So the same method was executed three times. That's the power of data provider. And now let's see the console. We can see what we are printing up here we are just printing the inputs. So basically, it's going to take the inputs here, right. So input one and input two First time is BMW and M three.
Then again, second time, the inputs are already NSX. Third time the inputs are Benz and C 300. And it says that it passed three methods and it shows the values. And here also we can see there are three methods. So that's about it. That's pretty cool, right?
Data providers are really powerful, and we can really use them in lots of different ways. So please download the code file and practice it. Let me know if you have any questions, and I'll see you in the next lectures. Thanks for attending this one. Thanks.