Optional Step in QTP/UFT with Example
โก Smart Summary
Optional steps in QTP and UFT One let a run session continue when a control simply is not there. The tool skips the step, logs a message, and finishes the test without recording a failure.
What is Optional Step in QTP/UFT?
An optional step is a step that is not necessarily required to successfully complete a run session.
During a run session, if the object of an optional step does not exist in the application QTP bypasses this step and continues to run the test.
A login window is the classic case. An application may prompt for a user name and password on one run and remember the credentials on the next, so the login steps are needed sometimes and not others. Marked optional, those steps stop breaking the run on the days the prompt never appears.
At the end of the run session the tool reports the step it did not execute, so the skip is visible in the results rather than silent. The step still does not fail the run.
โ ๏ธ Version note: The product described here shipped as HP QuickTest Professional, was renamed Micro Focus Unified Functional Testing, and is sold today as OpenText Functional Testing (UFT One). Optional steps behave the same in every one of those releases.
How to Set a Step as Optional
To set a step as optional in keyword view right click on the step and select Optional Step.
Alternatively, you can directly write the keyword “OptionalStep” preceding a statement to make it optional.
The syntax is the keyword, a full stop, and the statement you already have. Nothing else about the line changes.
OptionalStep.StatementToMakeOptional
Applied to a real step, the browser dialog below is clicked when it appears and skipped when it does not.
OptionalStep.Browser("Browser").Dialog("AutoComplete").WinButton("Yes").Click
The same prefix works on any statement, including one that selects from a list.
OptionalStep.Browser("Mercury Tours").Page("Find Flights").WebList("depart").Select "Paris"
Both routes produce the same result. The Keyword View menu is quicker while building a test, and the keyword is easier to apply in bulk once the script is written in the Expert View.
Following video describes an OPTIONAL STEP and how it can be used.
Click here if the video is not accessible
Default Optional Steps in QTP and UFT One
Some steps never need marking. During a recording session the tool automatically treats steps that open a small set of standard dialog boxes as optional, because those windows are known to appear unpredictably.
| Dialog box or message box title bar |
|---|
| AutoComplete |
| File Download |
| Internet Explorer |
| Enter Network Password |
| Error |
| Security Alert |
| Security Information |
| Security Warning |
| Username and Password Required |
Because these are handled for you, a recorded script that clicks through a security prompt usually replays cleanly on a machine that never shows it. Any dialog outside this list has to be marked by hand.
When an Optional Step Still Fails
An optional step guards against one thing only: the object being absent from the application. It does not guard against the object being absent from the object repository.
The run therefore still fails if the tool cannot find the optional step object in the repository. That usually happens for one of two reasons:
- The object was renamed in the test or component but not in the repository, so the two no longer match.
- The object was deleted from the repository while the step that uses it was left in the script.
Two smaller traps are worth knowing. Marking a step optional does not make its checkpoint optional in any useful sense, because a skipped step produces no value to check. And an optional step suppresses a failure rather than reporting a condition, so overusing it can hide a genuine regression behind a run that still passes.
Optional Step vs Conditional Statement vs Recovery Scenario
Optional steps are the lightest of three ways to handle something that may or may not appear. The right choice depends on how much control the situation needs.
| Approach | How it works | Best for |
|---|---|---|
| Optional step | The tool skips the step when the object is missing and logs a message | A single control that is sometimes absent, such as a login prompt |
| Conditional statement | Your VBScript checks existence first and branches | A block of steps that must run together, or a decision the report should record |
| Recovery scenario | A trigger fires and runs a recovery operation at any point in the run | Unpredictable pop-ups and crashes that can interrupt anywhere |
A useful rule of thumb: use an optional step when the missing control is expected and harmless, a conditional statement when the script needs to do something different as a result, and a recovery scenario when the interruption could arrive at any moment rather than at one known line.
