1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[SeleniumWebdriver][Ruby]よく使う操作をモジュール化

Last updated at Posted at 2018-09-30

今回は 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 ではこうやってよく使う操作なんかはモジュール化して
好きなようにカスタマイズできるのがとても楽しいですね。
「好きなようにカスタマイズ」って言葉がもう、たまらないですね。

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?