Giter Site home page Giter Site logo

test_selector's Introduction

TestSelector

CircleCI Module Version Hex Docs Total Download License Last Updated

A set of Elixir helpers to set/get test selectors to/from elements in a Phoenix app (when Mix.env == :test). These selectors are hashed (scoped to the view module they are used in) for accurate selection and "leakage" prevention. See our Wiki for examples.

Why? Because we prefer reserving classes for styling, and id's have limitations because they have to be unique.

Dependencies

  • Floki v0.25.x and higher

Installation

def deps do
  [
    {:test_selector, "~> 0.3.3"}
  ]
end

Use TestSelector.HTML.Helpers to your web.ex.

  • In phoenix 1.2.0 at web/web.ex
  • In newer Phoenix versions lib/your_project/web/web.ex

Add somewhere along:

def view(opts \\ []) do
  quote do
    ...
    use Phoenix.HTML

    use TestSelector.HTML.Helpers
    ...
  end
end

Testing with Floki

In the tests import the TestSelector.Test.FlokiHelpers:

defmodule Project.Web.MyTest do
  ...

  import TestSelector.Test.FlokiHelpers
  ...
end

Tests

Run the tests:

mix test

Copyright and License

Copyright (c) 2017 Defacto

Released under the MIT License, which can be found in the repository in LICENSE.md.

test_selector's People

Contributors

dkln avatar easterpeanut avatar fatboypunk avatar kianmeng avatar kuret avatar sn3p avatar tarzan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

test_selector's Issues

Add retries option to the search and find functions

Just like Hound's find_element/3 I'd like to add the retries option:

def find_test_element(view_module, retries \\ 5)
def search_test_element(view_module, retries \\ 5)

By default Hound retries 5 times. But when I want to make sure a page is lacking an element, I might want to check only once:

assert search_test_element(UserView, "does-not-exist", 1) == {:error, :no_such_element}

Multiple retries significantly increases the testing duration.

Looks like adding an optional attribute will conflict in our function arity, so need to find a workaround.

Extend the Hound `find_element` function

This would be nice:

def find_element(:test, view_module, retries \\ 5) do
  {:safe, test_selector} = view_module.test()
  find_element(:xpath, ~s|//*[@#{test_selector}]|, retries)
end

find_element(:test, MyModule)

But this :(

** (CompileError) lib/test_selector/test_helpers.ex:1: imported Hound.Helpers.Page.find_element/2 conflicts with local function

Add helpers and examples for usage with Floki

We should mention we're also supporting Floki out of the box:

html
|> Floki.find("[test-selector=#{SomeView.test_selector("user")}")
|> Floki.attribute("test-value")
{:safe, selector} = IndexView.test()
Floki.find(html, "[#{selector}]")

And we might want to rename TestSelector.Test.Helpers to TestSelector.Hound.Helpers, and optionally add TestSelector.Floki.Helpers (if any).

Make the README houndless

Since using test_selector can be done without, and is done without using hound, can we move the hound mentions to the Wiki and keep the README clean?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.