Ranorex中如何按条件退出录制模块并执行后续模块
Solution: Early Module Exit When Element Exists (No Test Failure)
Got it, let's adjust your code so you can exit the current module early without marking the test as failed when the Deconnexion element is detected. Here's the breakdown:
How It Works
- Check existence without failure: Instead of using
Validate.Exists()(which throws an error if the element isn't found), we use theExists()property of the element info object. This returns a simple boolean (true/false) so we can conditionally act without triggering a test failure. - Conditional exit: If
Deconnexionexists, we log the result and exit the module immediately—skipping all remaining steps and moving to the next module cleanly. - Proceed normally: If the element doesn't exist, we run your original module steps as intended.
Modified Code Implementation
// Check if Deconnexion element exists (no test failure if it doesn't) bool deconnexionIsPresent = repo.Internet.abc.Connexion.DeconnexionInfo.Exists(); Report.Log(ReportLevel.Info, "Validation", $"Validating existence of 'Internet.abc.Connexion.Deconnexion': {deconnexionIsPresent}", repo.Internet.abc.Connexion.DeconnexionInfo, new RecordItemIndex(0)); if (deconnexionIsPresent) { Report.Log(ReportLevel.Info, "Module Exit", "Deconnexion element detected—exiting current module early to proceed to next module."); // Exit the current module (use 'return;' if this is a method/module function) return; } // These steps only execute if Deconnexion does NOT exist Delay.Milliseconds(0); Report.Log(ReportLevel.Info, "Mouse", "Mouse Left Click item 'Internet.abc.Connexion.Httpsxyz' at Center.", repo.Internet.abc.Connexion.Httpsxyz, new RecordItemIndex(1)); repo.Internet.abc.Connexion.Httpsxyz.Click(); Delay.Milliseconds(200);
Key Notes
- Why replace Validate.Exists?:
Validate.Exists()is built to fail the test when the target element isn't found. Using theExists()property lets us check presence without triggering a failure, which aligns with your requirement. - Exiting the module: The
return;statement works if this code lives inside a method (common in frameworks like Ranorex). If your setup uses a different structure, replace this with your framework's specific way to skip remaining steps (e.g., setting a skip flag or calling a framework-provided skip method). - Logging clarity: The extra log entry explicitly states why the module exited early, making test reports easier to debug later.
内容的提问来源于stack exchange,提问作者ae2019
相关产品推荐
相关产品推荐

