boydm / phoenix_integration

Lightweight server side integration test tools for Phoenix
Apache License 2.0
219 stars 26 forks source link

Click to button method #19

Closed vtm9 closed 6 years ago

vtm9 commented 6 years ago

Hi! I have html like this:

<button 
 class="btn btn-info btn-block" 
 data-csrf="csrf=="
 data-method="post"
 data-to="/test?level=test"
 role="button">Test</button>

As I understand follow_link and click_link maethods works only with <a> tags. (Floki.find(html, "a"))

Is there any way to follow the buttons?

I can make PR, if this functionality is missing. Thanks all!

boydm commented 6 years ago

(I'm thinking this through, so sorry if it rambles a bit...)

I understand what you are asking for. Is the button link powered by javascript that you wrote or a commonly used library? When I want a button that acts like a link, I usually apply "btn" classes to the link via Bootstrap, which works without the use of javascript to make the link work.

I'm open to adding follow_button, or something like that. Don't think we should overload follow_link with buttons.

The thing is that following a button pretty much requires javascript unless the data-method / data-to attributes are standard enough now that we can rely on them without breaking under lots of implementations. Thus, why I was asking about what library you are using.

Let me know your thoughts here. Lets see if we can come up with a generic enough solution.

vtm9 commented 6 years ago

I am using standart phoenix_html js package and helper button/2 in html.

It works good for simple interfaces. I think for this code

<button 
 class="btn btn-info btn-block" 
 data-csrf="csrf=="
 data-method="post"
 data-to="/test?level=test"
 role="button">Test</button>

We could provide function click_button(conn, "Test"), which find button in html by name and send request to data-to path and data-method method with data-csrf param.

Maybe iplement function click_button(conn, "button_name", %{url: "data-to", method: "data-method", token: "data-csrf"}) which find button in html and try to find opts in button attributes.

boydm commented 6 years ago

That's pretty standard! Maybe I'll start using it too.

Yes. I think adding both click_button and follow_button functions is the way to go.

I've been in that code in the past few days, so I'll take a crack at it.

vtm9 commented 6 years ago

Cool, becase now in integration tests I am hand-parse the html in response, find button attributes and then make post-request. :+1:

boydm commented 6 years ago

OK. Should be in master. Can you please sync up to it and see if that does the trick for you?

I'm going to fix the other issue, which has been bugging me, while I'm here. So will push to hex after that is done and you confirm this works...

boydm commented 6 years ago

use this in deps to get the version from master...

{:phoenix_integration, git: "https://github.com/boydm/phoenix_integration.git" }

vtm9 commented 6 years ago

Ok I'll try

vtm9 commented 6 years ago

Thanks, it works good! But I need to specify method click_button("Test", method: :post) and get is default Is it possible to take method at data-method attribute by default?

boydm commented 6 years ago

Good point. I'll take a look.

boydm commented 6 years ago

Should now get the method from the button's data-method attribute unless it is otherwise specified in master.

Please update again and let me know how that works for you.

vtm9 commented 6 years ago

Ok

vtm9 commented 6 years ago

It's work great! Thanks!!!

boydm commented 6 years ago

Great. I'm going to close this issue and will probably push 0.5.0 out to hex later today or tomorrow.

Thank you much for the quick turn around testing and all that on this.

vtm9 commented 6 years ago

Thank you for the speed! Maybe you should update Readme and add there info about buttons.

boydm commented 6 years ago

Already added them to the documentation. I'll sprinkle sample calls into the readme too.

boydm commented 6 years ago

0.5.0 is pushed to hex.

{:phoenix_integration, "~> 0.5", only: :test}