lucky_flow icon indicating copy to clipboard operation
lucky_flow copied to clipboard

Add more element assertions for clearer specs

Open matthewmcgarvey opened this issue 4 years ago • 2 comments

In many of my specs where I write flow.el("@flow-id", text: "my text"), I find myself forgetting to write .should be_on_page. Often I remember because my test shows a false-positive but I've found places where I didn't remember after looking back at some of my specs. The reason that this gives false positives is because it is lazily resolving the element.

Every time I supply text to the #el call, and I would assume the majority of time most people do, is to make this assertion. I think it makes sense to add assertions to make this more explicit.

Ideas

  • [x] flow.el("@flow-id").should have_text("my text")
  • [ ] flow.el("@flow-id").should have_value("my value")
  • [ ] flow.el("@flow-id").should be_disabled

This would avoid the lazy loading but also make expectations more explicit. This is easier to accomplish in ruby rspec tests because they could translate that to a method on the element called #has_text? but we will have to explicitly make those Spec expectations.

matthewmcgarvey avatar May 22 '20 17:05 matthewmcgarvey

An example of broken specs I just found: https://github.com/matthewmcgarvey/validon/blob/3475a9028a3e00ec7c1ab0345fc9521dfc830847/spec/flows/toggle_experiment_spec.cr#L13-L15

matthewmcgarvey avatar May 22 '20 17:05 matthewmcgarvey

The original idea was to have one assertion method that worked for everything. But you make some very good points, especially since Lucky is very much about safety and not letting things bite you unintentionally. I'd be all for adding these assertions.

Also, I think they could still be lazy loaded since the expectation can have code to actually find the element. This may require adding a method to the element class. It's been awhile since I looked at that so I'm not 100% sure

EDIT: I think the way this would work is:

  • Making inner_text a property so you can set the value https://github.com/luckyframework/lucky_flow/blob/b00a8d82df5fa1ba687be9715f8f8b9344d2b027/src/lucky_flow/element.cr#L3
  • Add a HaveTextExpectation similar to https://github.com/luckyframework/lucky_flow/blob/master/src/lucky_flow/expectations.cr, but it first does element.inner_text = the_passed_in_text and then calls displayed. Same with the other ones. They would call whatevre method on element and then check displayed?

Does that make sense? I think this could be pretty simple and I agree it'll probably result in accidentally missing expectations 👍

paulcsmith avatar May 22 '20 19:05 paulcsmith