自学内容网 自学内容网

Ruby+Selenium教程

什么是 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::Test
  def test_page_title
    driver = Selenium::WebDriver.for :chrome
    driver.manage.window.maximize
    driver.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.click
    input = 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.click
    assert_equal(driver.title, "Restful-booker-platform demo")
    driver.quit
  end
end

新建test_baidu.rb文件

require 'minitest/autorun'
require 'selenium-webdriver'

class BaiduTest < MiniTest::Test
  def setup
    @browser = Selenium::WebDriver.for :chrome
    @browser.manage.timeouts.implicit_wait = 20
  end

  def 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.click
    assert(@browser.title.eql? "百度一下,你就知道")
  end

  def teardown
    @browser.close
  end
end

运行测试

打开终端输入

ruby test_page_title.rb
ruby test_baidu.rb


原文地址:https://blog.csdn.net/weixin_39810558/article/details/144690361

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!