How To Provide Parameters To Test Methods

12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.24
List Price:  €93.20
You save:  €27.96
£55.93
List Price:  £79.91
You save:  £23.97
CA$95.58
List Price:  CA$136.56
You save:  CA$40.97
A$107.13
List Price:  A$153.05
You save:  A$45.92
S$95.25
List Price:  S$136.08
You save:  S$40.82
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.85
List Price:  CHF 91.21
You save:  CHF 27.36
NOK kr770.13
List Price:  NOK kr1,100.23
You save:  NOK kr330.10
DKK kr486.51
List Price:  DKK kr695.05
You save:  DKK kr208.53
NZ$117.65
List Price:  NZ$168.07
You save:  NZ$50.42
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.19
List Price:  ৳10,970.75
You save:  ৳3,291.55
₹5,834.32
List Price:  ₹8,335.10
You save:  ₹2,500.78
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦91,422.33
List Price:  ₦130,608.93
You save:  ₦39,186.60
₨19,487.31
List Price:  ₨27,840.21
You save:  ₨8,352.89
฿2,585.90
List Price:  ฿3,694.31
You save:  ฿1,108.40
₺2,277.50
List Price:  ₺3,253.72
You save:  ₺976.21
B$361.10
List Price:  B$515.88
You save:  B$154.78
R1,320.17
List Price:  R1,886.05
You save:  R565.87
Лв127.48
List Price:  Лв182.13
You save:  Лв54.64
₩96,293.43
List Price:  ₩137,567.94
You save:  ₩41,274.51
₪266.53
List Price:  ₪380.78
You save:  ₪114.24
₱4,038.21
List Price:  ₱5,769.12
You save:  ₱1,730.90
¥10,975.59
List Price:  ¥15,680.08
You save:  ¥4,704.49
MX$1,206.90
List Price:  MX$1,724.22
You save:  MX$517.31
QR255.26
List Price:  QR364.68
You save:  QR109.41
P965.80
List Price:  P1,379.77
You save:  P413.97
KSh9,448.65
List Price:  KSh13,498.65
You save:  KSh4,050
E£3,352.63
List Price:  E£4,789.68
You save:  E£1,437.04
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,489.70
List Price:  Kz83,560.30
You save:  Kz25,070.60
CLP$66,385.51
List Price:  CLP$94,840.51
You save:  CLP$28,455
CN¥507.19
List Price:  CN¥724.59
You save:  CN¥217.40
RD$4,111.01
List Price:  RD$5,873.12
You save:  RD$1,762.11
DA9,398.61
List Price:  DA13,427.17
You save:  DA4,028.55
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q544.39
List Price:  Q777.73
You save:  Q233.34
GY$14,639.41
List Price:  GY$20,914.34
You save:  GY$6,274.92
ISK kr9,791.60
List Price:  ISK kr13,988.60
You save:  ISK kr4,197
DH708.11
List Price:  DH1,011.63
You save:  DH303.52
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,016.98
List Price:  ден5,738.79
You save:  ден1,721.81
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,575.16
List Price:  C$3,678.96
You save:  C$1,103.79
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/261.07
List Price:  S/372.97
You save:  S/111.90
K269.52
List Price:  K385.04
You save:  K115.52
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,842.03
List Price:  ZK2,631.59
You save:  ZK789.55
L324.63
List Price:  L463.78
You save:  L139.14
Kč1,640.70
List Price:  Kč2,343.96
You save:  Kč703.26
Ft25,605.88
List Price:  Ft36,581.40
You save:  Ft10,975.51
SEK kr762.91
List Price:  SEK kr1,089.92
You save:  SEK kr327.01
ARS$61,153.48
List Price:  ARS$87,365.86
You save:  ARS$26,212.38
Bs484.56
List Price:  Bs692.26
You save:  Bs207.70
COP$275,828.59
List Price:  COP$394,057.74
You save:  COP$118,229.14
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,727.65
List Price:  L2,468.18
You save:  L740.53
₲519,796.34
List Price:  ₲742,598.03
You save:  ₲222,801.68
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.95
List Price:  zł402.81
You save:  zł120.85
Already have an account? Log In

Transcript

Hello all welcome back to the next one. In this one, we'll see how we can provide parameters from testan g file to our test methods. Take a look at this, I open Eclipse and I created a file called test and j underscore test parameters. And here it is in the test classes package itself. I have a before class here. And I have a test method here.

So what I'm printing here is from test parameters, this is a setup. And I'm printing parameter value from XML file and it's twice basically I'm printing the same thing twice because we will try to read multiple parameters in the setup class. And in either a test method, I am just printing one response from XML file. So we will take a look at providing parameters in the XML file. I just wanted to go over the class structure right now because what we want to understand is the scenario where We would need the parameters from testan g dot XML file. Let's imagine you have a setup, which is maybe let's say you are running Selenium WebDriver, or mobile automation test cases.

And you want to run your test cases on Firefox browser. So what you can do here is in setup, you can read which browser you want to run the test cases from the test and G dot XML file. And based on that, you can initialize the driver whether you want to initialize Firefox driver, or if the parameter says chrome then you can initialize chromedriver. Same thing on mobile automation also. And maybe you're running REST API test cases, you want to read some expected response from parameters file so you can read that response. And you can do multiple things like that.

