什么是 Minitest?
Minitest 是 Ruby 的测试框架,提供一整套测试工具。它运行速度快,支持 TDD、BDD、模拟和基准测试
以下是使用Ruby、Selenium WebDriver和Minitest 的脚本,用于断言 Restful Booker Platform 的“页面标题”等于“Restful-booker-platform demo”
打开Rubymine新建项目
打开终端输入如下命令安装库
gem install selenium-webdriver
gem install webdrivers
gem install minitest
新建test_page_title.rb文件
require 'rubygems'
require "selenium-webdriver"
require 'minitest/autorun'class Test < Minitest::Testdef test_page_titledriver = Selenium::WebDriver.for :chromedriver.manage.window.maximizedriver.navigate.to "https://automationintesting.online/#/admin"wait = Selenium::WebDriver::Wait.new(timeout: 20)button = wait.until {element = driver.find_element(:xpath, "//button[text()='Let me hack!']")element if element.displayed?}button.clickinput = wait.until {element = driver.find_element(:id, "username")element if element.displayed?}input.send_keys("admin")input = wait.until {element = driver.find_element(:id, "password")element if element.displayed?}input.send_keys("password")button = wait.until {element = driver.find_element(:id, "doLogin")element if element.displayed?}button.clickassert_equal(driver.title, "Restful-booker-platform demo")driver.quitend
end
新建test_baidu.rb文件
require 'minitest/autorun'
require 'selenium-webdriver'class BaiduTest < MiniTest::Testdef setup@browser = Selenium::WebDriver.for :chrome@browser.manage.timeouts.implicit_wait = 20enddef test_baidu@browser.navigate.to "https://www.baidu.com"@search_box = @browser.find_element(:id, "kw")@search_box.send_keys("selenium")@search_box.submit@search_button = @browser.find_element(:id, "su")@search_button.clickassert(@browser.title.eql? "百度一下,你就知道")enddef teardown@browser.closeend
end
运行测试
打开终端输入
ruby test_page_title.rb
ruby test_baidu.rb