serenity-bdd / serenity-core

Serenity BDD is a test automation library designed to make writing automated acceptance tests easier, and more fun.
http://serenity-bdd.info
Other
715 stars 513 forks source link

Question::How to access the values of the variables defined in Serenity.Conf? #1752

Closed vbhardwaj-matchless closed 5 years ago

vbhardwaj-matchless commented 5 years ago

environments { default { webdriver.base.url = "https://duckduckgo.com"
} dev { webdriver.base.url = "https://duckduckgo.com/dev" } staging { webdriver.base.url = "https://duckduckgo.com/staging" } prod { webdriver.base.url = "https://duckduckgo.com/prod" } }

I want to access the webdriver.base.url in dev.

I have tried the following code:

EnvironmentVariables variables = SystemEnvironmentVariables.createEnvironmentVariables(); String webserviceEndpoint = EnvironmentSpecificConfiguration.from(variables).getProperty("webdriver.base.url");

webserviceEndpoint is always populated with "https://duckduckgo.com".

How could I access the webdriver.base.url in dev?

thienphuong commented 5 years ago

I think the value of webdriver.base.url is depend on the -Denvironment=staging that you push in your maven command

if mvn verify then webserviceEndpoint="https://duckduckgo.com" if mvn verify -Denvironment=staging then webserviceEndpoint="https://duckduckgo.com/staging" if mvn verify -Denvironment=prod then webserviceEndpoint="https://duckduckgo.com/prod"

Hendrione commented 5 years ago

I got same issue. it always opening "https://duckduckgo.com" even using openAt() method.

wakaleo commented 5 years ago

See https://serenity-bdd.github.io/theserenitybook/latest/environment-specific-config.html

vbhardwaj-matchless commented 5 years ago

Thanks John for the quick reply. I have gone through the link you have shared. I could access the value of the key defined in default but not able to access anything mentioned in dev, staging or prod.

Below are the details:

environments { default { webdriver.base.url = "https://duckduckgo.com" } dev { webdriver.base.url = "https://duckduckgo.com/dev" accounts.service.url = "http://dev.accounts.myorg.com" } staging { webdriver.base.url = "https://duckduckgo.com/staging" } prod { webdriver.base.url = "https://duckduckgo.com/prod" } }

Code: private EnvironmentVariables environmentVariables;

String webserviceEndpoint = EnvironmentSpecificConfiguration.from(environmentVariables).getProperty("accounts.service.url");

It throws ::"Method threw 'net.serenitybdd.core.environment.UndefinedEnvironmentVariableException' exception."

I want to access the value of "accounts.service.url" in dev on the page Object. How could I do it?

Note: I know it can be done using mvn command.

wakaleo commented 5 years ago

You can write environmentVariables.getProperty("environments.dev.accounts.service.url"), but it would defeat the whole point of having environment-specific variables, which is so that the tests don't need to know what environment they are running in at any point in time.

wakaleo commented 5 years ago

Presume answered.

santiagogf26 commented 3 years ago

how to use mvn verify -Denvironment=staging for gradle=

arvindjosh08 commented 3 years ago

@santiagogf26 ...i was trying with gradle clean test -Denvironment but it is always picking up the environment variables inside default section. Any luck with you?

My Serenity.conf file looks like below

environments { default { authorization.endpoint = "xyz" } dev { authorization.endpoint = "abc" }

webdriver.chrome.driver=src/test/resources/chromedriver.exe chrome_experimental_options.useAutomationExtension = "false" webdriver.wait.for.timeout=80000 serenity.timeout=80000 webdriver.timeouts.implicitlywait=80000 chrome.switches = --start-maximized

shawnjburke commented 7 months ago

Ran into this thread years later and was still struggling to run Serenity Environments with Gradle. Note that it worked fine on Maven. I expected -D to "just work" but in Gradle it behaved as if it passed it to Gradle, but never on to Serenity. Came up with this solution which seems heavy, but leave it for the next lost soul, or someone smarter to correct me.

I started with a serenity Template. It includes build.gradle which has a section called test

test {
    useJUnitPlatform()
}

I defined a new variable called jvmArgs and then picked up the value from the command line

test {
    useJUnitPlatform()
    def parameter = '-Denvironment='
    def environment = findProperty('environment')
    jvmArgs (parameter+environment)
}

Which then meant I passed it on the command line as gradlew test -Penvironment=dev

Sample environments file from Serenity https://johnfergusonsmart.com/environment-specific-configuration-in-serenity-bdd/.

Sample code from Serenity pulls the values

private EnvironmentVariables envVars;
@Given("{actor} is requesting api information with a valid token")
public void getsFhaCase(Actor actor) {
     String baseUrl =  EnvironmentSpecificConfiguration.from(envVars).getProperty("api.baseUrl");

Thanks to https://stackoverflow.com/questions/50435218/how-to-pass-args-to-jvm-which-runs-tests-with-gradle (Lukas Körfer) for the tip on jvmArgs.

Thanks to https://stackoverflow.com/questions/44216986/how-to-pass-command-line-arguments-to-main-class-in-gradle (Drew MacInnis) for the tipoff on findProperty().