So basically, for setup kind of things or maybe in test cases also, you can read parameters from testan g file and you can read Cuse them. So let's take a look how we define them in the test in G dot XML file. So, I created a file called test ng g dash parameters dot XML here, it is a parameter suite, and there is one class incited test ng g underscore test parameters and now we will create the parameters. So, inside the test tag, we will define in this angle brackets I will say parameter So, the second one and we can do Ctrl Space it shows us all the options right this will only show when you have doubt type suite system line in the first So, when you have this statement in the first line of test and the file then only you will see this IntelliSense So, right now I got this by pressing Control space And in the name, what I'm gonna do here is I'm going to provide browser.

And in value, I'm going to provide maybe Firefox. And let's define one more parameter. So angle bracket, P. And I can select this from the list and it completes it for me. Here, I'm going to provide platform. In the value maybe I can say Mac or Windows. And let's define one more parameter.

And here I'm going to say response for REST API if you have, and maybe our responses to 01. So basically, do not mix up Selenium with REST API or REST API with Selenium. I'm just taking both the examples here. So depending on what you're doing, you could just use right. So Selenium WebDriver and REST API are two different type of testing. So please do not mix them.

I'm just taking examples here. One thing to notice here is this thing is parameter. It's not parameters. So there is no s. And it has to be defined separately. There is no group tag like this like classes, and then we define individual class. So there is no tag for parameters.

And then we define parameter. So there's only one parameter tag, which we use to define, we cannot group them in a particular parameter stack, so there is nothing like that. So this is the parameter that we defined in the test in G dot XML file. And the real name is test and G dash parameters dot XML, you can name it anything. Now we'll go to our test class. I have a before class and I have a test method.

So let's say I want to read a couple of parameters. Maybe these two browser and platform in the setup class. So basically in the setup method before class before For doing that, we need to add one more annotation here. So the annotation name is parameters, I type p A and then hit control space. So it gives me all these options. So pick the one from our dot test and G dot annotations do not pick this parameter.

This is the difference we have to pick this one from test ng g dot annotations and this reads as parameters. This does not read as parameter. The reason is we can actually read multiple parameters from the test ng g file into one method, so that's why they added s here. Now how we add here is we add parenthesis and inside it we add curly braces inside it. in double quotes, we can name the parameter which we want to read. And then by comma, we can separate them and read another one also.

So we have read browser and platform here in the setup Same thing we can do here. So, parameters from testan G. And here I'm gonna read the one response. So, we are reading two parameters in the setup method and we are reading one parameter in the test method one. So I took both the examples because I wanted to show that we can read multiple parameters in one method, and we can read a single parameter also. Now when we read parameters, they don't take into effect by default we have to tell our method that something is being read. So here we define string and browser and then string platform.

If we are reading two parameters here from the test, NJ dot XML file, we need to define two arguments to this method. If we are adding one parameter we need to define one arguments so Here we're going to say string response. That's it. Once that is done, now we are actually reading the parameters from the XML file. Now how can we demonstrate that we are reading the values from there? So we are reading browser and platform but the real values are Firefox, Mac and 2014 response, right?

What we can do here is in the print statement, I'm gonna print the values so I'm gonna say browser here, this is this one, right? And I'm gonna add plus platform here. And then here, I'm gonna add plus response. So basically, this thing will read the values from the XML file. This is what this is a key value pair right browser is a key Firefox's value platform is a key Mac is a value responses the key to 01 is the value. So basically what it does is it takes the key and then test and it looks at the key and sends back the value to the methods.

Now let's run this by right clicking on the XML file, run as test in G Suite. And we can see it here. So from the setup method, parameter value from XML file is Firefox, first one and the second one is Mac. And for the test method, the responses to 01. So basically, we are reading the values by using the keys right. The advantage is that we don't have to hard code everything in our code files, we can provide the values from the XML file which is external, this is very helpful.

So we can only provide the strings from here we cannot provide like integers by default, if we have to provide integers like if we want to consider 201 as an integer We have to convert it here. By using Java casting like test engine or Selenium WebDriver or REST API, they don't take care of casting, it has to be done in Java. So if we if in case we want to do that, we have to cast it or converted. And if we want to provide arrays or anything else also, we can actually manipulate these things like we can do something like 201 separated by comma 200. And we can read, we can still read one parameter here. And then maybe let's say, I want to split it into a array.

So I can say maybe string array. Then I can read this response inside it. Sorry, response dot split, and then we can split them by a comma. So what it will do here is basically what we are doing As we are using Java methods to kind of treat it as an array, it is still a string value separated by comma, what we are doing is we are reading string value and then inside the array we are, we are storing the string how we are storing we are using split method separated, we are giving the separator as comma. So, it will split the string into an array wherever it will see, comma, and this is about it. So, what I can show you here is maybe, instead of response, I can show you the response also.

Let's do it. And then let's take a look at the array values. So, string array I'm gonna put it here. So, let's say this is the 100 index and this is the one at first index so I'm reading Both the values in the print statement string array at zero index and string array at first index because we provided only two values, so definitely there's only going to be two items. So what we can do here is string array, index zero and string array index one. That's about it.

So now we are reading both of them. So let's run it run as test in G Suite. And we can see so the total response was still string 201 comma 200. And in string array, at zero index, we provided 201. At first index we provided 200 because we separated the string or split the string with comma right, so that's about it. One more thing is that we cannot run this file individually if I try to run it as test Angie it will kind of fail because it is expecting some parameters and we are providing parameters from say it is expecting some parameters.

And we are providing parameters from test mg XML file. So we can only run these using the test ng g dot XML file. So that's about it, guys. Please download the files practice and let me know if you have any questions and see in the next lectures. Thanks for attending this one.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.