1
2
3
4
5
6
7
8
9
10
11
12
13 package com.eviware.soapui.impl.wsdl.actions.project;
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.WsdlProject;
24 import com.eviware.soapui.impl.wsdl.WsdlSubmit;
25 import com.eviware.soapui.impl.wsdl.WsdlTestSuite;
26 import com.eviware.soapui.impl.wsdl.panels.teststeps.HttpTestRequestDesktopPanel;
27 import com.eviware.soapui.impl.wsdl.support.HelpUrls;
28 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase;
29 import com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext;
30 import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequest;
31 import com.eviware.soapui.impl.wsdl.teststeps.HttpTestRequestStep;
32 import com.eviware.soapui.impl.wsdl.teststeps.registry.HttpRequestStepFactory;
33 import com.eviware.soapui.model.iface.Request.SubmitException;
34 import com.eviware.soapui.model.support.ModelSupport;
35 import com.eviware.soapui.support.MessageSupport;
36 import com.eviware.soapui.support.StringUtils;
37 import com.eviware.soapui.support.UISupport;
38 import com.eviware.soapui.support.action.support.AbstractSoapUIAction;
39 import com.eviware.x.form.XFormDialog;
40 import com.eviware.x.form.support.ADialogBuilder;
41 import com.eviware.x.form.support.AField;
42 import com.eviware.x.form.support.AForm;
43 import com.eviware.x.form.support.AField.AFieldType;
44 import com.sun.java.xml.ns.j2Ee.HttpMethodType;
45
46 public class CreateWebTestAction extends AbstractSoapUIAction<WsdlProject>
47 {
48
49 public static final String SOAPUI_ACTION_ID = "AddNewWebTestAction";
50 private WsdlProject project;
51 public static final MessageSupport messages = MessageSupport.getMessages( CreateWebTestAction.class );
52 private static final String CREATE_NEW_OPTION = "<Create New>";
53 private XFormDialog dialog;
54 HttpTestRequestDesktopPanel desktopPanel;
55 private final static Logger logger = Logger.getLogger( CreateWebTestAction.class );
56
57 public CreateWebTestAction()
58 {
59 super( "New Web TestCase", "New Web TestCase" );
60 }
61
62 public void perform( WsdlProject target, Object param )
63 {
64 this.project = target;
65 if( dialog == null )
66 {
67 dialog = ADialogBuilder.buildDialog( Form.class );
68 }
69
70 dialog.setOptions( Form.TESTSUITE, ModelSupport.getNames( new String[] { CREATE_NEW_OPTION }, project
71 .getTestSuiteList() ) );
72 dialog.setValue( Form.TESTSUITE, CREATE_NEW_OPTION );
73 dialog.setValue( Form.TESTCASENAME, "Web TestCase" );
74 dialog.setValue( Form.URL, "" );
75 dialog.setValue( Form.STARTRECORDING, Boolean.toString( true ) );
76 if( dialog.show() )
77 {
78 String targetTestSuiteName = dialog.getValue( Form.TESTSUITE );
79 String targetTestCaseName = dialog.getValue( Form.TESTCASENAME );
80 while( StringUtils.isNullOrEmpty( dialog.getValue( Form.URL ) ) )
81 {
82 UISupport.showErrorMessage( "You must specify the web address to start at" );
83 dialog.show();
84 }
85 String testStepName = dialog.getValue( Form.URL );
86 String url = HttpUtils.ensureEndpointStartsWithProtocol( testStepName );
87 WsdlTestSuite targetTestSuite = null;
88 WsdlTestCase targetTestCase = null;
89
90 targetTestSuite = project.getTestSuiteByName( targetTestSuiteName );
91 if( targetTestSuite == null )
92 {
93 targetTestSuiteName = "Web TestSuite";
94 while( project.getTestSuiteByName( targetTestSuiteName ) != null )
95 {
96 targetTestSuiteName = UISupport.prompt(
97 "TestSuite name must be unique, please specify new name for TestSuite\n" + "[" + project.getName()
98 + "->" + targetTestSuiteName + "]", "Change TestSuite name", targetTestSuiteName );
99
100 if( targetTestSuiteName == null )
101 return;
102 }
103
104 targetTestSuite = project.addNewTestSuite( targetTestSuiteName );
105 }
106 targetTestCase = targetTestSuite.getTestCaseByName( targetTestCaseName );
107 if( targetTestCase == null )
108 {
109 while( targetTestSuite.getTestCaseByName( targetTestCaseName ) != null )
110 {
111 targetTestCaseName = UISupport.prompt(
112 "TestCase name must be unique, please specify new name for TestCase\n" + "[" + targetTestCaseName
113 + "] in TestSuite [" + project.getName() + "->" + targetTestSuiteName + "]",
114 "Change TestCase name", targetTestCaseName );
115 if( targetTestCaseName == null )
116 return;
117 }
118 targetTestCase = targetTestSuite.addNewTestCase( targetTestCaseName );
119
120 }
121 while( testStepName == null || targetTestCase.getTestStepByName( testStepName ) != null )
122 {
123 testStepName = UISupport.prompt( "TestStep name must be unique, please specify new name for step\n" + "["
124 + testStepName + "] in TestCase [" + project.getName() + "->" + targetTestSuiteName + "->"
125 + targetTestCaseName + "]", "Change TestStep name", testStepName );
126
127 if( testStepName == null )
128 return;
129 }
130 createWebTest( targetTestCase, HttpUtils.ensureEndpointStartsWithProtocol( url ), testStepName, dialog
131 .getBooleanValue( Form.STARTRECORDING ) );
132
133 }
134 }
135
136 private void createWebTest( WsdlTestCase targetTestCase, String endpoint, String name, final boolean startRecording )
137 {
138 targetTestCase.setKeepSession( true );
139
140 HttpRequestConfig httpRequest = HttpRequestConfig.Factory.newInstance();
141 httpRequest.setMethod( HttpMethodType.GET.toString() );
142
143 httpRequest.setEndpoint( endpoint );
144
145 TestStepConfig testStepConfig = TestStepConfig.Factory.newInstance();
146 testStepConfig.setType( HttpRequestStepFactory.HTTPREQUEST_TYPE );
147 testStepConfig.setConfig( httpRequest );
148 testStepConfig.setName( name );
149 HttpTestRequestStep testStep = ( HttpTestRequestStep )targetTestCase.addTestStep( testStepConfig );
150
151 desktopPanel = ( HttpTestRequestDesktopPanel )UISupport.selectAndShow( testStep );
152 HttpTestRequest testRequest = null;
153 try
154 {
155 testRequest = testStep.getTestRequest();
156 WsdlSubmit<HttpRequest> submitRequest = testRequest.submit( new WsdlTestRunContext( testStep ), true );
157 if( startRecording )
158 {
159 submitRequest.waitUntilFinished();
160 HttpHtmlResponseView htmlResponseView = ( HttpHtmlResponseView )desktopPanel.getResponseEditor().getViews()
161 .get( 2 );
162 htmlResponseView.setRecordHttpTrafic( true );
163 }
164 }
165 catch( SubmitException e )
166 {
167 SoapUI.logError( e );
168 }
169 desktopPanel.focusResponseInTabbedView( true );
170 }
171
172 @AForm( description = "Specify Web TestCase Options", name = "Add Web TestCase", helpUrl = HelpUrls.CLONETESTSUITE_HELP_URL, icon = UISupport.TOOL_ICON_PATH )
173 public interface Form
174 {
175 @AField( name = "Web Address", description = "The web address to start at", type = AField.AFieldType.STRING )
176 public final static String URL = "Web Address";
177
178 @AField( name = "Web TestCase Name", description = "The Web TestCase name", type = AFieldType.STRING )
179 public final static String TESTCASENAME = "Web TestCase Name";
180
181 @AField( name = "Target TestSuite", description = "The target TestSuite to add WebTest to", type = AFieldType.ENUMERATION )
182 public final static String TESTSUITE = "Target TestSuite";
183
184 @AField( description = "", type = AFieldType.BOOLEAN, enabled = true )
185 public final static String STARTRECORDING = "Start Recording immediately";
186
187 }
188 }