Changing the scope of a variable to be available within a Method
After reading the value from a dropdown, I wanted to use that piece of information for a comparison test in another part of the Test Case. That's not a problem, except that I want to use the variable within a defined Method within the test case. I've done this sort of thing before and passed the needed values to the method. The Method didn't need any parameters, so I looked for another way to make that variable more "global" in scope.
Turns out this can be done using @Field
The first step is to import the correct library
import groovy.transform.Field
From there, "flag" the corresponding variable and change it's scope.
@Field String quoteBranchName
quoteBranchName=allQuoteBranchesList[0]
log.logWarning('The listed Branch in the quote is: ' + quoteBranchName)
That variable can now be referenced within a Method defined within that same Test Case
def addInventoryItem() {
log.logWarning('The listed Branch in the quote is: ' + quoteBranchName)
if (inventoryLocations.contains(quoteBranchName)==false){
log.logError("The Inventory Item does not match the Branch Location")
}
}
This is the first time I've needed to make this kind of reference. The verdict is still out on whether this is "cheating" or not :), but it gets the job, so I'm going with it.
Other articles of interest:
- Using Sets to fill in Form Details with Katalon Studio
- Conditional Statements – IF .. ELSE IF in Katalon Studio
- Creating and Calling Methods for Test Cases in Katalon Studio
- Output status messages and test information by writing to the Log File Viewer in Katalon Studio
- Global Variables – How to define your test environment
- Reading text of the currently selected value from a dropdown list
- Show the currently executing Test Case both before and after it runs
- Parsing Strings in Katalon – Split, Substring and Readlines
- Entering and reading text GetText, SetText and SendKeys in Katalon Studio
- Working with Tables using the Selenium WebDriver and Katalon Studio
Reading text of the currently selected value from a dropdown list
I was recently working on a test case where I needed to use the default value from a dropdown. For my case, I wanted to know the default location of the user. The dropdown value is set through Javascript and a query. Since it has a default, I didn't want to set it, I wanted to know what that default was. This turned out to be a little more problematic than I thought.
My first attempt was to use .getAttribute
which has worked for other fields.
String branchLocation = WebUI.getAttribute(findTestObject('Dropdown Location'),'value')
returns
c6f25c57-47a7-4ae3-b269-a57567faa23f
which is a GUID and can't be translated into anything I can work with.
I then tried it with getText
which does work, but brings back all the options available in the dropdown, not just the currently selected one.
String branchLocation = WebUI.getText(findTestObject('Dropdown Location'))
This returns
Location #1
Location #2
Location #3
Location #4
Etc
This is better, but is clearly more information than I want. All I want is the first entry. With that in mind, the String
is turned into a List
with a split
on the CRLF that exists at the end of each line. This gives one entry for each index of the List
String allBranchLocations = WebUI.getText(findTestObject('Dropdown Location'))
List allBranchesList=allBranchLocations.split("\\r?\\n") //Remove CRLF from each dropdown entry
String branchName=allBranchesList[0]
With that little conversion, branchName
contains the first item from the dropdown, which is the default value. This can now be used for my comparison. This may not be the best way to get the first item, or perhaps the most reliable, but it works situation and is easier than some of the other solutions I saw offered up.
Other articles of interest:
- Parsing Strings in Katalon – Split, Substring and Readlines
- Using Sets to fill in Form Details with Katalon Studio
- Simple wildcard searches for pattern matching
- Changing the scope of a variable to be available within a Method
- A Custom Keyword to Verify a List of Product Categories
- Entering and reading text GetText, SetText and SendKeys in Katalon Studio
- Setting up a repeatable Search Method in Katalon Studio
- Creating and Calling Methods for Test Cases in Katalon Studio
- Getting started with Katalon Studio
- Katalon Studio – Creating Objects
Getting started with Katalon Studio
You don't have automate an entire website to gain a benefit from automation. Even for yourself, there are plenty of ways Katalon Studio can help you save time. One of the main goals of automation is to perform repetitive tasks and emulate the same functions you do on a daily basis. And on one of the best places to start is filling in forms.
Entering form data is by no means a difficult job, but it's tedious and after a couple of passes, you really don't care what you type as long as the field isn't blank. Plus, it can be a waste of time. If it takes 5 minutes to fill in a form accurately and wait for it to save, that's roughly 10 forms an hour. And that's an hour that can be invested in something more meaningful.
With some simple automation code, filling in the entire form can be reduced to 30 seconds. Plus, it will be filled out correctly each time with predictable data you can search for and confirm exists. So instead of spending 5 minutes for each form, it would be possible to create 10 contacts in 5 minutes, or 10 times the amount of work with 1/10th the effort. And that savings of nearly 45 minutes can be put toward other projects like training, self-study, or for improving the automation code itself.
Filling in form data is one of the easier tasks to accomplish with automation. In most cases, the objects will follow a similar naming pattern such as:
css=input[name="contact_email"]
//div[@id='add-Contact']/form/input[7]
It would be a matter of copy/paste to change the xpath to the correct name, or use a variable for multiple inputfield objects.
The same should be true for entering text:
WebUI.setText(findTestObject('Object Location/inputfield-Contact-Contact Email'), 'user@domain.com')
After creating one SetText entry, copy/paste the rest, change the object and entered text and the job is done.
With a little bit of prep work, it would be possible to spend an hour or so and put together a script that will give a return on the time investment after the first couple of runs. And keep giving back time every time it's run.
Running the Test Case to create a new contact is something I run regularly. I can make dozens of entries while working on something else. That is a very big win in my opinion.
The core of the script could be some as simple as:
WebUI.setText(findTestObject('Object/inputfield-Contact-First Name Last Name'), 'Bob Smith')
WebUI.setText(findTestObject('Object/inputfield-Contact-Role or Title'), 'Customer Title)
WebUI.setText(findTestObject('Object/inputfield-Contact-Contact Phone'), '5552221112')
WebUI.setText(findTestObject('Object/inputfield-Contact-Contact Mobile'), '5553331113')
WebUI.setText(findTestObject('Object/inputfield-Contact-Contact Office'), '5554441114')
WebUI.setText(findTestObject('Object/inputfield-Contact-Contact Email'), 'user@domain')
WebUI.setText(findTestObject('Object/inputfield-Contact-Address Line'), '12 West Upper Court')
WebUI.setText(findTestObject('Object/inputfield-Contact-City'), 'Tempe')
WebUI.setText(findTestObject('Object/inputfield-Contact-State'), 'AZ')
WebUI.setText(findTestObject('Object/inputfield-Contact-Zip Code'), '85281')
WebUI.setText(findTestObject('Object/inputfield-Contact-Phone Number'), '3335551212')
WebUI.setText(findTestObject('Object/inputfield-Contact-Email Address'), 'user@domain.com')
WebUI.click(findTestObject('Object/btn-Contact-Save Button'))
Other articles of interest:
- Katalon Studio – Creating Objects
- Using Sets to fill in Form Details with Katalon Studio
- Filling forms with random numbers in Katalon Studio
- Calling Another TestCase in Katalon Studio
- Passing Object Names as String Variables in Katalon Studio
- Entering and reading text GetText, SetText and SendKeys in Katalon Studio
- Passing multiple variables to an object in Katalon Studio
- Katalon Studio – Manual View – The start of a test script
- Enter dates into a date picker for Chrome and Firefox
- Create a Dynamic Object at Runtime
Simple wildcard searches for pattern matching
For a search test I was working on, I needed to verify what was returned for the name of a warehouse location. Normally the .contains
command would be used, but for this test, I needed to check for multiple criteria. I need to check the warehouse is assigned to a certain coast and contains certain city values. The warehouse could be East or West with several city names listed afterward in any given order. For example, the warehouse location could look like:
String branchName="Warehouse East #585 Palm Beach, FL Charlottesville, VA Nampa, ID Charlottesville, VA"
String branchName="Warehouse West Los Angeles, CA, San Francisco, CA, San Diego, CA"
I want to verify "Warehouse East" is part of the text and I want to verify "Palm Beach" is in the text. Since these two strings are not next to each other and the city could appear anywhere on the line the .contains
will not work in this instance.
However, the .matches
command can be used which supports the wildcards, *
and ?
. The usage is the same as we see for filenames. For example *image*
matches all filenames with the word image such as image, images, fileimage
. The only difference is we have to "escape" the wildcard with the use of .*
With that in mind we can make the following command verify our chosen text strings exist in the returned result.
println(branchName.matches("Warehouse East .*Palm Beach.*"))
To use the single wildcard .?
we could write the line as:
println(branchName.matches(".?arehouse .?ast .*Palm Beach.*"))
This ignores the case for the W and E, so Warehouse East
is the same as warehouse east
It is also possible to use a more "regex" style and write the above line as:
println(branchName ==~/.?arehouse .?ast .*Plam Beach.*/)
This falls into the brute force category of pattern matching, but I have pretty simple needs and taking the time to write a custom parser isn't necessary and ins't in the cards.
Other articles of interest:
- Setting up a repeatable Search Method in Katalon Studio
- Reading text of the currently selected value from a dropdown list
- Reviewing the Execution Logs of Katalon Studio
- Getting the Length of a String and the Size of a List
- Parsing Strings in Katalon – Split, Substring and Readlines
- A Try Catch example in Katalon Studio
- Output status messages and test information by writing to the Log File Viewer in Katalon Studio
- Waiting for Elements to appear in Katalon Studio
- Wait For Alert, Verify Alert Present in Katalon Studio
- Securely storing passwords and login details with Set Encrypted Text in Katalon Studio
Some new tools to tackle the job in 2019
With 2019 practically upon us, I have some new tools to help with project management, task management, along with code and document organization.
Since there is plenty of manual testing to be done, I've brought in Pagico to help manage projects and 2Do, to help with task management and test management. I've got MWeb for note taking, DevonThink Office to organize and manage all my notes, Coderunner to help write code pieces and SnippetsLab to store code pieces I'm working on.
With Pagico, I can create lists, link documents, and group together the information I need for a given project. For example, I can enter due dates, store the links to Requirements and JIRA tickets, create reminders, track open tickets, and link to PDF files. In many respects it's my replacement for Freeter. I've tracked two projects so far with great success.
2Do is for task management, both for personal and project items. It's easy to create a list, set reminders, and even make a test plan without having to resort to Excel. To be honest, I've made one too many checklists using a spreadsheets and it's a terrible experience. 2Do is much easier to organize and structure.
DevonThink Office Pro is the command center of note taking and storage. It can create notes in a variety of formats as well as store documents like .doc, .xls, .pdf and pretty much everything else. Documents and topics can be tagged with keywords and linked together. Web pages can be imported and stored. To be honest, it's feature set is kind of massive. If you need to store information for a topic, this is the tool to handle it.
Coderunner 3 is a very nice tool for working on small code projects, whether they be Groovy, Java, Python or Shell Scripts. It's a very nice editor that keeps things simply and tidy. You can try out a new code idea in an IDE that takes a second to load rather than waiting for all the libraries and plugins to initialize for something like IntelliJ. It's very quick and nimble and I'm quite taken with it.
MWeb is great note taking and markdown tool. It runs very quickly, supports multiple themes as well as making your own, organizes documents into folders and allows multiple tabs. Documents can be exported in a variety of formats such as HTML, Rich Text, ePub, PDF, and Doc. DevonThink can also be used to track and index whatever notes you create.
You wouldn't ask a carpenter to make a fine table using nothing but a hacksaw and a screwdriver. I'm coming to the table with a variety of tools to get the job done and track what it took to complete.
Pagico
2Do
DevonThink Office Pro
Coderunner 3
SnippetsLab
Other articles of interest:
- An interesting dashboard app called Freeter
- MiniNote Pro for quick note taking
- RightNote on Sale for 50% Discount
- Dropbox + OneNote = Awesome
- AllMyNotes Organizer v3 for $9.97
- Scriver for Mac/Win 50% Off, Scapple for Mac/win 40% Off
- Boostnote for Code Snippets
- Out of OneNote and into Notebooks with Scrivner
- What is Katalon Studio? A Distro of Selenium, Groovy and Eclipse
- Get WinX DVD Ripper Platinum for FREE! Original Price $24.95
Recent Comments