Skip to content

Commit

Permalink
add: press refresh btn to scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
azuki774 committed Aug 7, 2024
1 parent 7418442 commit 1fd2306
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions internal/scenario/moneyforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ func (s *ScenarioMoneyForward) getBrowser(ctx context.Context) error {
l.Set("disable-gpu").Delete("disable-gpu")

// Launch with headful mode
l.Headless(true).XVFB("--server-num=5", "--server-args=-screen 0 1600x900x16")
l.Headless(false).XVFB("--server-num=5", "--server-args=-screen 0 1600x900x16")

s.browser = rod.New().Client(l.MustClient()).MustConnect()
return nil
}

func (s *ScenarioMoneyForward) login(ctx context.Context) error {
slog.Info("load loginpage start")
page := s.browser.Timeout(60 * time.Second).MustPage(mfCfURL).MustWaitStable()
page := s.browser.MustPage(mfCfURL).MustWaitStable()
slog.Info("load loginpage complete")

slog.Info("login information input")
Expand Down Expand Up @@ -110,6 +110,42 @@ func (s *ScenarioMoneyForward) pageDownload(ctx context.Context, fileName string
return nil
}

func (s *ScenarioMoneyForward) refreshFinIns(ctx context.Context) error {
refreshURL := "https://moneyforward.com/accounts"
page := s.browser.MustPage(refreshURL).MustWaitStable()

// 一括補正ボタンは押せないので、個別で「更新」を押す
tmpButtons, err := page.Elements(`[class="btn"]`) // 更新ボタンを全部1回集める
if err != nil {
return err
}

for _, btn := range tmpButtons {
s, err := btn.Attribute("value")
if err != nil {
// value がないボタンは skip
slog.Warn("get unexpected attribute", "err", err)
continue
}
if *s != "更新" {
// 更新のボタン以外は押さない
continue
}
n, err := btn.Attribute("name")
if err != nil || n == nil {
// btn で押せないものがあるのでそれは除外する
continue
}

// name="commit" && class="btn" のはず
btn.MustClick()
slog.Info("press refresh button")
}

time.Sleep(60 * time.Second) // 更新待ち
return nil
}

func (s *ScenarioMoneyForward) Start(ctx context.Context) (err error) {
fileName := filepath.Join(s.common.outputDir, "cf.html")
fileNameLm := filepath.Join(s.common.outputDir, "cf_lastmonth.html")
Expand All @@ -126,6 +162,12 @@ func (s *ScenarioMoneyForward) Start(ctx context.Context) (err error) {
return err
}

slog.Info("get refresh finins start")
if err := s.refreshFinIns(ctx); err != nil {
return err
}
slog.Info("get refresh finins end")

slog.Info("get cf page start")
page := s.browser.MustPage(mfCfURL).MustWaitStable()
slog.Info("get cf page complete")
Expand Down

0 comments on commit 1fd2306

Please sign in to comment.