THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

54 Zeilen
1.4KB

  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "bytes"
  7. "testing"
  8. "github.com/PuerkitoBio/goquery"
  9. "github.com/stretchr/testify/assert"
  10. )
  11. // HTMLDoc struct
  12. type HTMLDoc struct {
  13. doc *goquery.Document
  14. }
  15. // NewHTMLParser parse html file
  16. func NewHTMLParser(t testing.TB, body *bytes.Buffer) *HTMLDoc {
  17. t.Helper()
  18. doc, err := goquery.NewDocumentFromReader(body)
  19. assert.NoError(t, err)
  20. return &HTMLDoc{doc: doc}
  21. }
  22. // GetInputValueByID for get input value by id
  23. func (doc *HTMLDoc) GetInputValueByID(id string) string {
  24. text, _ := doc.doc.Find("#" + id).Attr("value")
  25. return text
  26. }
  27. // GetInputValueByName for get input value by name
  28. func (doc *HTMLDoc) GetInputValueByName(name string) string {
  29. text, _ := doc.doc.Find("input[name=\"" + name + "\"]").Attr("value")
  30. return text
  31. }
  32. // GetCSRF for get CSRC token value from input
  33. func (doc *HTMLDoc) GetCSRF() string {
  34. return doc.GetInputValueByName("_csrf")
  35. }
  36. // AssertElement check if element by selector exists or does not exist depending on checkExists
  37. func (doc *HTMLDoc) AssertElement(t testing.TB, selector string, checkExists bool) {
  38. sel := doc.doc.Find(selector)
  39. if checkExists {
  40. assert.Equal(t, 1, sel.Length())
  41. } else {
  42. assert.Equal(t, 0, sel.Length())
  43. }
  44. }