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.

  • ๐Ÿ”˜ Purpose: Handle controls that appear only sometimes, such as a login prompt the application may have remembered.
  • โ˜‘๏ธ Two ways to set one: Right-click the step in Keyword View, or prefix the statement with the OptionalStep keyword.
  • โœ… Syntax: OptionalStep followed by a full stop and the normal statement, with no other change to the line.
  • ๐Ÿงช Automatic cases: Steps that open certain standard dialog boxes are treated as optional without any action from you.
  • ๐Ÿ› ๏ธ The limit: A missing object in the application is skipped; a missing entry in the object repository still fails the run.
  • ๐Ÿ“Œ Alternatives: Conditional statements and recovery scenarios cover the cases an optional step cannot.

Setting a step as an optional step in QTP and UFT One

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.

FAQs

No. The run continues and finishes normally. A message naming the skipped step appears in the run results, so the skip is recorded and visible, but it is not counted as a failure.

No. The keyword applies to one statement at a time, so each line needs its own prefix. When several steps must run or be skipped together, wrap them in a conditional statement instead.

The tool marks the row so the step reads as an optional step in the Item column. Switching to the Expert View shows the same line carrying the OptionalStep prefix ahead of the statement.

Because the object is missing from the object repository rather than from the application. Renaming an object in the script without renaming it in the repository, or deleting the repository entry, produces exactly this error.

Machine learning models cluster run history to separate genuinely broken steps from ones that fail only occasionally, then suggest which lines deserve an optional marking, a wait, or a repaired locator instead of a blanket retry.

Copilot happily prefixes a statement once it has seen the pattern in the file, which makes bulk edits fast. It cannot judge which steps should be optional, so review each suggestion against the actual behaviour of the application.

Not quite. A skipped step is one you disabled and it never runs. An optional step still runs whenever its object is present, and is bypassed only on the runs where the control does not appear.

Yes. The behaviour is identical: the object is looked for, the step is bypassed when it is absent from the application, and a message naming the skipped step appears in the run results at the end.

Summarize this post with: