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

can't we use static variables for parallel execution in serenity framework #3442

Closed
geetha051993 opened this issue Apr 18, 2024 · 18 comments
Closed
Labels

Comments

@geetha051993
Copy link

geetha051993 commented Apr 18, 2024

when we are using static variables (Maps and list) and executing test cases parallelly ,since we are using same static variables across all testcases, testcases were failing is there any way that we can handle this.

Thanks in Advance

@wakaleo
Copy link
Member

wakaleo commented Apr 18, 2024

Use ThreadLocals.

@geetha051993
Copy link
Author

geetha051993 commented Apr 18, 2024

Thank you @wakaleo

@wakaleo
Copy link
Member

wakaleo commented Apr 18, 2024

It's a standard Java feature: https://docs.oracle.com/javase/8/docs/api/java/lang/ThreadLocal.html. You can also use the Serenity session variables, e.g

        Serenity.setSessionVariable("color").to("red");
        String color = Serenity.sessionVariableCalled("color");

@geetha051993
Copy link
Author

thank you,
you meant to say threadlocal with serenity session variables right?

@wakaleo
Copy link
Member

wakaleo commented Apr 18, 2024

Serenity session variables are thread safe (they use thread locals)

@geetha051993
Copy link
Author

ok Thank you

@geetha051993
Copy link
Author

Hi @wakaleo , i have tried Serenity session variables didn't work for me, i was calling a feature line where i used to retrieve dynamic data and storing it in map using Serenity.setSessionVariable("color").to("dynamicdata"); and then calling the data by using Serenity.sessionVariableCalled("color"); , then after some feature line (we used to do some apllication navigations) then need to retrieve same data using Serenity.sessionVariableCalled("color"); now it's returning null, can you help me to resolve this issue.

Example:
When i capture and store data.
And navigating to some screens
Then i return captured data

Thanks in Advance

@wakaleo
Copy link
Member

wakaleo commented Apr 20, 2024

Are you calling these in the same scenario?

@geetha051993
Copy link
Author

yes

@geetha051993
Copy link
Author

yes scenario, but calling in different methods

@wakaleo
Copy link
Member

wakaleo commented Apr 21, 2024

That should work fine then. Here's an example:

    @Given("{actor} has a todo list containing {items}")
    public void that_James_has_an_empty_todo_list(Actor actor, List<String> items) {
        Serenity.setSessionVariable("todoItems").to(items);
        actor.wasAbleTo(Start.withATodoListContaining(items));
    }
    ...
    @Then("his/her todo list should contain {items}")
    public void todo_list_should_contain(List<String> expectedItems) {
        List<String> originalItems = Serenity.sessionVariableCalled("todoItems");
        assertThat(originalItems).isNotEmpty();

        theActorInTheSpotlight().should(seeThat(TheItems.displayed(), equalTo(expectedItems))
                .orComplainWith(MissingTodoItemsException.class, "Missing todos " + expectedItems));
    }

@geetha051993
Copy link
Author

can we call Serenity.setSessionVariable("todoItems").to(items); and List originalItems = Serenity.sessionVariableCalled("todoItems"); in page classes like below

A a=new A();
B b=new B();

@given("{actor} has a todo list containing {items}")
public void that_James_has_an_empty_todo_list(Actor actor, List items) {
a.storevalues(actor,items);
}
...
@then("his/her todo list should contain {items}")
public void todo_list_should_contain(List expectedItems) {
b.retrievevalues(expectedItems));
}

page classs:

class A(){

public void storevalues(Actor actor, List items){
Serenity.setSessionVariable("todoItems").to(items);
actor.wasAbleTo(Start.withATodoListContaining(items));

}}

Class B(){

public void retrievevalues(List expectedItems){
List originalItems = Serenity.sessionVariableCalled("todoItems");
assertThat(originalItems).isNotEmpty();

    theActorInTheSpotlight().should(seeThat(TheItems.displayed(), equalTo(expectedItems))
            .orComplainWith(MissingTodoItemsException.class, "Missing todos " + expectedItems));

}
}

@wakaleo
Copy link
Member

wakaleo commented Apr 21, 2024

Yes, it doesn't matter what class the methods are in as long as they are executed in the same thread.

@geetha051993
Copy link
Author

geetha051993 commented Apr 21, 2024

is there any way we can check that they are executed in the same thread or not. i am executing two testcases both are using same methods because of that testcases were failing(static variables), if i execute single testcase(using serenity sessionvariables) also capture data was not getting displayed. iam using java selenium

@wakaleo
Copy link
Member

wakaleo commented Apr 21, 2024

Are you trying to share data between test cases or within a single test case?

@geetha051993
Copy link
Author

with in a single testcase only.
actually 10 testcases steps will be same . only few validations will differ so we are capturing data and storing it in static variables then we are using that data for validations.

Example:

Class A(){

public static Map<String, List> agingDetailsforAppointment = new HashMap<String, List>();

public void capturedata(){
agingDetailsforAppointment.put("dynamic data"); // this data will change for every testcase

}

}

Class B(){

public void retrievevalues(){

agingDetailsforAppointment.get("id"); // every testcase we are capturing new data and validating here

}}

same variables but independent testcases, capturing data in same testcase and validating data also in that testcase only

@wakaleo
Copy link
Member

wakaleo commented Apr 21, 2024

Either the Serenity session data or native ThreadLocals should work fine for that (I use it all the time) - if it is not, there may be an issue in your test logic.

@wakaleo
Copy link
Member

wakaleo commented May 12, 2024

Closing presumed answered

@wakaleo wakaleo closed this as completed May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants