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

39 次查看(过去 30 天)
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?

采纳的回答

Jianfei
Jianfei 2025-1-23,1:48
编辑:Jianfei 2025-1-31,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.

更多回答(2 个)

Madheswaran
Madheswaran 2024-11-18
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!

埃博拉酱
埃博拉酱 2024-11-18
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.

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

标签

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by