View Javadoc

1   /*
2    *  soapUI, copyright (C) 2004-2010 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of version 2.1 of the GNU Lesser General Public License as published by 
6    *  the Free Software Foundation.
7    *
8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
10   *  See the GNU Lesser General Public License for more details at gnu.org.
11   */
12  
13  package com.eviware.soapui.impl.wsdl.actions.testsuite;
14  
15  import org.apache.log4j.Logger;
16  
17  import com.eviware.soapui.SoapUI;
18  import com.eviware.soapui.config.HttpRequestConfig;
19  import com.eviware.soapui.config.TestStepConfig;
20  import com.eviware.soapui.impl.rest.panels.request.views.html.HttpHtmlResponseView;
21  import com.eviware.soapui.impl.support.HttpUtils;
22  import com.eviware.soapui.impl.support.http.HttpRequest;
23  import com.eviware.soapui.impl.wsdl.WsdlSubmit;
24  import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
25  import com.eviware.soapui.impl.wsdl.panels.teststeps.HttpTestRequestDesktopPanel;
26  import com.eviware.soapui.impl.wsdl.support.HelpUrls;
27  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
28  import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
29  import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequest;
30  import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep;
31  import com.eviware.soapui.impl.wsdl.teststeps.registry.HttpRequestStepFactory;
32  import com.eviware.soapui.support.MessageSupport;
33  import com.eviware.soapui.support.StringUtils;
34  import com.eviware.soapui.support.UISupport;
35  import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
36  import com.eviware.x.form.XFormDialog;
37  import com.eviware.x.form.support.ADialogBuilder;
38  import com.eviware.x.form.support.AField;
39  import com.eviware.x.form.support.AForm;
40  import com.eviware.x.form.support.AField.AFieldType;
41  import com.sun.java.xml.ns.j2Ee.HttpMethodType;
42  
43  public class CreateWebTestCaseAction extends AbstractSoapUIAction<WsdlTestSuite>
44  {
45  
46  	public static final String SOAPUI_ACTION_ID = "CreateWebTestCaseAction";
47  	private WsdlTestSuite testSuite;
48  	public static final MessageSupport messages = MessageSupport.getMessages( CreateWebTestCaseAction.class );
49  	private XFormDialog dialog;
50  	HttpTestRequestDesktopPanel desktopPanel;
51  	private final static Logger logger = Logger.getLogger( CreateWebTestCaseAction.class );
52  
53  	public CreateWebTestCaseAction()
54  	{
55  		super( "New Web TestCase", "Creates a new Web TestCase in this TestSuite" );
56  	}
57  
58  	public void perform( WsdlTestSuite target, Object param )
59  	{
60  		this.testSuite = target;
61  		if( dialog == null )
62  		{
63  			dialog = ADialogBuilder.buildDialog( Form.class );
64  		}
65  
66  		dialog.setValue( Form.TESTCASENAME, "Web TestCase" );
67  		dialog.setValue( Form.URL, "" );
68  		dialog.setValue( Form.STARTRECORDING, Boolean.toString( true ) );
69  		if( dialog.show() )
70  		{
71  			String targetTestCaseName = dialog.getValue( Form.TESTCASENAME );
72  			while( StringUtils.isNullOrEmpty( dialog.getValue( Form.URL ) ) )
73  			{
74  				UISupport.showErrorMessage( "You must specify the web address to start at" );
75  				dialog.show();
76  			}
77  			String testStepName = dialog.getValue( Form.URL );
78  			String url = HttpUtils.ensureEndpointStartsWithProtocol( testStepName );
79  			WsdlTestCase targetTestCase = null;
80  
81  			targetTestCase = testSuite.getTestCaseByName( targetTestCaseName );
82  			if( targetTestCase == null )
83  			{
84  				while( testSuite.getTestCaseByName( targetTestCaseName ) != null )
85  				{
86  					targetTestCaseName = UISupport.prompt(
87  							"TestCase name must be unique, please specify new name for TestCase\n" + "[" + targetTestCaseName
88  									+ "] in TestSuite [" + testSuite.getName() + "->" + testSuite.getName() + "]",
89  							"Change TestCase name", targetTestCaseName );
90  					if( targetTestCaseName == null )
91  						return;
92  				}
93  				targetTestCase = testSuite.addNewTestCase( targetTestCaseName );
94  
95  			}
96  			while( testStepName == null || targetTestCase.getTestStepByName( testStepName ) != null )
97  			{
98  				testStepName = UISupport.prompt( "TestStep name must be unique, please specify new name for step\n" + "["
99  						+ testStepName + "] in TestCase [" + testSuite.getName() + "->" + testSuite.getName() + "->"
100 						+ targetTestCaseName + "]", "Change TestStep name", testStepName );
101 
102 				if( testStepName == null )
103 					return;
104 			}
105 			createWebTest( targetTestCase, HttpUtils.ensureEndpointStartsWithProtocol( url ), testStepName, dialog
106 					.getBooleanValue( Form.STARTRECORDING ) );
107 
108 		}
109 	}
110 
111 	public void createWebTest( WsdlTestCase targetTestCase, String endpoint, String name, final boolean startRecording )
112 	{
113 		targetTestCase.setKeepSession( true );
114 
115 		HttpRequestConfig httpRequest = HttpRequestConfig.Factory.newInstance();
116 		httpRequest.setMethod( HttpMethodType.GET.toString() );
117 
118 		httpRequest.setEndpoint( endpoint );
119 
120 		TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
121 		testStepConfig.setType( HttpRequestStepFactory.HTTPREQUEST_TYPE );
122 		testStepConfig.setConfig( httpRequest );
123 		testStepConfig.setName( name );
124 		HttpTestRequestStep testStep = ( HttpTestRequestStep )targetTestCase.addTestStep( testStepConfig );
125 
126 		desktopPanel = ( HttpTestRequestDesktopPanel )UISupport.selectAndShow( testStep );
127 		HttpTestRequest testRequest = null;
128 		try
129 		{
130 			testRequest = testStep.getTestRequest();
131 			WsdlSubmit<HttpRequest> submitRequest = testRequest.submit( new WsdlTestRunContext( testStep ), true );
132 
133 			if( startRecording )
134 			{
135 				submitRequest.waitUntilFinished();
136 				HttpHtmlResponseView htmlResponseView = ( HttpHtmlResponseView )desktopPanel.getResponseEditor().getViews()
137 						.get( 2 );
138 				htmlResponseView.setRecordHttpTrafic( true );
139 			}
140 		}
141 		catch( Exception e )
142 		{
143 			SoapUI.logError( e );
144 		}
145 
146 		desktopPanel.focusResponseInTabbedView( true );
147 	}
148 
149 	@AForm( description = "Specify Web TestCase Options", name = "Add Web TestCase", helpUrl = HelpUrls.CLONETESTSUITE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
150 	public interface Form
151 	{
152 		@AField( name = "Web Address", description = "The web address to start at", type = AField.AFieldType.STRING )
153 		public final static String URL = "Web Address";
154 
155 		@AField( name = "Web TestCase Name", description = "The Web TestCase name", type = AFieldType.STRING )
156 		public final static String TESTCASENAME = "Web TestCase Name";
157 
158 		@AField( description = "", type = AFieldType.BOOLEAN, enabled = true )
159 		public final static String STARTRECORDING = "Start Recording immediately";
160 
161 	}
162 }