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

Please add support for JS expectations using XPATH-like expressions or jsonpath #157

Closed
sglebs opened this issue Feb 17, 2016 · 5 comments
Closed

Comments

@sglebs
Copy link
Contributor

sglebs commented Feb 17, 2016

DefiantJS allows one to use xpath-like expressions with JSON results. This is very useful with REST calls that return an array of objects and one wants to assert on a given property of a given object with a given id, for example. This involves searching in the array. See: http://defiantjs.com

I imagine the patch is to inject the DefiantJS lib. It looks like it should be done here:

    public Object evaluateExpression(RestResponse response, String expression) {
        if (expression == null) {
            return null;
        }
        Context context = Context.enter();
        ScriptableObject scope = context.initStandardObjects();
        injectFitNesseSymbolMap(scope);
        injectResponse(context, scope, response);
        Object result = evaluateExpression(context, scope, expression);
        return result;
    }

Maybe a generalization of this ticket is to allow people to inject the JS libs they want to use in the snippets. This way you don't need to hardcode any specific lib.

@sglebs sglebs changed the title Please add support for JS expectations using XPATH-like expressions via DefiantJS Please add support for JS expectations using XPATH-like expressions os jsonpath Feb 18, 2016
@sglebs sglebs changed the title Please add support for JS expectations using XPATH-like expressions os jsonpath Please add support for JS expectations using XPATH-like expressions or jsonpath Feb 18, 2016
@sglebs
Copy link
Contributor Author

sglebs commented Feb 18, 2016

DefiantJS has a problem: it assumes it runs in a web browser. See hbi99/defiant.js#71

@sglebs
Copy link
Contributor Author

sglebs commented Feb 18, 2016

I was able to make it work with https://github.com/s3u/JSONPath , after patching it for JSONPath-Plus/JSONPath#67 . This is how it can be used:

!define TEST_SYSTEM {slim}


!***> classpath config 
!path ${java.class.path}

***!

!|Import| 
|fitnesse.fixtures| 

!| Import |
| smartrics.rest.fitnesse.fixture.RestFixture |
| smartrics.rest.fitnesse.fixture.FitRestFixture |
| smartrics.rest.fitnesse.fixture.RestFixtureConfig |

|script | 
| start | smartrics.rest.fitnesse.fixture.RestFixture |

|Table:smartrics.rest.fitnesse.fixture.RestFixture | http://jsonplaceholder.typicode.com |
|GET    |/users |200|Content-Type : application/json||
|let    | $site | js    | JSONPath({json: response.jsonbody, path: "$..[?(@.id==3)].website"}).toString() |ramiro.info|



sglebs added a commit to sglebs/RestFixture that referenced this issue Feb 18, 2016
@smartrics
Copy link
Owner

Thanks I would be happy to review and possibly merge a pull request
On 18 Feb 2016 19:34, "Marcio Marchini" notifications@github.com wrote:

I was able to make it work with https://github.com/s3u/JSONPath , after
patching it for JSONPath-Plus/JSONPath#67
JSONPath-Plus/JSONPath#67 . This is how it can be used:

!define TEST_SYSTEM {slim}

!***> classpath config
!path ${java.class.path}

***!

!|Import|
|fitnesse.fixtures|

!| Import |
| smartrics.rest.fitnesse.fixture.RestFixture |
| smartrics.rest.fitnesse.fixture.FitRestFixture |
| smartrics.rest.fitnesse.fixture.RestFixtureConfig |

|script |
| start | smartrics.rest.fitnesse.fixture.RestFixture |

|Table:smartrics.rest.fitnesse.fixture.RestFixture | http://jsonplaceholder.typicode.com |
|GET |/users |200|Content-Type : application/json||
|let | $site | js | JSONPath({json: response.jsonbody, path: "$..[?(@.id==3)].website"}).toString() |ramiro.info|


Reply to this email directly or view it on GitHub
#157 (comment)
.

@sglebs
Copy link
Contributor Author

sglebs commented Feb 18, 2016

Done. I was unable to do it with just the 6cafe9d commit, so the pull request also has the setUri change and also a local pom modification which I committed by accident.

The pull request: #159

@smartrics
Copy link
Owner

I commented on the pull request - closing this issue now as we can discuss how best to implement it in the pull request comments section. thanks for the contribution

smartrics added a commit that referenced this issue Mar 17, 2016
…able for expectations and let - generalised approach issue #157 commit 6cafe9d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants