简体   繁体   中英

How to get list label text using watir

I am trying to use WATIR to automate testing of a web application. 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.

            <!--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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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