简体   繁体   English

如何使用watir获取列表标签文本

[英]How to get list label text using watir

I am trying to use WATIR to automate testing of a web application. 我正在尝试使用WATIR来自动测试Web应用程序。 I have the application functionality working but am struggling on the login page as it requests two questions to provide random characters from the passphrase. 我的应用程序功能正常运行,但是在登录页面上却很挣扎,因为它要求两个问题来提供密码短语中的随机字符。

In the code snippet below this is requesting characters 8 and 1. 在下面的代码段中,这是要求字符8和1。

            <!--Deal with Passphrase Authentication-->
            <p>Enter the requested characters of your Passphrase below</p>
            <ul class="passphrase-list">
              <li>
                <label style="float:left; margin-right:10px;" for="PATHM_LTR1">Enter Letter 8</label>
                <input type="password" size="1" class="passphrase" maxlength="1" name="PATHM_LTR1" />
              </li>
              <li>
                <label style="float:left; margin-right:10px;" for="PATHM_LTR2">Enter Letter 1</label>
                <input type="password" size="1" class="passphrase" maxlength="1" name="PATHM_LTR2" />
              </li>
            </ul>

I was hoping to retieve the label text to determine which character to use in specifying the passphrase. 我希望重新整理标签文本,以确定在指定密码短语时使用哪个字符。 The first part of the text 'Enter Letter ' is the same in all cases it's just the number that changes. 在所有情况下,文本“输入字母”的第一部分都是相同的,只是数字有所变化。

Any help would be appreciated. 任何帮助,将不胜感激。

Try this: 尝试这个:

require 'watir-webdriver'

browser = Watir::Browser.new
browser.goto 'http://localhost:3000'
labels = browser.labels(text: /Enter Letter \d+/)

password = "AelloworlZ"

labels.each do |label|
  md = label.text.match(/\d+/)

  if md
    number = md[0].to_i 
    #puts number
    #puts password[number-1]
    browser.text_field(name: label.for).set(password[number-1])
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM