main_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package main
  2. import (
  3. "log"
  4. "testing"
  5. "github.com/playwright-community/playwright-go"
  6. )
  7. var pw *playwright.Playwright
  8. var browser playwright.Browser
  9. func setup() {
  10. var err error
  11. if err = playwright.Install(); err != nil {
  12. log.Fatalf("could not install playwright dependencies")
  13. }
  14. pw, err = playwright.Run()
  15. if err != nil {
  16. log.Fatalf("could not start playwright")
  17. }
  18. browser, err = pw.Chromium.Launch()
  19. if err != nil {
  20. log.Fatalf("could not launch browser: %v", err)
  21. }
  22. log.Print("Setup is complete")
  23. }
  24. func teardown() {
  25. if err := browser.Close(); err != nil {
  26. log.Fatalf("could not close browser: %v", err)
  27. }
  28. if err := pw.Stop(); err != nil {
  29. log.Fatalf("could not stop Playwright: %v", err)
  30. }
  31. log.Print("Teardown is complete")
  32. }
  33. // TestMain can be used for global setup and teardown
  34. func TestMain(m *testing.M) {
  35. setup()
  36. _ = m.Run()
  37. teardown()
  38. }
  39. func getBaseURL() string {
  40. return "http://0.0.0.0:8282"
  41. // return "https://pixivfe.exozy.me"
  42. }
  43. func checkIfPageHasError(page playwright.Page) bool {
  44. hasErr, _ := page.Locator(".error").IsVisible()
  45. return hasErr
  46. }
  47. func TestBasicGetHomepage(t *testing.T) {
  48. page, err := browser.NewPage()
  49. if err != nil {
  50. t.Errorf("could not create page: %v", err)
  51. }
  52. if _, err = page.Goto(getBaseURL() + "/"); err != nil {
  53. t.Errorf("could not goto: %v", err)
  54. }
  55. artworks, err := page.Locator(".artwork-small").All()
  56. if err != nil {
  57. t.Errorf("could not get entries: %v", err)
  58. }
  59. if len(artworks) != 50 {
  60. t.Errorf("number of daily ranking artworks is %d. expected: 50", len(artworks))
  61. }
  62. }
  63. func TestBasicAllRoutes(t *testing.T) {
  64. page, err := browser.NewPage()
  65. if err != nil {
  66. t.Errorf("could not create page: %v", err)
  67. }
  68. testPositiveURLs := []string{
  69. // Discovery pages
  70. "/discovery",
  71. "/discovery?mode=r18",
  72. "/discovery/novel",
  73. "/discovery/novel?mode=r18",
  74. // Ranking pages
  75. "/ranking",
  76. "/ranking?content=all&date=20230212&page=1&mode=male",
  77. "/ranking?content=mangas&date=20221022&page=3&mode=weekly_r18",
  78. "/ranking?content=ugoira&date=&page=1&mode=daily_r18",
  79. "/rankingCalendar?mode=daily_r18&date=2018-08-01",
  80. // Artwork page
  81. "/artworks/121247335",
  82. "/artworks/120131626", // NSFW
  83. "/artworks-multi/121289276,121247331,121200724",
  84. // Users page
  85. "/users/810305",
  86. "/users/810305/novels",
  87. "/users/810305/bookmarks",
  88. }
  89. for _, url := range testPositiveURLs {
  90. if _, err = page.Goto(getBaseURL() + url); err != nil {
  91. t.Errorf("could not goto: %v", err)
  92. }
  93. if checkIfPageHasError(page) {
  94. log.Fatalf("Path's response NOT OK: %s", url)
  95. }
  96. }
  97. }