mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 08:46:53 +08:00
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
package goquery
|
|
|
|
import (
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestAttrExists(t *testing.T) {
|
|
EnsureDocLoaded()
|
|
|
|
if val, ok := Doc().Root.Find("a").Attr("href"); !ok {
|
|
t.Error("Expected a value for the href attribute.")
|
|
} else {
|
|
t.Logf("Href of first anchor: %v.", val)
|
|
}
|
|
}
|
|
|
|
func TestAttrNotExist(t *testing.T) {
|
|
if val, ok := Doc().Root.Find("div.row-fluid").Attr("href"); ok {
|
|
t.Errorf("Expected no value for the href attribute, got %v.", val)
|
|
}
|
|
}
|
|
|
|
func TestText(t *testing.T) {
|
|
txt := Doc().Root.Find("h1").Text()
|
|
if strings.Trim(txt, " \n\r\t") != "Provok.in" {
|
|
t.Errorf("Expected text to be Provok.in, found %s.", txt)
|
|
}
|
|
}
|
|
|
|
func TestText2(t *testing.T) {
|
|
txt := Doc().Root.Find(".hero-unit .container-fluid .row-fluid:nth-child(1)").Text()
|
|
if ok, e := regexp.MatchString(`^\s+Provok\.in\s+Prove your point.\s+$`, txt); !ok || e != nil {
|
|
t.Errorf("Expected text to be Provok.in Prove your point., found %s.", txt)
|
|
if e != nil {
|
|
t.Logf("Error: %s.", e.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
/*func TestText3(t *testing.T) {
|
|
txt := Doc().Root.Find(".pvk-gutter").First().Text()
|
|
if ok, e := regexp.MatchString(`^\s+$`, txt); !ok || e != nil {
|
|
t.Errorf("Expected spaces, found %v.", txt)
|
|
if e != nil {
|
|
t.Logf("Error: %s.", e.Error())
|
|
}
|
|
}
|
|
}
|
|
*/
|
|
|
|
func TestHtml(t *testing.T) {
|
|
txt, e := Doc().Root.Find("h1").Html()
|
|
if e != nil {
|
|
t.Errorf("Error: %s.", e)
|
|
}
|
|
|
|
if ok, e := regexp.MatchString(`^\s*<a href="/">Provok<span class="green">\.</span><span class="red">i</span>n</a>\s*$`, txt); !ok || e != nil {
|
|
t.Errorf("Unexpected HTML content, found %s.", txt)
|
|
if e != nil {
|
|
t.Logf("Error: %s.", e.Error())
|
|
}
|
|
}
|
|
}
|