Selenium 简明教程
Selenium with Ruby Tutorial
Selenium 可以使用多种语言,比如 Java、Python、Kotlin、JavaScript、Ruby 等等。Selenium 被广泛用于 web 自动化测试。Selenium 是一个开源且可移植的自动化软件测试工具,用于测试 Web 应用程序。它具有跨不同浏览器和操作系统运行的能力。Selenium 不仅是一个单一的工具,而是一套工具,可帮助测试人员更有效地自动化基于 Web 的应用程序。
Selenium can be used with multiple languages like Java, Python, Kotlin, JavaScript, Ruby, and so on. Selenium is used extensively for web automation testing. Selenium is an open-source and a portable automated software testing tool for testing web applications. It has capabilities to operate across different browsers and operating systems. Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently.
从 Selenium 4 版本开始,整个架构可以完全兼容 W3C - 世界联盟,这意味着 Selenium 4 遵循 W3C 给出的全部标准和指南。
From the Selenium 4 version, the entire architecture is fully compatible with W3C - World Wide Consortium meaning Selenium 4 follows all the standards and guidelines given by W3C.
How to Setup Selenium With Ruby?
Step 1 - 使用以下链接下载并安装本地系统中的 Ruby -
Step 1 − Download and install Ruby in the local system using the below link −
通过运行以下命令来确认已安装的 Ruby 版本 -
Confirm the version of the Ruby installed by running the below command −
ruby -v
执行的命令的输出将表示系统中安装的 Ruby 版本。
The output of the command executed would signify the Ruby version installed in the system.
Step 2 - 从以下链接下载并安装 Ruby 代码编辑器 RubyMine 以编写并运行 Selenium 测试 -
Step 2 − Download and install the Ruby code editor RubyMine from the below link to write and run the Selenium test −
Step 3 - 启动 RubyMine 并点击新建项目按钮。
Step 3 − Launch RubyMine and click on the New Project button.
Step 4 - 输入项目名称(比如 SeleniumTest),选择 Ruby 解释器,然后点击创建按钮。
Step 4 − Enter a project name, say SeleniumTest, select a Ruby Interpreter, then click on the Create button.
Step 5 - 右键点击 SeleniumTest 项目,点击新建选项,然后点击文件选项。
Step 5 − Right click on the SeleniumTest project, click on the New option, and then click on the File option.
Step 6 - 输入文件名(比如 FirstTest.rb)作为新建文件名称,然后按 Enter。
Step 6 − Enter a filename, say FirstTest.rb under the New File field, and then press Enter.
Step 7 - 通过在 FirstTest.rb 文件中添加以下代码片段来确认 Ruby 解释器是否正确配置 -
Step 7 − Confirm if the Ruby Interpreter is configured correctly by adding the piece of below code in the FirstTest.rb file −
puts 'Tutorialspoint'
Step 8 - 通过右键点击并选择运行 FirstTest 的选项来运行代码。
Step 8 − Run the code by right clicking and selecting the option Run FirstTest.
它将显示以下输出 −
It will show the following output −
Tutorialspoint
Process finished with exit code 0
在以上示例中,消息 - Tutorialspoint 被捕获到控制台中并收到了消息 Process finished with exit code 0 ,表示代码执行成功。
In the above example, the message - Tutorialspoint got captured in the console, and the message Process finished with exit code 0 was received, signifying successful execution of the code.
Step 9 - 为了安装 Selenium,从终端运行以下命令 -
Step 9 − To install Selenium, run the below command from the terminal −
gem install selenium-webdriver
Step 10 - 在 FirstTest.rb 文件中添加以下代码。
Step 10 − Add the below code in the FirstTest.rb file.
require 'selenium-webdriver'
# Initiate Webdriver
driver = Selenium::WebDriver.for :edge
# adding implicit wait of 15 seconds
driver.manage.timeouts.implicit_wait = 15
# launch an application
driver.get 'https://www.tutorialspoint.com/selenium/practice/text-box.php'
# get page title
puts 'Page Title: ' + driver.title
它将显示以下输出 −
It will show the following output −
Page title: Selenium Practice - Text Box
Process finished with exit code 0
在以上示例中,我们首先启动 Edge 浏览器并打开一个应用程序,然后检索浏览器标题,并在控制台中显示消息 - Page title: Selenium Practice - Text Box 。与此同时,Chrome 浏览器启动,在顶部显示消息 Edge is being controlled by automated test software 。
In the above example, we had first launched the Edge browser and opened an application then retrieved the browser title and in the console with the message - Page title: Selenium Practice - Text Box. Along with that Chrome browser got launched with the message Edge is being controlled by automated test software at the top.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Launch a Browser and Quit a Driver With Selenium Ruby
我们可以使用 get 方法启动浏览器并打开应用程序,最后使用 quit 方法退出浏览器。
We can launch the browser and open an application using the get method, and finally quit the browser with the quit method.
Code Implementation
require 'selenium-webdriver'
# Initiate Webdriver
driver = Selenium::WebDriver.for :edge
# adding implicit wait of 15 seconds
driver.manage.timeouts.implicit_wait = 15
# launch an application
driver.get 'https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php'
# get page title
puts 'Browser title after launch: ' + driver.title
# close browser
driver.quit
它将显示以下输出 −
It will show the following output −
Browser title after launch: Selenium Practice - Student Registration Form
Process finished with exit code 0
在以上示例中,我们首先启动 Edge 浏览器,然后检索浏览器标题,最后退出浏览器,并在控制台中收到消息 - Browser title after launch: Selenium Practice - Student Registration Form 。
In the above example, we had first launched the Edge browser then retrieved the browser title, finally quit the browser, and in the console received the message - Browser title after launch: Selenium Practice - Student Registration Form.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Identify An Element and Check Its Functionality using Selenium Ruby
一旦我们导航到某个网页,我们必须与该页面中可用的 web 元素进行交互,比如点击链接/按钮、在编辑框中输入文本等,以完成我们的自动化测试用例。
Once we navigate to a webpage, we have to interact with the web elements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.
为此,我们的第一任务应该是识别元素。我们可以将链接文本用于链接以进行其识别并利用方法 find_element(name: '<value of name attributes>')。通过此项操作,应返回与名称属性匹配值匹配的第一个元素。
For this, our first job should be to identify the element. We can use the link text for a link for its identification and utilize the method find_element(name: '<value of name attributes>'). With this, the first element with the matching value of the name attribute should be returned.
如果不存在与名称属性匹配值匹配的元素,则应引发 NoSuchElementException。
In case there is no element with the matching value of the name attribute, NoSuchElementException should be thrown.
让我们查看下图中编辑框的 html 代码 −
Let us see the html code of the edit box in the below image −
<input id="fullname" name="fullname" type="text" class="form-control" placeholder="Full Name">
上图中高亮显示在 FullName: 标签旁边的编辑框具有名称属性,其值为 fullname 。让我们在对其进行识别后向此编辑框中输入文本 Selenium 。最后,我们将退出浏览器。
The edit box beside the FullName: label highlighted in the above image has a name attribute with a value as fullname. Let us input the text Selenium into this edit box after identifying it. Finally we would quit the browser.
Code Implementation
require 'selenium-webdriver'
# Initiate Webdriver
driver = Selenium::WebDriver.for :edge
# adding implicit wait of 15 seconds
driver.manage.timeouts.implicit_wait = 15
# launch an application
driver.get'https://www.tutorialspoint.com/selenium/practice/text-box.php'
# identify element then enter text
name = driver.find_element(:name ,'fullname')
name.send_keys("Selenium")
# close browser
driver.quit
在上面的示例中,我们首先启动 Edge 浏览器并打开了一个应用程序,然后在输入框中输入文本 Selenium。
In the above example, we had first launched the Edge browser and opened an application then entered the text Selenium in the input box.
最后,收到了消息 Process finished with exit code 0 ,表示代码成功执行。
Finally, the message Process finished with exit code 0 was received, signifying successful execution of the code.
Conclusion
这结束了我们对 Selenium Ruby 教程教程的全面了解。我们首先介绍如何使用 Ruby 设置 Selenium 并使用 Selenium Ruby 退出会话,如何使用 Selenium Ruby 启动浏览器和退出会话,以及如何使用 Selenium Ruby 识别元素并检查其功能。
This concludes our comprehensive take on the tutorial on Selenium Ruby Tutorial. We’ve started with describing how to set up Selenium with Ruby and quit a session using the Selenium Ruby, how to launch a browser and quit a session using the Selenium Ruby, and how to identify an element and check its functionality using Selenium Ruby.
Selenium Ruby 教程给您提供了深入的知识。明智的做法是坚持学习您已经学到的内容,并探索与 Selenium 相关的其他内容,以加深您的理解并扩展您的视野。
This equips you with in-depth knowledge of the Selenium Ruby Tutorial. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.