使用Geb

时间:2018-02-28 21:43:43

标签: jquery geb

使用Geb验证所选选项的文本是否有更好的解决方案?我有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")}

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

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

当我验证日期格式的选定文本时,该方法返回[MM/DD/YYYY]

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

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

1 个答案:

答案 0 :(得分:2)

在Geb中,您可以对表单元素使用更强类型的方法,即所谓的form control modules。它使得处理表单元素变得更加容易,并且可以摆脱额外的静态帮助程序方法。我已经复制了你的情况:

HTML示例代码:

870530414842019

Geb页面对象:

<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.stackoverflow

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) }
  }
}