Using Variables | Bondar Academy
Course: Mastering testRigor
Module: Reusability and Optimization
Instructor: Artem Bondar
Lesson Summary
In this lesson, we focus on improving reusability and optimization of our tests by utilizing variables . Variables allow us to store values and reuse them throughout the script, reducing duplication and enhancing maintainability. Key Concepts Defining Variables: To create a variable, use the prompt: save , followed by the value and the variable name. For example, save 17 as dayToSelect . Using Variables: Replace hard-coded values in the script with the variable name. For instance, instead of using 17 multiple times, use dayToSelect . Parameterizing Strings: When incorporating variables into strings, use the syntax ${variableName} to ensure proper formatting. Example Implementation In the date picker test , we initially had the date May 17 hard-coded multiple times. By saving it as dayToSelect , we streamlined the script. Similarly, in the table test scenarios , we replaced hard-coded values for columnID , firstName , and lastName with variables. Best Practices Use camel case for variable names (e.g., firstName ). Ensure variable names are meaningful to enhance code readability. Utilize the More Details feature to view the values used during tests. By implementing these practices, we can significantly improve the organization and efficiency of our test scripts. In the next lesson, we will continue to explore further optimizations.
Video Transcript
In this section, we begin working on reusability and optimization of our current tests that we already created so far. And one of the first step to improve reusability is using variables. So variable is the mechanism where you can store the certain values into the entity called variable and then reuse this variable several times later in the script. This way you can remove and reduce the duplication of the certain values inside of the script. And in this lesson, I will show you how to do this in the test trigger. So let's get into it. This is the test that we have created so far. And let's open the date picker tests, quick edit, and here we go. So in the date picker test, we're selecting May 17th date right here. And we were using 17 multiple times during the script. Here, here, number three, and two assertions using 17 as well. So if we need to change this date to, for example, 18 or something, we need to update this value in five different places. So it's not optimal at all and time consuming. It would be much nicer to save this value into the variable and then just use this variable which stores this value for us. So how to do this in test trigger? It's very easy. You need to provide the prompt like this. Save, then the value that we wanna save, which is 17, and as, and provide the name, how you want to save the variable, dayToSelect, something like this. So the variable name should be meaningful enough and descriptive enough. So just by reading the variable, you understand what this value is related to. So in our example, we want to select the day. So this value will represent dayToSelect. And now we can use this variable, dayToSelect, later inside of our script. So how would we do this? So if page contains exactly stored value, dayToSelect, to the left of exactly one. And we replace everywhere, store value, dayToSelect, here and here. And now we change the value 17 over here and just calling the variable dayToSelect inside of the prompt. But that's not it. How about this? So we have also May 17, 2024, have to be as part of the assertion. Unfortunately, we cannot use the same type of format, stored value and variable name, because this is already in double quotes, and this is already a string. So we need to parametrize our string using the variable. The way how you would do this is this way. So we replace this with a dollar sign and curly braces and provide the value of the variable. And also the prompt have to be slightly updated to string with parameters. Just like that. And here we also need to update as well. Check that input value string with parameters and 17 we replace with the variable. All right, I think that's it. So let's just run this test and it should work exactly as it worked before. All right, test pass successfully and validation pass successfully with May 17 is selected. So let's quickly review this one more time. So we saved the value 17 as a variable dayToSelect and then we used the variable dayToSelect five times inside of the script, replacing the hard-coded values of 17. So let me show you one more example. And in the meantime, take a look at this banner over here. There is a new version of test trigger available. One of the beauty of the test trigger that it will be automatically kept for you up to date. So all you need to do is just to reload the browser and the latest version of the test trigger will be available to you. All right, moving on. So the second example, let's open table test scenarios. Table test scenarios, this one, and let's refactor this using the same approach. So here in the test table, we were using the column ID with a value three, and we were using it several times over here. And also first and last name, also hard-coded and then we're validating it right here. So let's replace those things with variables as well. And I need to create three variables. So save three as column ID. Save John as first name and save Smith as last name. And by the way, look at the type of the syntax that I'm using. I'm using the camel case, which means that the first letter start with the lowercase and each next letter for the variable name or the each next sentence of the variable start with the capital one. So your variable have to be a single string without using spaces or something like this. So first name and last name. And now we can replace the values inside of the prompt using the variables. So equals to stored value, column ID. And the same here. I update it here and here. And for the first and last name, the same thing. So John enter stored value. First name. And I need to replace it in the assertion. Assertion, so the equals to stored value, first name. And the same thing for the Smith, last name. Enter stored value, last name. And I'm replacing it in the assertion as well here. All right, that's it. So we replaced three hard-coded values from the prompts using the variables. Column ID, first name, and last name. Let's rerun this test and see if it is working as before. All right, and test pass successfully. And let me show you one more thing. When you are looking through the screenshots, for example, and when you look at this test and reading the description. So from the description, you not always can tell which values were used, for example, for first name. And in order to figure this out, you can click More Details. In here, you can find the values that were actually used by TestRigger during the test. So you can see John Smith was used during this step. This is also a reference point. All right, so let's quickly summarize what we did in this lesson. Open the date picker. So variables is a great way to remove the duplication and organize the code, to remove the repetitive operations of hard-coded values. The way how you would save the value in the variable is use a save, then the double quotes the value that you want to save, and as, and name of the variable. Better to provide the variable names in the camel case format. Also, if you want to use variable as a part of the other assertion string or any other string, you need to update the string with string with parameters. And inside of the string, use $ and the variable name. All right, that's it, guys, and see you in the next lesson.