使用Ruby进行自动测试:从下拉列表中选择一个选项

时间:2023-01-07 13:14:10

I write automated test with Ruby(Selenium framework) and I need to know how can I select an option from drop-down list.

我用Ruby(Selenium框架)编写自动化测试,我需要知道如何从下拉列表中选择一个选项。

Thanks in advance!

提前致谢!

5 个解决方案

#1


building on floehopper's answer:

建立在floehopper的答案上:

selenium.addSelection(locator, value)
or
selenium.select(locator, value)

You almost certainly want "id=my_select_box_id" (with the quotes) for locator, though other CSS selectors will work. value is the literal text value (not the display value) of the option to be selected.

您几乎肯定希望“id = my_select_box_id”(带引号)用于定位器,但其他CSS选择器也可以使用。 value是要选择的选项的文字文本值(不是显示值)。

#2


It sounds like you are writing a functional test here. Selecting it probably won't do you much good on its own. You need to submit the form in order to test the controller. :) It might help people answering to know which testing framework you are using, because there are several to choose from. If you are using RSpec, check out this screencast.

听起来你在这里写了一个功能测试。选择它可能不会对你有好处。您需要提交表单以测试控制器。 :)它可能有助于人们回答你知道你正在使用哪个测试框架,因为有几个可供选择。如果您使用的是RSpec,请查看此截屏视频。

Hope that helps anyway.

无论如何希望有所帮助。

#3


Aside from functional tests, if you're looking for something that acts a bit more like the real app, have a look at WebRat. For non-AJAXed integration tests, it has a very nice DSL for selection your DOMs and taking appropriate actions against them. (link-clicking form-filling etc.).

除了功能测试之外,如果您正在寻找更像真实应用程序的东西,请查看WebRat。对于非AJAXed集成测试,它有一个非常好的DSL,用于选择您的DOM并对它们采取适当的措施。 (链接点击表格填写等)。

On the other hand if your App is an external Web App that you just want to do acceptance tests on, you can also check Selenimum or Watir.

另一方面,如果您的应用程序是您只想进行验收测试的外部Web应用程序,您还可以检查Selenimum或Watir。

Note that WeRat is heavily web framework based where as Selenimum and Watir use the browser to interact with your web app directly (like a real user).

请注意,WeRat是基于Web框架的,Selenimum和Watir使用浏览器直接与您的Web应用程序交互(如真实用户)。

#4


I think you want this command :-

我想你想要这个命令: -

select(selectLocator, optionLocator)
  • selectLocator identifies the drop down list
  • selectLocator标识下拉列表

  • optionLocator identifies the option within the list
  • optionLocator标识列表中的选项

#5


Easiest way of doing this: select(selectLocator,optionLocator) as suggested above.

最简单的方法:按照上面的建议选择(selectLocator,optionLocator)。

selectLocator: name or xpath for dropdown object optionLocator: name or xpath for dropdown option to be selected

selectLocator:下拉对象的名称或xpath optionLocator:要选择的下拉选项的name或xpath

E.g.

@selenium.select "Language", "label=Ruby"

#1


building on floehopper's answer:

建立在floehopper的答案上:

selenium.addSelection(locator, value)
or
selenium.select(locator, value)

You almost certainly want "id=my_select_box_id" (with the quotes) for locator, though other CSS selectors will work. value is the literal text value (not the display value) of the option to be selected.

您几乎肯定希望“id = my_select_box_id”(带引号)用于定位器,但其他CSS选择器也可以使用。 value是要选择的选项的文字文本值(不是显示值)。

#2


It sounds like you are writing a functional test here. Selecting it probably won't do you much good on its own. You need to submit the form in order to test the controller. :) It might help people answering to know which testing framework you are using, because there are several to choose from. If you are using RSpec, check out this screencast.

听起来你在这里写了一个功能测试。选择它可能不会对你有好处。您需要提交表单以测试控制器。 :)它可能有助于人们回答你知道你正在使用哪个测试框架,因为有几个可供选择。如果您使用的是RSpec,请查看此截屏视频。

Hope that helps anyway.

无论如何希望有所帮助。

#3


Aside from functional tests, if you're looking for something that acts a bit more like the real app, have a look at WebRat. For non-AJAXed integration tests, it has a very nice DSL for selection your DOMs and taking appropriate actions against them. (link-clicking form-filling etc.).

除了功能测试之外,如果您正在寻找更像真实应用程序的东西,请查看WebRat。对于非AJAXed集成测试,它有一个非常好的DSL,用于选择您的DOM并对它们采取适当的措施。 (链接点击表格填写等)。

On the other hand if your App is an external Web App that you just want to do acceptance tests on, you can also check Selenimum or Watir.

另一方面,如果您的应用程序是您只想进行验收测试的外部Web应用程序,您还可以检查Selenimum或Watir。

Note that WeRat is heavily web framework based where as Selenimum and Watir use the browser to interact with your web app directly (like a real user).

请注意,WeRat是基于Web框架的,Selenimum和Watir使用浏览器直接与您的Web应用程序交互(如真实用户)。

#4


I think you want this command :-

我想你想要这个命令: -

select(selectLocator, optionLocator)
  • selectLocator identifies the drop down list
  • selectLocator标识下拉列表

  • optionLocator identifies the option within the list
  • optionLocator标识列表中的选项

#5


Easiest way of doing this: select(selectLocator,optionLocator) as suggested above.

最简单的方法:按照上面的建议选择(selectLocator,optionLocator)。

selectLocator: name or xpath for dropdown object optionLocator: name or xpath for dropdown option to be selected

selectLocator:下拉对象的名称或xpath optionLocator:要选择的下拉选项的name或xpath

E.g.

@selenium.select "Language", "label=Ruby"