Welcome back.
So let's understand what is world constructor in Cucumber?
So I'll try to explain you in very layman terms.
Okay.
So consider this is a world.
This scenario has its own world.
Okay, so this is executing what all it needs to satisfy the scenario called placing the order.
Okay, so if we consider this entire scenario as one world, that means let's say if you have some variables
in this step definition, I would be able to access them in any of the steps below in the complete scenario,
because this is one single place.
Assume that this is one single earth.
Okay, so a guy can go anywhere, right?
So whatever variables you access here, those can be accessed here.
Or this step, this step, any step, as long as you are inside that scenario.
That's what they were saying.
But practically how it is possible.
Now, if you go back to step definition file, so what you did here, you declared a page variable here.
Okay.
So the scope of this variable dies within this block because this is not a global variable, right?
So this is a local variable which is defined inside this block.
Now, here also, you might use other things.
Now for give you a quick example.
If you use Page anywhere here, will it work?
No, because this is again empty.
This do not have any clue because the whatever you define here that dies after you come out of this
block, the scope of that only inside this.
Okay.
So one way is to make it global variable, declare on top and make it access.
So without using global variables, also you can talk between your scenario step definitions.
Okay, so all these are now interconnected.
You can talk to each other and share your variables happily without even declaring them in globally.
That's what these guys have brought this constructor.
It's very simple, guys.
Now I'll I'll show you this quick example.
Now here, PWA manager is there right now in my next method.
I'm using this technically as of now this do not have life, but what you can do instead of assigning
to this const variable.
Right like this, this dot.
So what happens is when you use this dot, this world constructor gets activated and it will place in
its world.
Okay.
And again, if this PWA manager, if you are trying to access any other method, as long as that method
stays in this scenario, world constructor is happy to share.
Okay.
This is like a global mission.
Now one of your friend needs this PWA manager.
Ideally this is NULL.
Why can't you share same instance which is active in this to here?
Yes.
So you can do that with this dot PWA manager.
Okay.
Call the same method again while assigning you will assign for this dot PWA manager and if you want
to retrieve you will just call this this dot manager.
Now what happens is scope will not end here.
The scope will carefully stored in the world constructor and whoever asked this, they will keep sharing.
So that's how you are.
You will not end up having issues.
So if you don't do like this and run it will say PWA manager is null.
That's you will get a null pointer exception which we are overcoming with a simple change.
Okay.
Ideally in normal frameworks to make this happen, you need to declare it globally or use some other
stuff.
If it's in Java, we use dependency injection a lot of code we need to wrap up.
But in JavaScript cucumber they have given a very simple thing called world where you can simply access
with this dot.
Okay.
Now here also PWA manager is there fine.
Same thing you can wherever manager is there you can use that.
Okay.
This is one way of figuring out and see if if you have anything else.
And here dashboard page is there.
Okay.
This does not have any life.
See where this is initialized?
Um.
Yeah.
So here where we we are initializing this first time the life is activated, but unfortunately, the
life is dying inside this block.
We can change that if you mark it as a this.so that it gets stored in its global variable.
And wherever it is there, you can write it here.
Okay.
So still there could be some changes we might be missing.
Let's run this test and then it will point out as errors which line we are still need to make modification
and we can do that.
Okay.
Instead of.
Going line by line.
Let it tell us what errors we have.
But before that, one quick thing.
So previously I asked to change it to username password.
Right.
I just shown as a demonstration.
But here, just make sure you bring back normal string how it got generated.
So string string.
This is the original, right?
So here you can catch them with username and password.
We need not replace here.
We should replace it here.
That's my mistake there.
Okay.
And another one, I think let's have a separate variable to declare this, you know.
Right.
Why we have written this Because we don't have access to Page.
So that's why we pulled out playwright object from their module and then got a browser from browser.
We got a context.
So const, let's have a separate constant defined instead of mixing that with.
Previous line.
Okay.
So you can get rid of it.
We don't require a test anymore.
Test is when you use a MOCA framework here where we used test, but we are exclusively depending on
cucumber.
Now we just need expect for assertion where we are using somewhere in the bottom here.
All good.
So let me run this test.
Definitely it will fail, but we need to know what places.
Okay, It failed.
It says c p manager is not defined.
Step number 17, it is saying.
Okay.
I know why.
Because we declared this variable.
Where in the world constructor.
Now again, to access this manager, you cannot directly simply write.
Manager.
It has to be accessed from the world constructor only like this.
Dot Manager.
Okay, that is one thing.
So yeah.
Let me run again.
Let's keep running this until we we know the solution.
Um.
Looks like it's going through now.
Oh, it failed.
So where are we missing?
Data is not defined.
Oh, of course.
This is the place right now.
We got this username from where we got this username.
That string.
What you are seeing here got captured here.
And from here it reaches here.
Okay, so this username we are using in our login, there is no data dot now we are not relying on MOCHA
framework to get the data.
Previously when we did Playwright Mocha, we got a data from this JSON parsing.
Right now we are driving the data from feature file as per cucumber standards.
So you need to just get rid of all that.
You cannot use Mocha framework and cucumber together either.
Use one of them.
Perfect.
So this looks good.
Okay.
And anything else here?
Also, we need string is what we are using, right?
So product name.
I would declare the variable as product name and I can directly call it here.
And similarly, same product name is coming here, making sure that product is displayed in the cart.
And these don't have any arguments.
Okay.
And another thing, guys, so basically any spec step file, I mean, this is one method, right?
Given this is when then.
So by default, Cucumber tries to execute your method in five seconds.
If it could not find in five seconds, your test will fail.
Okay, let's say if your method is not completed in five seconds, it's still trying to log in.
And if five seconds is done, if it still could not finish this method, then it will say test is failed.
So that's why whenever you write any method, make sure that method piece piece of information gets
executed.
Not more than five seconds.
In our case, everything is fast.
But this one particular given may take time because we are logging it open the browser, it has to click,
log in and then land on page.
All this might take little more time so we can override the default timeout by simply saying like this
timeout.
Let's give ten seconds at least.
So that means we are giving in milliseconds 100 into 1000 milliseconds equals to ten seconds, right?
So if you think any of your method takes more than five seconds, I recommend you to put timeout so
your step will not fail and it will wait until the whole execution is done here.
Okay, let's give one more run.
See, this time we might get rid of some errors.
See, I'm on live teaching you what kind of errors you have when you migrate your test to cucumber,
and we are trying to solve it.
Okay, so another error is dashboard page is not defined in step number 25.
That means see here we have accessed it in the world constructor.
That means you should update here as well.
Right?
That's the mandatory task, isn't it?
So see anywhere if you are using dashboard.
Okay, you updated it.
Okay.
Similarly, there is another this dot manager.
This is also we are sending into world constructor.
So I'm updating that with this.to access.
That's it.
I think we are all set.
So this time we should not see any errors because we have fixed all the world constructor stuff.
Okay, so let me run this.
So by default this run in headless mode, you know, right.
Until unless you mention that run head, it will just run in headless mode.
You see that it retrieved all the product details and it's given as confirmation order ID and now it's
checking on the last page.
Perfect.
You see one passed.
All the steps are successfully executed and if you carefully observe, execution is done.
But we are not seeing the terminal.
Okay.
It's still hanging there.
Okay.
Because you just can't run your tests like this.
It will run your test, but ultimately you have to give another keyword called exit.
So that's how it comes out of terminal.
If not, your test will show as hanged.
That means cucumber is still on executable mode.
You have to explicitly give hyphen hyphen exit so it will run all your test and it will come out of
the loop.
Now if you see something here, it is saying every time you are getting this message like this so you
can disable this message by adding this to your cucumber dot JS file.
What is this?
Can we do that?
So what is this cucumber dot js file?
So basically by default we know where is our cucumber dot?
JS Right.
It is an on bin directory, but if you want to give additional instructions to your cucumber dot js
to say I mean in this case it is saying disable this block.
I don't want to see this message.
Every time they were saying to use this.
Okay, in that case in your project level, simply create a new file.
Cucumber.
Dot.
JS Okay, and in that file, just provide this information.
So that means we are giving additional directions instructions to your cucumber on how it has to behave
on running your tests.
Okay, so just create this file on your project level and add this so you won't see this every time
it's there suggesting us to do that and we are doing it.
And if you want to see them running in head mode, then I would recommend you to again, where is our
step definition file?
So by default it runs in head mode that we are launching the chromium, but you can actually say to
run it in headless by simply having one property here, curly braces and here you say headless false.
So this time browser should open when you run your test.
Okay.
Three changes we made that it has to exit from the terminal.
This box should not come and it should run in head mode.
Let's see with hyphen hyphen exit.
Okay.
You see, the browser is open so you can visually see what's happening.
It's trying to add item to CART.
All right.
That's it.
Execution is done.
And you can see one past all the six steps are successfully passed.
So that's pretty much about this lecture.
In the next lecture, let's continue and see.
There are many futures, are there still in cucumber to explain, but they are very easy.
Hardest part is done, guys.
Now it's all cakewalk, which I'll show in the upcoming lectures.
Thank you.