Getting text from 'console' tab

WindowTester allows you to easily create and run unit tests for every GUI they build. It can also be used to generate system level tests.

Moderators: gnebling, Eric Clayberg, Dan Rubel, keertip, Phil Quitslund

Getting text from 'console' tab

Postby nsajjan » Thu Jan 28, 2010 7:38 am

Hi,
My RCP application, when it builds a project, spits out build result information into the Console tab. Using the WindowTesterPro 4.0.0 and eclipse classic SDK 3.5, I recorded the project creation and build. As a part of the test, I need to read the build output text in the console tab window to determine if the build was successful. Is there any other way that I can extract the build output text from the console tab?

I tried the below code in my recorded .java file but when I playback, it always returns null.

Code: Select all

String buildOutput = new CTabItemLocator("Console").getText(ui);
System.out.println(buildOutput);



Thanks,
nsajjan
 
Posts: 4
Joined: Thu Jan 28, 2010 7:26 am

Re: Getting text from 'console' tab

Postby Phil Quitslund » Thu Jan 28, 2010 10:50 am

Hey there,

In a future release we will provide a dedicated StyledTextLocator that will make this kind of assertion easier. In the meantime, take a look at this snippet. I hope it helps!

Code: Select all
package com.windowtester.test.eclipse;


import static com.windowtester.runtime.swt.locator.eclipse.EclipseLocators.perspective;
import static com.windowtester.runtime.swt.locator.eclipse.EclipseLocators.view;

import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;

import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.condition.WidgetShowingCondition;
import com.windowtester.runtime.locator.IWidgetLocator;
import com.windowtester.runtime.locator.IWidgetReference;
import com.windowtester.runtime.swt.UITestCaseSWT;
import com.windowtester.runtime.swt.locator.SWTWidgetLocator;
import com.windowtester.runtime.swt.locator.eclipse.ViewLocator;




public class ConsoleReadingSmokeTest extends UITestCaseSWT {

   

   private static final String CONSOLE_TEXT = "Hello console!";

   
   /* (non-Javadoc)
    * @see junit.framework.TestCase#setUp()
    */
   @Override
   protected void setUp() throws Exception {
      IUIContext ui = getUI();
      ui.ensureThat(view("Welcome").isClosed());
      ui.ensureThat(perspective("Java").isActive());
      ui.ensureThat(view("Console").isShowing());   
      
   }

   public void testConsole() throws Exception {
      
      doWriteToConsole();
      
      IUIContext ui = getUI();
   
      IWidgetLocator consoleLocator = new SWTWidgetLocator(StyledText.class, new ViewLocator("org.eclipse.ui.console.ConsoleView"));   
      ui.wait(new WidgetShowingCondition(ui, consoleLocator));
      
   
      IWidgetReference consoleRef = (IWidgetReference) ui.find(consoleLocator);
      String text = getText((StyledText)consoleRef.getWidget());
      assertEquals(CONSOLE_TEXT, text.trim());
      
   }


   private String getText(final StyledText widget) {
      final String[] text = new String[1];
      Display.getDefault().syncExec(new Runnable(){
         public void run() {
            text[0] = widget.getText();
         }
      });
      return text[0];
   }

   private void doWriteToConsole() {
      Display.getDefault().syncExec(new Runnable() {
         public void run() {
            try {
               writeToConsole();
            } catch (PartInitException e) {
               e.printStackTrace();
               fail(e.getMessage());
            }
         }
      });
   }


   private void writeToConsole() throws PartInitException {
      IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
      String id = "org.eclipse.ui.console.ConsoleView"; // IConsoleConstants.ID_CONSOLE_VIEW;
      page.showView(id);

      MessageConsole myConsole = findConsole(id);
      MessageConsoleStream out = myConsole.newMessageStream();
      out.println(CONSOLE_TEXT);
   }
   
   
   private MessageConsole findConsole(String name) {
      ConsolePlugin plugin = ConsolePlugin.getDefault();
      IConsoleManager conMan = plugin.getConsoleManager();
      IConsole[] existing = conMan.getConsoles();
      for (int i = 0; i < existing.length; i++)
         if (name.equals(existing[i].getName()))
            return (MessageConsole) existing[i];
      //no console found, so create a new one
      MessageConsole myConsole = new MessageConsole(name, null);
      conMan.addConsoles(new IConsole[] { myConsole });
      return myConsole;
   }
}
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am

Re: Getting text from 'console' tab

Postby nsajjan » Fri Jan 29, 2010 8:26 am

Thanks Phil,
I was able to grab the 'console' text by inserting the below piece of code that I found in another thread in this forum:
Code: Select all
        IWidgetReference ref = (IWidgetReference) ui.find(new SWTWidgetLocator(StyledText.class, new ViewLocator("org.eclipse.ui.console.ConsoleView")));
        StyledText text = (StyledText) ref.getWidget();
        String contents = getText(text);

     private String getText(final StyledText text) {
        final String [] contents = new String[1];
        Display.getDefault().syncExec(new Runnable() {
           public void run() {
              contents[0] = text.getText();
           }
        });



Thanks,
NS
nsajjan
 
Posts: 4
Joined: Thu Jan 28, 2010 7:26 am

Re: Getting text from 'console' tab

Postby Phil Quitslund » Fri Jan 29, 2010 9:44 am

Good deal. Thanks for following up!
--
Phil Quitslund
Software Engineer
Google, Inc.
Phil Quitslund
Moderator
 
Posts: 491
Joined: Fri Apr 28, 2006 6:26 am


Return to Window Tester

Who is online

Users browsing this forum: No registered users and 1 guest