Serenity-js: REST API: Pass URL as an argument instead of setting it as a property value

Created on 27 Apr 2018  路  4Comments  路  Source: serenity-js/serenity-js

Hello all! I'm creating a REST API tests using serenityjs/rest package and I'm following examples posted here but I want to know how to pass the URL as a CLI argument rather than setting it as a property value on a config file or any .ts file?

documentation question

Most helpful comment

Well, Jan would know better, but I think that UI or API actually doesn't change the matter. It's more about how to give the info to protractor (acting as your test runner), and then how it can send that data to your tests, shall they be API or UI.

Ex. (using an ability, there's other way to do that)

UI : return Actor.named(firstNameActor).whoCan(Open.browserOn(HOME_PAGE));
API : return Actor.named(firstNameActor).whoCan(CallAnApi.at(API_URL));

With for example

export const API_URL = ENV === 'prod' ? `https://api.mysite.com` : `https://api.mysite-${ENV}.com`;
export const HOME_PAGE = ENV === 'prod' ? `https://www.mysite.com` : `https://www.mysite-${ENV}.com`;

And then using something like Jan suggested (env. variable before npm test), or follow https://moduscreate.com/blog/protractor_parameters_adding_flexibility_automation_tests/ article. I'm no protractor expert though, so maybe there's even a more elegant way to communicate URL to it.

All 4 comments

Hey @marktigno! Do you mean as an argument set at runtime? You could use an env variable for example:

API_URL=https://mycompany.com/api npm test

and then

Actor.named('Adam').whoCan(CallAnApi.at(process.env.API_URL))

Does that help?
Jan

That could help too, and thanks for this. another would be placing it on a batch file or something like this:
npm test -- --baseUrl='http://www.somewebsite.com/'
I could do this in protractor test but I don't know on API based tests. :)

Well, Jan would know better, but I think that UI or API actually doesn't change the matter. It's more about how to give the info to protractor (acting as your test runner), and then how it can send that data to your tests, shall they be API or UI.

Ex. (using an ability, there's other way to do that)

UI : return Actor.named(firstNameActor).whoCan(Open.browserOn(HOME_PAGE));
API : return Actor.named(firstNameActor).whoCan(CallAnApi.at(API_URL));

With for example

export const API_URL = ENV === 'prod' ? `https://api.mysite.com` : `https://api.mysite-${ENV}.com`;
export const HOME_PAGE = ENV === 'prod' ? `https://www.mysite.com` : `https://www.mysite-${ENV}.com`;

And then using something like Jan suggested (env. variable before npm test), or follow https://moduscreate.com/blog/protractor_parameters_adding_flexibility_automation_tests/ article. I'm no protractor expert though, so maybe there's even a more elegant way to communicate URL to it.

Thanks @san-ouadghiri and @jan-molak for the comments, I will try these on my tests here. Closing this ticket for now. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrj04 picture mrj04  路  3Comments

rakeshnambiar picture rakeshnambiar  路  7Comments

AbhineetSharmax picture AbhineetSharmax  路  4Comments

codesandtags picture codesandtags  路  4Comments

san-ouadghiri picture san-ouadghiri  路  6Comments