简体   繁体   中英

Calling a hidden value triggers an error in ruby with watir-webdriver

I'm currently still learning ruby and making slow but study progress on writing an automation suite for our in house CMS system but when attempting to delete an image entry I'm hitting a problem.

At its most basic, when a new image is added to a library it sets an ID against the item which remains static once assigned. Thanks to Justin Ko i can amend the item, but now i want to call the delete button for the item through ruby and watir-webdriver.

The HTML code for the block where the id in question is located is:

 <form action="" method="get" style="float:left; margin-bottom: 0; padding-right: 5px; padding-top: 5px">
 <input type="hidden" name="module" value="image" id="module" />
 <input type="hidden" name="action" value="amend" />
 <input type="hidden" name="imageHeadingID" value="152" />
 <input type="submit" name="submitButton" value="Amend" class="btn btn-primary btn-small" />
 </form>

<form action="" method="get" style="float:left; margin-bottom: 0; padding-top: 5px">
<input type="hidden" name="module" value="image" id="module" />
<input type="hidden" name="action" value="amend" />
<input type="hidden" name="imageHeadingID" value="152" />
<input type="submit" name="submitButton" value="Delete" class="btn btn-danger btn-small" />
</form>

Extracting the numeric value id aa reference at the image entry creation stage using;

@@grabelement = $browser.hidden(:name, "imageHeadingID")
@@id = @@grabelement.value

when calling the 'amend' function I use;

form_element = $browser.hidden(:name => 'imageHeadingID', :value => "#{@@id}").parent
form_element.button(:value => 'Amend').click

when attempting to call the 'delete' function I'm using;

form_element = $browser.hidden(:name => 'imageHeadingID', :value => "#{@@id}").parent
form_element.button(:value => 'Delete').click 

but where the amend code works, using the delete code triggers an exception of;

Watir::Exception::UnknownObjectException: unable to locate element, using {:value=>"Delete", :tag_name=>"button"}

Given that both are essentially the same function I can't see why I'm getting this error or how to work around it. Any help would be appreciated

The problem is the line:

form_element = $browser.hidden(:name => 'imageHeadingID', :value => "#{@@id}").parent

This says to find the first hidden input with name "imageHeadingID and value "152". In other words, the code is expecting the Amend and Delete button to be in the same form:

<form action="" method="get" style="float:left; margin-bottom: 0; padding-right: 5px; padding-top: 5px">
  <input type="hidden" name="module" value="image" id="module" />
  <input type="hidden" name="action" value="amend" />
  <input type="hidden" name="imageHeadingID" value="152" />
  <input type="submit" name="submitButton" value="Amend" class="btn btn-primary btn-small" />
  <input type="submit" name="submitButton" value="Delete" class="btn btn-danger btn-small" />    
</form>

You are instead going to need to look for the Delete button that is a sibling of the hidden field. One approach is to iterate through the buttons to find one that has the expected sibling:

# Amend
button = $browser.buttons(:value => 'Amend').find { |button| 
  button.parent.hidden(:name => 'imageHeadingID', :value => "#{@@id}").exists?
}
button.click

# Delete
button = $browser.buttons(:value => 'Amend').find { |button| 
  button.parent.hidden(:name => 'imageHeadingID', :value => "#{@@id}").exists?
}
button.click

Some might prefer to use CSS-selectors (or XPath) in these types of situations:

# Amend
button = $browser.button(:css => "input[name='imageHeadingID'][value='#{@@id}'] ~ input[value='Amend']")
button.click

# Delete
button = $browser.button(:css => "input[name='imageHeadingID'][value='#{@@id}'] ~ input[value='Delete']")
button.click

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