Online Training: Web test automation with CodeceptJS. Sign up now!

Chain Methods with CodeceptJS

Helloooo guys,

Aand I’m here again with newbie questions:

Someone here has already written chain methods in CodeceptJS? Any suggestion?

I’m stuck! :grimacing:

Well, why would you need to do so?
By design we don’t allow you to write chain methods. It’s easier to debug code when you have each call on each line without chaining.

Anyway, it’s our design decision to not to provide chaining syntax. One of the reasons, that some methods, like grabbers (with grab* prefix) do return value so they can’t be chained.

I see!

On the other hand, with chain methods, you get processes like fill in the forms more flexible in accord with scenarios. Without this, if you want to create something like that, you should create a method full of conditions.

My opinion!

Hi @andreyagra, can you provide an example of a methods that requires or would benefit from method chaining? I am curious because I believe that you can achieve the same result without using method chaining and being just as stable and reliable with CodeceptJS. I would like to help you achieve this so an example of what you are trying to solve/achieve would help me provide the correct solution for your situation.

Technically speaking, in automation best practices method chaining is accepted in Java and C# (although preferred it’s to use async code without chaining) however in other languages/libraries/frameworks, this is frowned upon because it increases debugging time and complexity without providing that much benefit.

In Locate method, for instance, you have some examples with chain methods:

locate(‘bla’).withAttr(‘blo’).withAttr(‘blu’)

In my case, I’m writing a method to fill in specific form in different scenarios… So, sometimes, I want to fill in price field, sometimes, don’t.

And once, you create a method to do this, I feel free to change my component, I’ll just adjust in one point of my suit test. I could enumerate advantages to use methods chaining here, but I think it’s not the focus about this topic.

But, I’m always open to suggestions…

I think the problem you are rising is solved well with page objects.
You can pass in an object with parameters and based on these parameters perform actions.

Could you show me an example how chaining reduces the usage of conditionals?

@davert, I’ve already gotten it using class inside of page objects.

But, I got your point…