Clear Filters
Clear Filters

uitest with questdlg - possible?

8 views (last 30 days)
Nick
Nick on 8 Jul 2022
Answered: Ravi on 29 Dec 2023
Hi,
I'm trying to implement a uitest for one of my dialogues. Everything worked well, until a pressing of a button, triggered confirmation dialog (questdlg with "yes"/"no" answers).
The test is stopping at this moment and waits for user interraction.
Is there a way to close this dialog with choosing of an answer option as part of uitest?
Out of curiousity I tried to use
tc.dismissAlertDialog(fig);
where fig is my object under test, which expectingly reported
Error using matlab.ui.internal.Driver/dismissAlert
Unable to find any alert dialog boxes in the figure window.
after I had to close this dialog manually.
EDIT: as alternative, is there a way to find in the code, that it runs within a TestCase? I.e. I can incorporate in production code check for test execution and not show the confirmation dialog, but provide answer immediately or read it from somewhere?
Ideally, if I can find which TestCase is executed, and read parameters from the TestCase!
Thank you!
Nick

Answers (1)

Ravi
Ravi on 29 Dec 2023
Hi Nick,
I understand that you are facing an issue in closing the dialog box without manual intervention.
To programmatically click a button in a dialog box during a test in MATLAB, you would typically use the matlab.uitest.TestCase class to interact with the UI components. However, uiconfirm dialog boxes are modal and block the MATLAB command line, preventing further commands from running until a user makes a selection.
Since you cannot directly interact with the uiconfirm dialog in an automated way using matlab.uitest.TestCase, you would need to create a custom dialog using uifigure and UI components (like uibutton) if you want to automate interaction with a dialog box. Once you have a custom dialog with buttons, you can use the press method of the matlab.uitest.TestCase object to simulate a button click.
Here's an example of how you might create a custom dialog and then simulate a button click:
function tester(data)
% Create a TestCase object for UI testing
testCase = matlab.uitest.TestCase.forInteractiveUse;
app = YourAppName;
pause(2);
% Create a custom dialog with buttons
dlg = uifigure('Name', 'Confirmation', 'Position', [100 100 250 150]);
btnYes = uibutton(dlg, 'Text', 'Yes', 'Position', [50 50 70 25]);
btnNo = uibutton(dlg, 'Text', 'No', 'Position', [130 50 70 25]);
% Pause to ensure the dialog is rendered before trying to interact
drawnow;
pause(0.5); % Pause for half a second
% Use the TestCase to press the 'Yes' button
testCase.press(btnYes);
pause(0.5);
% Additional testing code goes here
% Close the custom dialog and the app when done
delete(dlg);
delete(app);
end
In case, if you wish to pass the data from workspace into the app when testing, you can modify the function syntax as follows.
function tester(data)
% Code
% The data variable is passed as an argument from workspace
app.EditField.Value = data;
% Code
end
When calling this testcase from the command window, we need to specify the parameters as well.
tester(data)
Hope this solution helps.
Thanks,
Ravi

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!