How can I test a function that contains a "waitFor" function or popup dialogs without requiring external user input?

36 views (last 30 days)
I am testing a GUI function in my code. Within my test class, I use Java functions to simulate mouse clicks, which trigger a popup dialog. However, the function execution pauses at the waitFor line, requiring me to manually click a button to close the dialog before the code can proceed. How can I automate this process entirely?

Accepted Answer

Jianfei
Jianfei on 23 Jan 2025 at 1:48
Edited: Jianfei on 31 Jan 2025 at 13:34
I found a perfect solution to this issue. I've made this answer the accepted one as it can be implemented across all MATLAB versions.
In the test function:
function testcase1(testCase)
t = timer;
t.StartDelay = 2;
t.TimerFcn = @pressESC;
start(t)
% Call your test function below that will trigger a dialogue. Code execution
% will be stuck inside that function if without external input.
end
You can control mouse and keyboard in the timer callback. The example is pressing ESC.
function pressESC(~, ~)
disp('Pressing ESC')
r = java.awt.Robot;
r.keyPress(java.awt.event.KeyEvent.VK_ESCAPE);
pause(0.1);
r.keyRelease(java.awt.event.KeyEvent.VK_ESCAPE);
end
If you need to click a button, set a tag in your button.
function clickBtn(tagName)
% Get Java button object
hFig = findall(0, 'tag', tagName);
jBtn = findjobj(hBtn);
% Get central point of the button
p = jBtn.getLocationOnScreen;
x = p.x + jBtn.getWidth / 2;
y = p.y + jBtn.getHeight / 2;
% Mouse left click
r = java.awt.Robot;
r.mouseMove(x, y);
pause(0.1)
r.mousePress(java.awt.event.InputEvent.BUTTON1_MASK)
pause(0.1)
r.mouseRelease(java.awt.event.InputEvent.BUTTON1_MASK);
end
If you have a sequence of dialogs, create another timer within the timer callback function to trigger another action. Use disp to confirm whether you have set the correct waiting time for timers and make sure all actions are in the correct order.

More Answers (2)

Madheswaran
Madheswaran on 18 Nov 2024
Hi,
Starting from MATLAB R2024b, you can programmatically interact with alert and confirmation dialog boxes using 'chooseDialog' and 'dismissDialog' methods. They work with both modal and non-modal dialogs and can automatically select options or close dialogs without manual intervention.
For more information and examples, refer to the following documentations:
  1. dismissDialog - https://mathworks.com/help/matlab/ref/matlab.uitest.testcase.dismissdialog.html
  2. chooseDialog - https://mathworks.com/help/matlab/ref/matlab.uitest.testcase.choosedialog.html
Hope this helps!

埃博拉酱
埃博拉酱 on 18 Nov 2024
Possible ideas:
  1. Create a waitfor function for testing in the current directory before testing to see if it can mask the waitfor of MATLAB.
  2. Create an environment or a global variable with a special name (such as YourAppName_Debug) before the test, check whether the environment variable exists in the test objective function, and skip the dialog box if it exists.
  3. Add an undocumented optional parameter to the function. This parameter is only offered by the developer in the test environment.

Categories

Find more on App Building in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!