elixir-wallaby / wallaby

Concurrent browser tests for your Elixir web apps.
https://twitter.com/elixir_wallaby
MIT License
1.66k stars 197 forks source link

Better names for screenshots #110

Closed NobbZ closed 4 years ago

NobbZ commented 7 years ago

I have configured wallaby to take screenshots when something went wrong.

This was fine when only a single test failed, but when there are multiple ones failing I do have problems to assign the correct screenshot to the test.

So it would be nice if either the testsname or some info from conn ("#{method}-#{path}") could be part of the name of the screenshot.

asiniy commented 7 years ago

@NobbZ it's better to allow to make take_screenshot/2 and user will be able to pass any string he wants to it.

@keathley WDYT?

NobbZ commented 7 years ago

This is only an option if take_screenshot/* is pipeable.

But I'd like to have an option which gives me a nicely named shot on failure. This way, I do get the picture when the test actually fails. I do not need to search the test in the suite, add the function call into it and remember to delete it after fixing the test case.

Still, beeing able to give scrrenshots which are taken on purpose via take_screenshot/* would be a cool feature for my QA department. Even more when I can change the resolution of the screenshot taken, to mimic a couple of devices. That way one could take a lot of a burden to manually visit a bunch of pages just to check if a change has crashed the design.

keathley commented 7 years ago

I'm fine with adding an option to take_screenshot that allows the user to specify the desired name of the screenshot. It might be useful to include a timestamp after the specified name just to help reduce collisions.

I would love to make the names more meaningful. My ideal would actually be to include the test name in the screenshot along with the action. That way when something breaks you can look at the screenshots and find the screenshot that matches your failing action.

fusillicode commented 7 years ago

Hi, I together with @gr0uch0dev and @xrmx tried to tackle this issue but we got stuck due the impossibility to access the tests context from the Wallaby.Browser.take_screenshot.

We tried to "climb up the stack" and look at the take_screenshot callers (e.g. Wallaby.find) but also at their level the context wasn't available.

Just to clarify, our investigations were aimed to automatically gave better names (e.g. inferred from tests names) to the screenshots taken in case of failures.

keathley commented 7 years ago

@fusillicode take a look at #156. The idea in that implementation is that we could pass the name of the test in as an optional param when starting the session.

michallepicki commented 5 years ago

I also wanted a similar feature (naming the screenshots with the test filename and timestamp). Additionally I have a lot of custom assertions and other hacks. Using the :screenshot_on_failure option was not feasible because my tests were failing and screenshots were not getting taken.

What I did is wrap my tests in a custom my_test macro that wraps the ExUnit test macro and adds code for taking screenshots:

  defmacro my_test(message, var, do: block) do
    contents = [
      do:
        quote do
          try do
            unquote(block)
          rescue
            e ->
              filename =
                to_string(:erlang.system_time()) <>
                  "_" <> Path.basename(__ENV__.file, ".exs")

              try do
                take_screenshot(var!(session), name: filename)
              rescue
                _ ->
                  IO.puts("\nFailed to make a screenshot")
              end

              reraise(e, __STACKTRACE__)
          end
        end
    ]

    quote do
      test(unquote(message), unquote(var), unquote(contents))
    end
  end

Maybe this helps someone