Example #1
0
// TestFinders validates the utility functions which help in retrieving underline
// markup.
func TestFinders(t *testing.T) {
	body := generateMarkup()

	if _, err := trees.GetStyle(body, "width"); err != nil {
		t.Fatalf("\t%s\t  Should have been able to find style in markup", failed)
	}
	t.Logf("\t%s\t  Should have been able to find style in markup", success)

	if _, err := trees.GetAttr(body, "id"); err != nil {
		t.Fatalf("\t%s\t  Should have been able to find attr in markup", failed)
	}
	t.Logf("\t%s\t  Should have been able to find attr in markup", success)

	if len(trees.GetStyles(body, "width", "auto")) < 1 {
		t.Fatalf("\t%s\t  Should have been able to find style in markup with value of auto and name of width", failed)
	}
	t.Logf("\t%s\t  Should have been able to find style in markup with value of auto and name of width", success)

	if len(trees.ElementsUsingStyle(body, "width", "")) > 2 {
		t.Fatalf("\t%s\t  Should have been able to found more than two elements using width style", failed)
	}
	t.Logf("\t%s\t  Should have been able to found more than two elements using width style", success)

	if len(trees.ElementsUsingStyle(body, "id", "")) > 2 {
		t.Fatalf("\t%s\t  Should have been able to found more than two elements using id attr", failed)
	}
	t.Logf("\t%s\t  Should have been able to found more than two elements using id attr", success)

	if len(trees.ElementsUsingStyle(body, "width", "200px")) > 2 {
		t.Fatalf("\t%s\t  Should have been able to found more than two elements using width style with value of 200px", failed)
	}
	t.Logf("\t%s\t  Should have been able to found more than two elements using width style with value of 200px", success)

	if len(trees.ElementsWithTag(body, "div")) < 2 {
		t.Fatalf("\t%s\t  Should have been able to found two div element", failed)
	}
	t.Logf("\t%s\t  Should have been able to found two div element", success)

	if len(trees.ElementsWithTag(body, "label")) != 1 {
		t.Fatalf("\t%s\t  Should have been able to find a label element", failed)
	}
	t.Logf("\t%s\t  Should have been able to find a label element", success)
}
Example #2
0
func TestQueries(t *testing.T) {
	tree := trees.ParseAsRoot("section.tree-house#house", `
    <div class="wrapper" aria="wrapper-div">
      <section id="header" class="section"></section>
      <section id="menu" class="section"></section>
      <section id="content" class="section"></section>
    </div>

    <div class="links">
      <a rel="delay" href="#delay">Delay</a>
    </div>
  `)

	class, err := trees.GetAttr(tree, "class")
	if err != nil {
		tests.Failed(t, "Should have root with provided class property")
	}
	tests.Passed(t, "Should have root with provided class property")

	if _, val := class.Render(); val != "tree-house" {
		tests.Failed(t, "Should have class value matching 'tree-house': %q", val)
	}
	tests.Passed(t, "Should have class value matching 'tree-house'")

	id, err := trees.GetAttr(tree, "id")
	if err != nil {
		tests.Failed(t, "Should have root with provided id property")
	}
	tests.Passed(t, "Should have root with provided id property")

	if _, val := id.Render(); val != "house" {
		tests.Failed(t, "Should have class value matching 'house': %q", val)
	}
	tests.Passed(t, "Should have class value matching 'house'")

	if div := trees.Query.Query(tree, "div.wrapper"); div == nil {
		tests.Failed(t, "Should have returned a div with provided class 'wrapper'")
	}
	tests.Passed(t, "Should have returned a div with provided class 'wrapper'")

	if item := trees.Query.Query(tree, "section#menu"); item == nil {
		tests.Failed(t, "Should have returned a section with provided id 'menu'")
	}
	tests.Passed(t, "Should have returned a section with provided id 'menu'")

	if div := trees.Query.Query(tree, "div[aria*=wrapper-div]"); div == nil {
		tests.Failed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'")
	}
	tests.Passed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'")

	if div := trees.Query.Query(tree, "div[aria=wrapper-div]"); div == nil {
		tests.Failed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'")
	}
	tests.Passed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'")

	if div := trees.Query.QueryAll(tree, "div[aria*=wrapper-div]"); len(div) != 1 {
		tests.Failed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]': %d", len(div))
	}
	tests.Passed(t, "Should have returned a div with provided attr 'div[aria*=wrapper-div]'")

	items := trees.Query.QueryAll(tree, "section.section")
	if len(items) != 3 {
		tests.Failed(t, "Should have returned 3 elements for selector 'section.section': %d", len(items))
	}
	tests.Passed(t, "Should have returned 3 elements for selector 'section.section'")

}