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

Method Chaining #928

Closed
entrptaher opened this issue Oct 1, 2017 · 8 comments
Closed

Method Chaining #928

entrptaher opened this issue Oct 1, 2017 · 8 comments

Comments

@entrptaher
Copy link
Contributor

entrptaher commented Oct 1, 2017

You can chain the commands on other Nightmare, Chromeless etc. You can provide selector along with type(). Like the following. A total of 188 characters. The chain methods are still doable with the seperate awaits.

const awesomeness = await chromeless
  .goto('https://example.com')
  .type("*****", "#login_username")
  .type("*****","#login_password")
  .click("button")
  .wait(20000)
  .screenshot()

for the same thing we have to write the await multiple times. A total of 244 characters.

await page.goto('https://example.com');
await page.click("#login_username")
await page.type("*****")
await page.click("#login_password")
await page.type("*****")
await page.click(" button")
await page.waitFor(2000) 
await page.screenshot({path: 'screenshot.png'});

What should be done to achieve the chain methods for clean coding and innerpeace?

@Janpot
Copy link
Contributor

Janpot commented Oct 1, 2017

I think functions should have meaningful results that are relevant and predictable to what the function does. I think this principle should go before counting characters when designing an API. I think almost all of these functions can return a result that can be useful in some scenario. Fluent APIs are probably one of the most abused API design patterns.

@Garbee
Copy link
Contributor

Garbee commented Oct 1, 2017

According to a previous implementation, #212, we simply need to see a good level of people requesting the functionality. For now it is possible to implement it in a wrapper system, so we'd rather not have it in core unless there is a strong need for it.

@entrptaher
Copy link
Contributor Author

Well, I don't disagree with @Janpot. Everyone has their own perspective, that's why there are debates over Atom/Sublime, Mocha/Jest, Single Quote/Double Quote, Vue/React, Same line Braces/ Different line Braces.

From my perspective, I have a really huge file with lots of lines you know, and if I repeat the

await page.doThis()
await page.doThat()
await page.doSomething()

etc. multiple times, that await things really starts to bug, it's not about the code count, it's about the clean structure (the definition of clean structure is different to everyone) where I can read the chain and follow along. I ended up wrapping the codes either inside a function, or inside a promise chain.

Let's see if someone else want's this too. :) Should I close this issue and wait for someone else to create a similar issue?

@Janpot
Copy link
Contributor

Janpot commented Oct 2, 2017

Ah, ok, if it's as a secondary API like in #212 I have no problem with it. But then it'd probably make more sense as a wrapper package? Like fluent-puppeteer or so.

@Garbee
Copy link
Contributor

Garbee commented Oct 2, 2017

Honestly, my recommendation here is someone build a wrapper to provide a chainable API. That can then be used and tested live in its own space and once any issues are hammered out it could be brought up for discussion to be included in core.

That keeps the core focus here on the primary API until launch. While allowing people to have a chain option to spot issues and work out kinks in case it decides to be brought into core. And if it still doesn't make it into core, you just have a solid wrapper package for developers to use who want that style.

@Janpot
Copy link
Contributor

Janpot commented Oct 2, 2017

yep, makes more sense as a "userland" package

@aslushnikov
Copy link
Contributor

I like the fluent-puppeteer package. It should be possible to implement a quite general approach similarly to #212.

If someone comes up with one, we'd be happy to refer to it from our docs.

@lopugit
Copy link

lopugit commented Mar 23, 2019

Did anyone happen to make this?

https://npmjs.com/package/node-horseman did it really fkn beautifully

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants