Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » Jubula » How to test SWT-Messageboxes(Object Mapping isn't working though it's a standard SWT-widget)
How to test SWT-Messageboxes [message #1228531] Tue, 07 January 2014 12:49 Go to next message
Lars Friedland is currently offline Lars FriedlandFriend
Messages: 8
Registered: January 2014
Junior Member
Hi everybody!

I'm doing automated tests with Jubula for my Java-Application (SWT).
Everything works great except from testing MessageBoxes (org.eclipse.swt.widgets.MessageBox). I can neither map any GUI-objects in the MessageBox-window (no green squares are shown) nor the window itself.
So far my only idea was to use key combinations to close the MessageBox but this is only a poor workaround and no solution because I really need to test things like if the text shown in the MessageBox is the right one and so on...

So is there any possibility to test a SWT-Messagebox with Jubula?

Thank's for your help!!
Re: How to test SWT-Messageboxes [message #1229770 is a reply to message #1228531] Fri, 10 January 2014 07:15 Go to previous messageGo to next message
Alexandra Schladebeck is currently offline Alexandra SchladebeckFriend
Messages: 1613
Registered: July 2009
Senior Member
Hi,

I'm assuming from the description that the message boxes contain native components from the operating system. These can't be tested with the "normal" actions because the components aren't Java. In most cases, such native dialogs are e.g. file choosers and the aim in the test isn't to test their functionality, but to use them as part of a larger workflow. For that reason, the actions "external key combination" and "copy text to clipboard" followed by "ctrl+v" are generally enough.

So to answer your question - no, you can't *test* the message box, but you can use it / deal with it. Without knowing what kind of dialog it is, I can't really say whether I think it is something that really needs to be tested, or whether it being filled out is just a necessary part of the test. Maybe there's somewhere else in the GUI you can check that the action you did in the message box worked, e.g. if you want to check that the name you used to save a file was correctly used, perhaps the name is shown in the UI after saving. Or you can use execute external command to check the file system with a small script.

There's some additional information on this on the testing portal as well [1], [2].

Hope that helps,
Alex

[1] FAQ about native dialogs
[2] Module examples (look at the "Using the clipboard to deal with native dialogs" example)
Re: How to test SWT-Messageboxes [message #1229801 is a reply to message #1229770] Fri, 10 January 2014 08:44 Go to previous messageGo to next message
Lars Friedland is currently offline Lars FriedlandFriend
Messages: 8
Registered: January 2014
Junior Member
Hey Alex,

thank's for your advice! Unfortunately in my case I need to check the text in the Messageboxes to be sure that the user get's the right error message...
Do you know any other SWT-libriary that doesn't use native components for dialogs? Or is there only the possibility to build a "custom dialog" from SWT components?

[Updated on: Fri, 10 January 2014 08:46]

Report message to a moderator

Re: How to test SWT-Messageboxes [message #1229810 is a reply to message #1229801] Fri, 10 January 2014 09:15 Go to previous messageGo to next message
Alexandra Schladebeck is currently offline Alexandra SchladebeckFriend
Messages: 1613
Registered: July 2009
Senior Member
Ah, I see. In terms of other libraries and the development part, I'm afraid I can't help. For the test, there may be some workarounds:
- you could take a screenshot and use an image comparison tool (again, added to the test via execute external command), or you could take a screenshot and then check it manually as part of the test analysis process.
- you could press "ctrl+a" (with external key combination) to copy all of the text from the message box, then paste it somewhere in your application and check text on it

These aren't necessarily pretty, but if they help you, then maybe that's enough.

Best regards,
Alex
Re: How to test SWT-Messageboxes [message #1229834 is a reply to message #1229810] Fri, 10 January 2014 10:17 Go to previous messageGo to next message
Lars Friedland is currently offline Lars FriedlandFriend
Messages: 8
Registered: January 2014
Junior Member
Probably it's the cleanest and fastest solution to implement a class for dialogs that uses only SWT components. Nevertheless thank you for your fast help and the good advice! Wink

Regards,
Lars
Re: How to test SWT-Messageboxes [message #1230959 is a reply to message #1228531] Mon, 13 January 2014 13:07 Go to previous messageGo to next message
Lars Friedland is currently offline Lars FriedlandFriend
Messages: 8
Registered: January 2014
Junior Member
So I thought if somebody is facing the same problem here's the code for my "custom SWTDialog"-class. Maybe you could save some time using this as a template Smile

It's possible that this code contains errors and probably is not perfect! Feel free to do whatever you want with it. Wink

package xxx.xxx.xxx;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

/**
 * 
 * @brief Custom dialogs build only with SWT components
 *
 */
public class SWTDialog {
	private static Shell shell;
	private static Label lblMessage;
	private static Button btnLeft;
	private static Button btnRight;
	private static int response;
	
	public SWTDialog(int style) {
		Display display = Display.getCurrent();
		shell = new Shell (display);
		shell.setBounds(0, 0, 413, 158);
		btnLeft = new Button (shell, SWT.PUSH);
		btnLeft.setBounds(207, 82, 87, 25);
		btnRight = new Button (shell, SWT.PUSH);
		btnRight.setBounds(300, 82, 87, 25);
		lblMessage = new Label (shell, SWT.PUSH);
		lblMessage.setBounds(67, 13, 320, 63);
		Label lblImage = new Label (shell, SWT.NONE);
		lblImage.setBounds(10, 10, 51, 43);
			
		if (style == SWT.ICON_ERROR) {
			//Error icon
			lblImage.setImage(display.getSystemImage(SWT.ICON_ERROR));
			shell.setText("Fehler");
			btnLeft.setText("OK");
			btnLeft.setBounds(150, 82, 87, 25);
			btnRight.setVisible(false);
		} else if (style == SWT.ICON_WARNING) {
			//Warning icon
			lblImage.setImage(display.getSystemImage(SWT.ICON_WARNING));
			shell.setText("Warnung");
			btnLeft.setText("Ja");
			btnRight.setText("Nein");

		} else if (style == SWT.ICON_INFORMATION) {
			//Info icon			
			lblImage.setImage(display.getSystemImage(SWT.ICON_INFORMATION));
			shell.setText("Information");
			btnLeft.setText("OK");
			btnLeft.setBounds(160, 82, 87, 25);
			btnRight.setVisible(false);
		}
	}
	
	public int open() {
		shell.open();
		
		btnLeft.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (btnLeft.getText() == "OK") {
					shell.close();
					response = SWT.OK;
				} else if (btnLeft.getText() == "Ja") {
					shell.close();
					response = SWT.YES;					
				}
				
			}
		});
		
		btnRight.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				shell.close();
				response = SWT.NO;
			}
		});		
		
		Display display = Display.getCurrent();
		while (!shell.isDisposed()) {
	      if (!display.readAndDispatch()) {
	        display.sleep();
	      }
	    }
		return response;
	}
	
	public String getMessage() {		
		return lblMessage.getText();
	}
	
	/**
	 * 
	 * @brief Sets Message of Dialog and automatically breaks lines
	 */
	public void setMessage(String sMessage) {
		int maxLength = 52; //max number of characters per line/row
		int maxRow = 4; 
		
		if(sMessage.length() > maxLength) {
			int iCount = 0;
			int iString = 0;
			int iRow = 0;
			boolean bTooLong = false; //Indicator for line breaking
			String sHelp;
			String[] sRow = new String[maxRow];   //String array that contains one line/row per item
			String[] split = sMessage.split(" ");
			sMessage = "";

			while (iCount < maxLength && iString < split.length) {
				if (sRow[iRow] == null) {
					sRow[iRow] = split[iString] + " ";
					iString++;
				} else {					
					sHelp = sRow[iRow] + split[iString] + " ";
					if (sHelp.length() > maxLength) {
						bTooLong = true;
					} else {
						sRow[iRow] = sHelp;
						bTooLong = false;
						iString++;
						iCount++;
					}
				}
				if (bTooLong == true){ 
					iCount = 0;
					iRow++;
					if (iRow >= maxRow) {   //adds "..." to end of last line that can be displayed
						sRow[maxRow-1] += "...";
						break;
					}
					sRow[iRow-1] += "\n";
					bTooLong = false;
				}
			}
			for(int i = 0; i < maxRow ; i++) {
				if(sRow[i] == null) break;
				sMessage += sRow[i];
				lblMessage.setText(sMessage);
			}
		} else {
			lblMessage.setText(sMessage);
		}

	}
	public void setText(String sTitle) {
		shell.setText(sTitle);
	}
}

[Updated on: Tue, 14 January 2014 12:45]

Report message to a moderator

Re: How to test SWT-Messageboxes [message #1231668 is a reply to message #1230959] Wed, 15 January 2014 07:17 Go to previous message
Alexandra Schladebeck is currently offline Alexandra SchladebeckFriend
Messages: 1613
Registered: July 2009
Senior Member
Thank you so much for the update! Smile
Previous Topic:Autagent running systems
Next Topic:Logo and Icons recognition
Goto Forum:
  


Current Time: Fri Apr 26 01:16:27 GMT 2024

Powered by FUDForum. Page generated in 0.03476 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top