puppeteer-rubyを開発していて、いよいよ単体試験を書くことにした。
そこで問題となったのが、「ボタンだけを含むHTMLページを開く」みたいな試験。
本家Puppeteerでは静的なHTMLページをいくつか用意してHTTPサーバーを立ててmocha(JavaScriptのテスト)を流しているっぽいのだけど、単体試験としてそれはどうなの?
describe 'click button' do http_server do get '/button.html' do <<~HTML <html> <head><title>button</title></head> <body> <button onclick='document.body.innerHTML="123"'>click here</button>" </body> </html> end end it 'can click button' do page.goto('/button.html') page.click('button') expect(page.body).to eq("123") end end
こんな感じで必要最低限のHTMLを返すHTTPサーバーを都度立てるspecを書けたらいいのに?と思った。