Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

passing context to execute method for initiating step JIT with data from context #156

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
/test-output/
/img
/dependencies
.github
13 changes: 13 additions & 0 deletions src/com/qmetry/qaf/automation/step/StringTestStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public StringTestStep(String name, Map<String, Object> context,
this.context = context;
}


public void initStep() {
if (step == null) {
step = getTestStep();
Expand All @@ -85,6 +86,17 @@ public void initStep() {
}
}

public void initStep(Map<String, Object> context) {
if (step == null) {
step = getTestStep();
if (null != step) {
step.setActualArgs(actualArgs);
step.setDescription(description);
step.getStepExecutionTracker().setContext(context);
}
}
}

public TestStep deepClone() {
initStep();
TestStep s = step.clone();
Expand Down Expand Up @@ -289,4 +301,5 @@ private void absractArgsAandSetDesciption() {
name = StringUtil
.toCamelCaseIdentifier(description.length() > 0 ? description : name);
}

}
153 changes: 81 additions & 72 deletions src/com/qmetry/qaf/automation/step/client/DataDrivenScenario.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
/*******************************************************************************
* QMetry Automation Framework provides a powerful and versatile platform to author
* Automated Test Cases in Behavior Driven, Keyword Driven or Code Driven approach
*
* QMetry Automation Framework provides a powerful and versatile platform to
* author
* Automated Test Cases in Behavior Driven, Keyword Driven or Code Driven
* approach
* Copyright 2016 Infostretch Corporation
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
*
* You should have received a copy of the GNU General Public License along with this program in the name of LICENSE.txt in the root folder of the distribution. If not, see https://opensource.org/licenses/gpl-3.0.html
*
* See the NOTICE.TXT file in root folder of this source files distribution
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT
* OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE
* You should have received a copy of the GNU General Public License along with
* this program in the name of LICENSE.txt in the root folder of the
* distribution. If not, see https://opensource.org/licenses/gpl-3.0.html
* See the NOTICE.TXT file in root folder of this source files distribution
* for additional information regarding copyright ownership and licenses
* of other open source software / files used by QMetry Automation Framework.
*
* For any inquiry or need additional information, please contact [email protected]
* For any inquiry or need additional information, please contact
* [email protected]
*******************************************************************************/


package com.qmetry.qaf.automation.step.client;

import static com.qmetry.qaf.automation.core.ConfigurationManager.getBundle;
Expand All @@ -45,7 +50,6 @@
import org.testng.internal.collections.ArrayIterator;

import com.qmetry.qaf.automation.core.AutomationError;
import com.qmetry.qaf.automation.step.StringTestStep;
import com.qmetry.qaf.automation.step.TestStep;
import com.qmetry.qaf.automation.testng.dataprovider.QAFDataProvider.dataproviders;
import com.qmetry.qaf.automation.testng.dataprovider.QAFDataProvider.params;
Expand All @@ -65,13 +69,14 @@
public class DataDrivenScenario extends Scenario {
private String dataProviderDesc;

public DataDrivenScenario(String testName, Collection<TestStep> steps, String dataProviderDesc) {
public DataDrivenScenario(String testName, Collection<TestStep> steps,
String dataProviderDesc) {
super(testName, steps);
this.dataProviderDesc = dataProviderDesc;
}

public DataDrivenScenario(String testName, Collection<TestStep> steps, String dataProviderDesc,
Map<String, Object> metadata) {
public DataDrivenScenario(String testName, Collection<TestStep> steps,
String dataProviderDesc, Map<String, Object> metadata) {
super(testName, steps, metadata);
this.dataProviderDesc = dataProviderDesc;

Expand All @@ -92,32 +97,37 @@ public void scenario(Map<String, String> testData) {
context.put("${args[0]}", testData);
context.put("args[0]", testData);

execute(getStepsToExecute(context));
execute(getStepsToExecute(), context);

}

protected String comuteSign() {
return getPackage() + "." + scenarioName + "( " + Map.class.getName() + ")";
}

@DataProvider(name = "scenariodp", parallel=true)
@DataProvider(name = "scenariodp", parallel = true)
public Iterator<Object[]> dp(ITestNGMethod tm, ITestContext c) {
System.out.println(dataProviderDesc);
dataProviderDesc = getBundle().getSubstitutor().replace(dataProviderDesc);
Map<String, String> param = StringUtil
.toMap(StringUtil.parseCSV(dataProviderDesc, getBundle().getListDelimiter()), true);
Map<String, String> param = StringUtil.toMap(
StringUtil.parseCSV(dataProviderDesc, getBundle().getListDelimiter()),
true);
System.out.println(param);

if(param.containsKey(params.DATAPROVIDER.name())){
return invokeCustomDataProvider(tm, c, param.get(params.DATAPROVIDER.name()), param.get(params.DATAPROVIDERCLASS.name()));
if (param.containsKey(params.DATAPROVIDER.name())) {
return invokeCustomDataProvider(tm, c, param.get(params.DATAPROVIDER.name()),
param.get(params.DATAPROVIDERCLASS.name()));
}
String dataproviderName = DataProviderUtil.getDataProvider(param);
if (dataproviderName.equalsIgnoreCase(dataproviders.isfw_excel.name())) {
return new ArrayIterator(ExcelUtil.getExcelDataAsMap(param.get(params.DATAFILE.name()), param.get(params.SHEETNAME.name())));
return new ArrayIterator(
ExcelUtil.getExcelDataAsMap(param.get(params.DATAFILE.name()),
param.get(params.SHEETNAME.name())));
}

if (dataproviderName.equalsIgnoreCase(dataproviders.isfw_excel_table.name())) {
return new ArrayIterator(ExcelUtil.getTableDataAsMap(param.get(params.DATAFILE.name()), (param.get(params.KEY.name())),
return new ArrayIterator(ExcelUtil.getTableDataAsMap(
param.get(params.DATAFILE.name()), (param.get(params.KEY.name())),
param.get(params.SHEETNAME.name())));
}

Expand All @@ -130,74 +140,73 @@ public Iterator<Object[]> dp(ITestNGMethod tm, ITestContext c) {
return new ArrayIterator(DatabaseUtil.getRecordDataAsMap(query));
}
if (dataproviderName.equalsIgnoreCase(dataproviders.isfw_property.name())) {
List<Object[]> mapData = DataProviderUtil.getDataSetAsMap(param.get(params.KEY.name()));
List<Object[]> mapData =
DataProviderUtil.getDataSetAsMap(param.get(params.KEY.name()));

return mapData.iterator();

}

if (dataproviderName.equalsIgnoreCase(dataproviders.isfw_json.name())) {
if(param.containsKey(params.JSON_DATA_TABLE.name()))
return new ArrayIterator(JSONUtil.getJsonArrayOfMaps(param.get(params.JSON_DATA_TABLE.name())));

return new ArrayIterator(JSONUtil.getJsonArrayOfMaps(param.get(params.DATAFILE.name())));
if (param.containsKey(params.JSON_DATA_TABLE.name()))
return new ArrayIterator(JSONUtil
.getJsonArrayOfMaps(param.get(params.JSON_DATA_TABLE.name())));

return new ArrayIterator(
JSONUtil.getJsonArrayOfMaps(param.get(params.DATAFILE.name())));
}
throw new RuntimeException("No data provider found");
}

private Iterator<Object[]> invokeCustomDataProvider(ITestNGMethod tm, ITestContext c, String dp, String dpc) {
if(StringUtil.isBlank(dpc)){
dpc=getBundle().getString("global.dataproviderclass",getBundle().getString("dataproviderclass"));
private Iterator<Object[]> invokeCustomDataProvider(ITestNGMethod tm, ITestContext c,
String dp, String dpc) {
if (StringUtil.isBlank(dpc)) {
dpc = getBundle().getString("global.dataproviderclass",
getBundle().getString("dataproviderclass"));
}
if(StringUtil.isNotBlank(dpc)){
if (StringUtil.isNotBlank(dpc)) {
Method m = getDataProviderMethod(dp, dpc);
Object instanceToUse = ClassHelper.newInstanceOrNull(m.getDeclaringClass());
return InvocatoinHelper.invokeDataProvider(instanceToUse, m, tm, c, null, new Configuration().getAnnotationFinder());
}else{
throw new AutomationError("Data-provider class not found. Please provide fully qualified class name as dataProviderClass");
return InvocatoinHelper.invokeDataProvider(instanceToUse, m, tm, c, null,
new Configuration().getAnnotationFinder());
} else {
throw new AutomationError(
"Data-provider class not found. Please provide fully qualified class name as dataProviderClass");
}
}

private Method getDataProviderMethod(String dp, String dpc){
private Method getDataProviderMethod(String dp, String dpc) {
try {
Class<?> dpClass = Class.forName(dpc);
Set<Method> dpMethods = ClassUtil.getAllMethodsWithAnnotation(dpClass, DataProvider.class);
for(Method m : dpMethods){
Set<Method> dpMethods =
ClassUtil.getAllMethodsWithAnnotation(dpClass, DataProvider.class);
for (Method m : dpMethods) {
DataProvider dpObj = ClassUtil.getAnnotation(m, DataProvider.class);
if(dp.equalsIgnoreCase(dpObj.name())){
//this is the mehod we are lo
if (dp.equalsIgnoreCase(dpObj.name())) {
// this is the mehod we are lo
return m;
}
}
} catch (ClassNotFoundException e) {
throw new AutomationError("Data-provider class " + dpc + " not found. Please provide fully qualified class name as dataProviderClass");
}
throw new AutomationError("Data-provider: '" + dp + "' not found in class: '"+ dpc+ "'. Please provide valid data provider name as dataProvider");
}
private TestStep[] getStepsToExecute(Map<String, Object> context) {
TestStep[] proxySteps = new TestStep[steps.size()];
int stepIndex = 0;
for (TestStep testStep : steps) {

StringTestStep proxy = new StringTestStep(testStep.getName(), context, testStep.getActualArgs());
proxy.initStep();
proxy.setLineNumber(testStep.getLineNumber());
proxy.setFileName(testStep.getFileName());
if (null != proxy.getStepExecutionTracker())
proxy.getStepExecutionTracker().setContext(context);

proxySteps[stepIndex++] = proxy;
throw new AutomationError("Data-provider class " + dpc
+ " not found. Please provide fully qualified class name as dataProviderClass");
}

return proxySteps;
throw new AutomationError("Data-provider: '" + dp + "' not found in class: '"
+ dpc + "'. Please provide valid data provider name as dataProvider");
}

private static class InvocatoinHelper extends MethodInvocationHelper{
protected static Iterator<Object[]> invokeDataProvider(Object instance, Method dataProvider,
ITestNGMethod method, ITestContext testContext, Object fedInstance,
IAnnotationFinder annotationFinder) {
return MethodInvocationHelper.invokeDataProvider(instance, dataProvider, method, testContext, fedInstance, annotationFinder);
}
private TestStep[] getStepsToExecute() {
TestStep[] stepsToExecute = steps.toArray(new TestStep[steps.size()]);// new
return stepsToExecute;
}

private static class InvocatoinHelper extends MethodInvocationHelper {
protected static Iterator<Object[]> invokeDataProvider(Object instance,
Method dataProvider, ITestNGMethod method, ITestContext testContext,
Object fedInstance, IAnnotationFinder annotationFinder) {
return MethodInvocationHelper.invokeDataProvider(instance, dataProvider,
method, testContext, fedInstance, annotationFinder);
}
}

}
25 changes: 12 additions & 13 deletions src/com/qmetry/qaf/automation/step/client/Scenario.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,15 @@ public String getTestName() {
return scenarioName;
}

protected void execute(TestStep[] stepsToExecute) {
protected void execute(TestStep[] stepsToExecute, Map<String, Object> context) {
int executionIndx = 0;
try {
for (executionIndx = 0; executionIndx < stepsToExecute.length;) {
TestStep currTestStep = stepsToExecute[executionIndx];
((StringTestStep) currTestStep).initStep();

if (null != context) {
((StringTestStep) currTestStep).initStep(context);
} else((StringTestStep) currTestStep).initStep();

StepExecutionTracker stepExecutionTracker =
currTestStep.getStepExecutionTracker();
Expand All @@ -154,11 +157,9 @@ protected void execute(TestStep[] stepsToExecute) {
stepResultBean.setType(MessageTypes.Warn);
stb.getCheckPointResults().add(stepResultBean);

LoggingBean comLoggingBean =
new LoggingBean(currTestStep.getName(),
new String[]{Arrays
.toString(currTestStep.getActualArgs())},
"Error: Step Not Found");
LoggingBean comLoggingBean = new LoggingBean(currTestStep.getName(),
new String[]{Arrays.toString(currTestStep.getActualArgs())},
"Error: Step Not Found");
stb.getLog().add(comLoggingBean);
if (!ApplicationProperties.DRY_RUN_MODE.getBoolenVal(false))
throw new StepNotFoundException((StringTestStep) currTestStep);
Expand Down Expand Up @@ -193,11 +194,9 @@ protected void execute(TestStep[] stepsToExecute) {
stepResultBean.setType(MessageTypes.TestStep);
stb.getCheckPointResults().add(stepResultBean);

LoggingBean comLoggingBean =
new LoggingBean(notRunTestStep.getName(),
new String[]{
Arrays.toString(notRunTestStep.getActualArgs())},
"Not Run");
LoggingBean comLoggingBean = new LoggingBean(notRunTestStep.getName(),
new String[]{Arrays.toString(notRunTestStep.getActualArgs())},
"Not Run");
stb.getLog().add(comLoggingBean);

}
Expand All @@ -216,7 +215,7 @@ public void scenario() {
for (int i = 0; i < stepsToExecute.length; i++) {
stepsToExecute[i] = stepsToExecute[i].clone(); // fix for retry
}
execute(stepsToExecute);
execute(stepsToExecute, null);
}

}
Expand Down