Parametrized Reusable Rules | Bondar Academy
Course: Mastering testRigor
Module: Reusability and Optimization
Instructor: Artem Bondar
Lesson Summary
In this lesson, we explore how to make reusable rules smarter by parametrizing them, enhancing their flexibility in test cases. Creating a Reusable Rule We start by creating a new test case for the login functionality: Navigate to the Login page. Enter [email protected] as the email and welcome as the password. Click the Login button and validate redirection to the dashboard . Defining the Rule The rule is named Fill username and password, and click Login button . Steps include: Enter email and password. Click the Login button. Assert that the header contains playground . Parametrizing the Rule To avoid hard-coding credentials, we can parametrize the rule: fill username with "username" and password with "password" This allows us to pass different credentials when invoking the rule. Using Global Variables We can also utilize global variables like super_username and super_password as parameters in our reusable rules. Example of Form Submission We create a rule for submitting forms that can handle different form layouts: Define a rule called submit form with super user credentials . Parametrize the form name to allow submission of different forms. Summary In summary, we learned how to: Parameterize reusable rules for better reusability. Use global variables as parameters. Adapt rules for different user interfaces. These techniques enhance the efficiency and flexibility of our testing processes.
Video Transcript
In this lesson, we will make reusable rules even smarter by parametrizing those rules. So using parametrized rules helps to make them more flexible when you use them in the test cases. So let's get into it. So we will create a new test case utilizing the login functionality. So go into auth, then click on Login. When I enter, for example, test.test.com with any password, I click Login, and I redirected back to the dashboard page successfully. So let me create a new rule related to this functionality, the same way how we did it before, and then later we parametrize this rule. So create a new rule, and I call this rule something like this. Fill username and password, and click Login button, and be taken to dashboard page. And this rule is related to login page, so I type login page. And here I just need to put my steps. Enter, for example, test at test.com into email address. Then enter, let me do this, I have copy, will be faster. Enter, let's say, welcome into password. Then I want to click button Login. And the expectation, check that. We need to validate that after we click the Login button, we navigate it to the dashboard page. And for example, the assertion can be this text playground, which is on the home page in the header. So how can we find it? So for example, we can use class logo or class header container, any of this. So let's use logo, the most closest class to the text that we are asserting. And the assertion will be check that Logo contains playground. All right, I believe that's it. Let's save this rule and we go back to create a new test. Add a new test case. And for example, I call it enter user name and password and click Login button. Enter user credentials to login to application, something like this. And in order to do this, we need to navigate to login page and then fill username and password. So you see how quickly I was able to create two steps for the test case using reusable rules. And that's pretty much it. So let's run this test and see if it is working as expected. All right, test passed as expected. So here we entered our credentials, click Login. And on the next step, we made validation that we are on the playground page. So that worked as expected. But if we go back to reusable rules one more time and look into this reusable rule that we just created, here is one problem. That we actually hard-coded our credentials, test.test.com and welcome. So what if, for example, you would like to use several different types of users to log into your application for your test? So one way of this is to creating several reusable rule and each rule for each type of the account. For example, fill admin username and admin password. And under these names, you can put the credentials inside of the test. This is, of course, not the optimal way. Much better way is to improve reusability of already existing rules by parametrizing those. So instead of hard-coding the values inside of the steps, you can parametrize those steps and only pass argument into the reusable rule. And this is how you would do this. First, you need to update the name of the reusable step with the arguments that you want to use, with the parameters that you want to use. For example, fill the username and put the username. This will be our variable that we're gonna use, username. And to be more readable, I will type fill username with username and password with password. So username in double quotes and password in double quotes becoming our parameters of the reusable rule. And now we can use those parameters inside of the test. Instead of calling the exact value that you wanna use, we would call enter stored value and the value of this parameter. So I will type stored value, username, and enter stored value, password, okay? So we also updated the name of the test and we need to save this test to be updated as well in the test case. So I will put save and update the name. And the test rigger immediately found which name and which test case we want to update. And it is this one that we just created, update selected, and I click update selected. Now going back to our test, quick edit, and here inside of the test, we see this situation, fill username with username and password with password. And instead of having here username and password, we provide the actual values inside of the test case that we want to use. For example, test at test.com, and password, welcome, something like this. And that's pretty much it, so going back one more time. So here we updated the name of the test case, providing in the double quotes the variable that we wanna use inside of the steps. And inside of the steps, we use stored value, username, and the actual value we pass from the test case level here. Fill username with test at test.com and password, welcome. Update and retest. All right, and test pass successfully. Let's check out the screenshots real quick. So here we go, test at test.com, and we are on the homepage, and Playground was validated. And now with a parameterized reusable rule, I can use any credentials right here, and they will be passed inside of the rule. For example, test123 at test.com, and I run it one more time. All right, test passed, let's check out the screenshots. You can see right now test123 at test.com was used as the value for our credentials. Let me show you one more example. So we also have, if you remember from the previous lessons, we also have the global variables such as super username and super password. How about if you want to use those global variables as argument for your reusable rule? How would you do about this? So let me show you this example. Going back to the test, edit it one more time. And let's say that after I navigated to the application using test123, I want to navigate one more time with a new credential. So I call my field username with username and password. But instead of using this placeholder, I type with stored value and provide the stored value of our global variables. So super user email, and instead of password, I provide stored value. And provide the global variable super user password like this. And let me run this test one more time. So first test should navigate us to the login page with test123. And the second test, we should navigate to login page again, but use a global variable for the username and the password. So let's run it one more time. All right, and test passed successfully, so let's check the screenshots. So first time we used test123 at test.com. We logged into the application, and then we repeat the cycle again using the same reusable rule. But this time we used credential super user at test.com, which we took from the global variables. And it was working successfully. So this is how you can parameterize the reusable rule, make them more universal, and more reusable across the test cases. And let me show you one more example that you can do with a reusable rule. For example, go to Form Layouts page. So let's assume you have application where you have similar forms on the page. And according to your test cases, sometimes you need to submit one form, and sometimes you need to submit the other form. But the context of the form is the same. The context, let's say, username and password and button that you need to click, or here, username and password and button that you need to click. And the difference only is the name of the form. So instead of creating two separate reusable rules for this kind of scenario, you can create just one reusable rule, which would handle both of those cases. And let me show you this example like this. So let's create a new rule first, new rule. And I would call it submit form with a super user credentials. For example, we will use our global variables as a super user intentionally. And this is Form Layouts page. And the steps would be the following. Enter stored value. Stored value super user email into email below. And let's say it will be using the grid, using the grid form. Below using the grid. Then we need to enter password. Enter stored value super user password into password below using the grid form. And then we need to click on the button to submit, to sign in. Here we have sign in. Okay, and we can click something. Click button below using the grid. Okay, so let me save it like this just for now. And then later we update this scenario. So now I'm going back to the test and creating new test case. Submit form by name, so we'll keep it simple. So first of all, we need to navigate to the Form Layouts page. And then we're looking for the rule, submit with super user credentials. So let's just run this real quick to make sure that it's working. And then we parametrize, add and run. All right, that's passed successfully, let's check out. So it was working as expected. Using the grid, superuser at test.com and password and we click sign in button. So it worked successfully. So now we can parametrize this reusable rule as well. And instead of working this only for using the grid form, we can parametrize to use, for example, in horizontal form, something like this. So it can be used for using the grid and horizontal form. And this is very easy to do. So we will do something like this, submit form with name. And we need to provide the parameter, form name, something like this. And now we need to use this parameter inside of the step. So form name, we put it here. And use stored value, form name. And I will replace it right here, below stored value, form name and below stored value, form name. That's it, so we replaced hard-coded form name with a parameter, which we will pass from the test. And by providing the form name, we will tell, okay, which form would we want to submit and I click Save and Update. So I'm going back to the test, Update Selected, and then going back to the test. And here, instead of form name, I would provide, for example, horizontal form, exactly as it is right now in the application, horizontal form. And now it's also easy to read, submit form with name horizontal form with a super user credentials, Update and Retest. All right, test pass successfully, let's check out the screenshots. Here we go, we submitted horizontal form with the credentials and clicked Sign In button. And if I just go to the test and update the form name, instead of horizontal form, now I want to submit using the grid form and run this test one more time. And this test pass successfully as well, check out the screenshot. Right now, we submitted using the grid form. So it worked as expected. So let me quickly summarize what we did in this lesson. In TestRigor, you can parameterize the reusable rules. To do that, you need to update the reusable name with a parameter in the double quotes. And then inside of the steps of the usable rule, you need to call stored value and in double quotes, call the parameter that you have defined in the name of the reusable rule. Also, if you would like to use global variables as a parameters for your reusable rules, in this case, you need inside of the test, provide the stored value and the value of the reusable rule. Even if the stored value is not the part of the name of your reusable rule name. So this is worth to remember. And also, you can parameterize not only the values related to the input that you want to use, you also can parameterize different user interface. For example, use the parameter of reusable rule as a relative locator on the page. All right, that's it guys and see you in the next lesson.