Pre-defined Variables and Text Extraction | Bondar Academy
Course: Mastering testRigor
Module: Reusability and Optimization
Instructor: Artem Bondar
Lesson Summary
This lesson focuses on how to grab text values from a web page and save them into variables for later use in tests. It also covers predefined variables available in the test trigger. Key Concepts Variable Creation: Instead of hardcoding values, you can grab the text from buttons or other elements and store them in variables. Assertions: Use stored variables to make assertions in your tests, ensuring that dynamic values match expected outcomes. Example 1: Button Text Assertion To compare button texts: Navigate to the form layouts page. Grab the value from the button next to "Remember Me" using the prompt: grab value from button and save it as button text . Make an assertion to check if button text contains "submit". Run the test to validate the assertion. Example 2: Grabbing Values by Template To grab a username from a table: Navigate to the smart table page. Use the prompt: grab value by template to find a username starting with "@" followed by three letters. Make an assertion to check if the stored value equals the expected username. Predefined Variables Utilize predefined variables such as username and password for common tasks. For date-related tests, variables like today day of month can simplify date selection in scripts. In summary, use grab value to store text in variables, utilize templates for partial matches, and leverage predefined variables for efficiency in date handling.
Video Transcript
In this lesson I will show you how you can grab the text values from the page and save those values into the variables and then you can reuse those variables later in your test. And also we will talk about predefined variables available in the test trigger. So let's get into it. This is our test application form layouts page and let's imagine that we would like to make an assertion that text of this button is equals to this button. So we don't want to hardcode the value of submit. So for example the text of this button have to be changed time to time and we don't care what's the text on this button we care that those should be equal. And for us to not update the test every time with a hardcoded value we just can grab the value from this button and compare that this value is the same as this button. And this is what we're gonna do in this lesson. So I will create a new test. And first of all we need to navigate to form layouts page. Click forms and then click form layouts. All right the first step we need to save the value of the button to the right of remember me to the variable. How would you do this in test trigger? The prompt would look like grab value from button then relative location to the right of text remember me and save as for example button text. Well that's pretty much it. So we grabbing the text from the button and saving to the variable with a name button text. And then we can make for example assertion. Check that stored value stored value button text contains submit. So we will validate that the value that we grab from the button is actually a submit. Let's run this test. All right and the test passed successfully. And now the last step that we need to do is to make a validation that this button also has the same text that we grabbed on the first step. So we just add a second assertion. Check that button below what's the relative location check me out button below check me out contains stored value and the name of the variable which is a button text. All right and let's run this one more time. And test passed successfully. Let's check it out. We can see that validation of the submit button was successful. So using this simple script we grab the values from the first submit button save it into the button text and validate into the second button below check me out contains exactly the same text. Let me show you one more example how you can grab values by particular templates. So if we go to smart table page. Here we go. So let's say that we want to grab the username from this table and this username have to be three letters. That all we care about. And this username can be absolutely different. So we want to tell test trigger hey grab for me username or not username grab for me a text which starts with add sign and then three different letters. So you can create a template and test trigger will look for most closest match for this template and grab the value which is the closest to this template. So let me show you how to do this kind of stuff. So we need to navigate to where is it tables and data tables and data and navigate to smart table. Okay and now I would like to grab the first available value which has that add sign. This is the way how you would do it. So grab value by template and then you provide that how template should look like. So it should start for me with add because I want to grab the values only which start from add and then any other text which is available. And I would like that my template and my username that will be taken will have only three letters after that. So this template will not pick up for example Twitter or snow because this one has four letters. This template will pick either MDO, FAD or N something that has three letters. So my expectation that this template should pick up this value because this is the first one available in the table. And then I provide where in the context of table and save as and provide any variable name. Three letters username for example. And let's make the assertion that this worked successfully. Check that stored value three letters username equals to what it should be MDO. All right let's hit let's try to run this test. And test passed successfully. So the value MDO we grab this value by template and made the assertion. All right so let's make a quick summary of this section. So if you want to grab the value from the page you use grab value then provide the relative location and save as a variable name. And then you can use this variable normally calling stored value and variable name. You can use this inside of the other test scripts or inside of the assertions. And now let me show you something one more thing which is related to the test trigger is predefined variables. So I go to documentation and here I would navigate here to using variables. Scroll a little bit down and here is a list of predefined variables available for you. So there are two main variables that you can use username and password. So those username and password would be useful if you save those in the settings of test trigger. In this case you can you just use those variables without defining them they will be available. And some of the variables home domain home file and home prefix which is also can be useful and related to the URL of your application. But most of the variables are related to working with the date. And let's say I use some of the variable inside of our date picker test. For example I will pick this one today day of month. In this case our test will automatically will select the current day inside of the calendar and I don't need to deal with any other variables or something. The predefined variable will handle this for me. So for example I copy this today day of month going back to test and here is our date picker test. This one quick edit and instead of using 17 for example data select I want to select today day of month and what I'm using is just here. Data select replace with this this this and here data select and day to select. That's it. I don't need this declaration anymore because I will use a predefined variable which holds the current day of the month. And let's just run this test. All right test passed successfully. So today is April 26th and let's open the test. And the test selected is May 26th. It is expected it's selected May 26th because inside of our script we actually selecting May. But for the day it's selected the current day which is 26. You can also replace a hard-coded May for example with the today month. For example with this one today month short and it will also will work. So replacing the May with a variable in this case the script will automatically will select the current date and when you run the script tomorrow the day after tomorrow or the next month it will always select the current date. So this type of functionality is very useful when you work with the calendars. All right so let's quickly summarize what we did in this lesson. When you want to save the text from the page you can use the prompt grab value and then save this value into the variable and then later in your code you can use this variable in your script. Also you can grab the value by the template when you are looking for the partial match of the value that you are looking for. In this case you provide grab value by template and provide this value replacing by the stars like wildcards the values that are not necessary and any match can happen. Also when you work with date and time very useful feature is the predefined variables which you can use available in the test trigger without declaring them inside of your script. All right that's it guys and see you in the next lesson.