如何在柑橘框架中重用场景?

How to reuse a scenario in citrus framework?

我用柑橘框架设置了这个测试场景。现在我试图在其他场景中重用它。 我正在为每个步骤创建一个行为。我的行为主要是http请求

public class NoProductDocumentValidationScenarioIT {

    private @CitrusResource TestContext parentContext;

    @CitrusEndpoint(name = "todoBasicAuthClient")
    private HttpClient cmsAuthClient;

    @CitrusEndpoint(name = "vdmBasicAuthClient")
    private HttpClient vdmAuthClient;

    @CitrusEndpoint(name = "gvHttpClient")
    private HttpClient gvHttpClient;

    @Test
    @CitrusTest
    public String NoProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context)
            throws BadNewsMLG2Exception {
        String pdtIdentifier = "EDIT-FR-SVID2-YM9N001479";
        String videoDocument = VideoDocument.setUpVideoDocument("fr", "v1_afptv_sport_broadcast_photos");
        int jobPublicationID = 5;
        // CMS Authentification
        TestBehavior authenticateCMS = new ProductAuthenticationBehavior(cmsAuthClient);
        ApplyTestBehaviorAction authenticateActionCMS = new ApplyTestBehaviorAction(runner, authenticateCMS);
        authenticateActionCMS.doExecute(context);

        // Document Creation
        CreateVideoDocumentBehavior createDoc = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument);
        ApplyTestBehaviorAction createDocAction = new ApplyTestBehaviorAction(runner, createDoc);
        createDocAction.doExecute(context);

        // get document data
        videoDocument = createDoc.getVideoDocument();
        G2VideoDocument g2VideoDocument = ((G2VideoDocument) G2ObjectFactory.parse(videoDocument));
        g2VideoDocument.getProducts();
        String linkToVideoDocument = g2VideoDocument.getLinkToSelf().toString();
        String linkToProject = g2VideoDocument.getLinkToVideoProject().toString();
        String projectID = IrisStringTools.extractIdFromUri(linkToProject);
        String documentID = IrisStringTools.extractIdFromUri(linkToVideoDocument);
        String etag = g2VideoDocument.getEditorialTag();

        // Lock document Metadata
        EditVideoDocumentMetaBehavior lockDocMeta = new EditVideoDocumentMetaBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument, documentID);
        ApplyTestBehaviorAction lockDocMetaAction = new ApplyTestBehaviorAction(runner, lockDocMeta);
        lockDocMetaAction.doExecute(context);
}
}

我运行这在eclipse中作为JUnit测试。

我考虑过使用超级 class 但它没有用。

public class ProductDocumentValidationScenarioIT extends NoProductDocumentValidationScenarioIT {

    public String ProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context)
            throws BadNewsMLG2Exception {
                return something;

    }
}

测试行为是解决问题的方法。我建议使用这样的东西

CreateVideoDocumentBehavior createDoc = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier,
                videoDocument);

runner.applyBehavior(createDoc);

我们最后做的是创建一个行为运行器(一个java class),其中所有的行为都被实例化,然后在场景中我们调用行为运行器,行为常量对应于我需要的行为:

public class BehaviorRunner {
private void doExecute(TestRunner runner, TestContext context, TestBehavior testBehavior) {
    ApplyTestBehaviorAction behaviorAction = new ApplyTestBehaviorAction(runner,testBehavior);
    behaviorAction.doExecute(context);
}

public void execute( String behaviorLabel, @CitrusResource TestRunner runner, @CitrusResource TestContext context) {
    try {
        switch (behaviorLabel) {
            case BehaviorConstants.CREATE_VIDEO_DOCUMENT :
                CreateVideoDocumentBehavior createVideoDocumentBehavior = new CreateVideoDocumentBehavior(cmsAuthClient, pdtIdentifier, VideoDocument.setUpVideoDocument2(LanguageConstants.EN, "v1_afptv_sport_broadcast_photos"));
                doExecute(runner, context, createVideoDocumentBehavior);
                break;
            case BehaviorConstants.MOVIEDRAFT :
                MovieDraftDocumentBehavior movieDraftDocumentBehavior = new MovieDraftDocumentBehavior(cmsAuthClient, pdtIdentifier, 1, g2VideoDoc);
                doExecute(runner, context, movieDraftDocumentBehavior);     
                break;
            case BehaviorConstants.PUBLICATION_PROGRESSION_STATUS:
                GetPublicationProgressionStatusBehavior publicationProgressionStatusBehavior = new GetPublicationProgressionStatusBehavior(vdmAuthClient, pdtIdentifier , g2VideoDoc);
                doExecute(runner, context, publicationProgressionStatusBehavior);   
                break;
            case BehaviorConstants.VALIDATE :
                ValidateDocumentBehavior validateDocumentBehavior = new ValidateDocumentBehavior(cmsAuthClient, pdtIdentifier, g2VideoDoc);
                doExecute(runner, context, validateDocumentBehavior);   
                break;
            default:
                    break;
        }   

我们最终遇到了这样的场景:

@Test
@CitrusTest
public void NoProductDocumentValidation(@CitrusResource TestRunner runner, @CitrusResource TestContext context) throws BadNewsMLG2Exception {
    slf4jLogger.info("Montage analysis scenario START");
    // execute scenario
    // Document Creation 
    behaviorRunner.execute(BehaviorConstants.CREATE_VIDEO_DOCUMENT, runner, context);
    // Lock document Metadata
    behaviorRunner.execute(BehaviorConstants.EDITOR, runner, context);
    // Lock Document Binary 
    behaviorRunner.execute(BehaviorConstants.BINARY_EDITOR, runner, context);

这为我们节省了很多代码行,因为我们在不同的场景中使用了不同的行为组合。

我希望这对某人有所帮助!