使用 Geb 验证下拉列表中的选定选项

Verify selected option in a drop-down using Geb

使用 Geb 验证 selected 选项的文本是否有更好的解决方案?我有 2 个下拉列表

日期格式

<select name="dateFormat" class="select2-single-drop-down form-control" style="width:100%" id="dateFormat">
<option value="MM/DD/YYYY">MM/DD/YYYY</option>
<option value="DD/MM/YYYY">DD/MM/YYYY</option>
</select>

时区

<select id="timeZonePreference" name="timeZonePreference" class="form-control">
<option value="America/Chicago">- Select time zone -</option>
<option value="Etc/GMT+12">GMT -12:00, Dateline Time  Marshall Islands </option>
<option value="US/Samoa">GMT -11:00, Samoa Time  Samoa </option>
<option value="US/Hawaii">GMT -10:00, Hawaii Time  Honolulu </option>
<option value="US/Alaska">GMT -09:00, Alaska Time (Anchorage)</option>
<option value="US/Pacific">GMT -08:00, Pacific Time (San Francisco)</option>
<option value="US/Arizona">GMT -07:00, Mountain Time (Arizona)</option>
<option value="America/Denver">GMT -07:00, Mountain Time (Denver)</option>
<option value="America/Chicago">GMT -06:00, Central Time (Chicago)</option>
<option value="America/Mexico_City">GMT -06:00, Mexico Time (Mexico City)</option>
</select>

我为每个下拉菜单创建了一个页面对象

modalDateDropdown {$("#dateFormat")}
modalTZoneDropdown {$("#timeZonePreference")}

我有以下方法来验证 selected 值的文本

static void verifyDDSelection (def selector, String expected){
    String b = selector.find('option', value:selector.getAt(0).value())*.text()
    assert b == expected
}

当我验证日期格式的selected文本时,方法returns [MM/DD/YYYY]

时区returns[- Select time zone -, GMT -06:00, Central Time (Chicago)]

技术上它工作正常,但我不喜欢括号作为字符串的一部分返回。 geb 有没有类似于 seleniums 的东西 select class?

在 Geb 中,您可以对表单元素使用更强类型的方法,so-called form control modules。它使使用表单元素变得更加容易,并让您摆脱额外的静态辅助方法。我已经像这样重现了你的情况:

HTML示例代码:

<html>
  <body>
    <form>
      Date Format
      <select name="dateFormat" id="dateFormat">
        <option value="MM/DD/YYYY">MM/DD/YYYY</option>
        <option value="DD/MM/YYYY">DD/MM/YYYY</option>
      </select>
      <p/>
      Time Zone
      <select id="timeZonePreference" name="timeZonePreference">
        <option value="America/Chicago">- Select time zone -</option>
        <option value="Etc/GMT+12">GMT -12:00, Dateline Time Marshall Islands</option>
        <option value="US/Samoa">GMT -11:00, Samoa Time Samoa</option>
        <option value="US/Hawaii">GMT -10:00, Hawaii Time Honolulu</option>
        <option value="US/Alaska">GMT -09:00, Alaska Time (Anchorage)</option>
        <option value="US/Pacific">GMT -08:00, Pacific Time (San Francisco)</option>
        <option value="US/Arizona">GMT -07:00, Mountain Time (Arizona)</option>
        <option value="America/Denver">GMT -07:00, Mountain Time (Denver)</option>
        <option value="America/Chicago">GMT -06:00, Central Time (Chicago)</option>
        <option value="America/Mexico_City">GMT -06:00, Mexico Time (Mexico City)</option>
      </select>
    </form>
  </body>
</html>

Geb 页面对象:

package de.scrum_master.Whosebug

import geb.Page
import geb.module.Select

class DateFormatTimeZonePage extends Page {
  static url = "file:///C:/Users/.../GebSpockSamples/src/test/resources/dateformat-timezone.htm"

  static content = {
    modalDateDropdown { $("#dateFormat").module(Select) }
    modalTZoneDropdown { $("#timeZonePreference").module(Select) }
  }
}

Geb 测试:

package de.scrum_master.Whosebug

import geb.spock.GebReportingSpec

class DateFormatTimeZoneIT extends GebReportingSpec {
  def "Check texts for selected drop-down elements"() {
    given:
    def page = to DateFormatTimeZonePage

    when: "selecting drop-down elements by unique value"
    page.modalDateDropdown = "DD/MM/YYYY"
    page.modalTZoneDropdown = "US/Samoa"

    then: "corresponding texts match"
    page.modalDateDropdown.selectedText == "DD/MM/YYYY"
    page.modalTZoneDropdown.selectedText == "GMT -11:00, Samoa Time Samoa"

    when: "selecting drop-down default element with non-unique value by text"
    page.modalTZoneDropdown = "- Select time zone -"

    then: "corresponding value matches"
    page.modalTZoneDropdown.selected == "America/Chicago"

    when: "selecting another drop-down element with non-unique value by text"
    page.modalTZoneDropdown = "GMT -06:00, Central Time (Chicago)"

    then: "corresponding value matches"
    page.modalTZoneDropdown.selected == "America/Chicago"
  }
}