今回は SeleniumWebdriver のお話になります。
Selenium では、Wait の処理をある程度してくれる機能はないので、
クリック前に1呼吸置きたい!
とか、
文字を入力する前に、必ず削除から入りたい!
とか、
その人、そのシステムによって、いろいろ出てくると思います。
なので、じゃあ作っちゃおう!というコーナーになります。
operation_module.rb
# モジュールとして定義させます
module Operation
# 1呼吸置いてからクリックモジュール
def click(object)
sleep 1
object.click
end
# 削除してから入力モジュール
def input(object,input_string)
object.clear
object.send_key "#{input_string}"
end
# これを忘れるとめっちゃ怒られます
module_function :click
module_function :input
end
やろうとしている操作は[TestCafe]ページモデルを使用してみると同じです。
他にもテスト用のモジュールを作って、そちらも呼び出します。
Test.rb
require 'selenium-webdriver'
require_relative 'operation_module'
require_relative 'test_module'
driver = Selenium::WebDriver.for :chrome
driver.get "http://devexpress.github.io/testcafe/example/"
DisplayTitle = "Example"
MyName = "t_y_cafe"
OutputResult = "Thank you, t_y_cafe!"
# ページ要素
Title = driver.find_element(:xpath,'//*[@id="main-form"]/div/header/h1').text
NameBox = driver.find_element(:id,'developer-name')
SubmitButton = driver.find_element(:id,'submit-button')
# 画面タイトル確認
Test::equal(Title,DisplayTitle)
# 名前入力
Operation::input(NameBox,MyName)
# submit
Operation::click(SubmitButton)
# 出力確認
OutputText = driver.find_element(:id,'article-header').text
Test::equal(OutputText,OutputResult)
driver.quit
SeleniumWebdriver ではこうやってよく使う操作なんかはモジュール化して
好きなようにカスタマイズできるのがとても楽しいですね。
「好きなようにカスタマイズ」って言葉がもう、たまらないですね。