Ruby / Watir : how to click on a disabled button (Watir::Exception::ObjectDisabledException) - ruby

I want to make a program in Ruby that create a github repository. Everything is all right, but when i want to click the button 'create repository' after filling the repository name, nothing happened and the program stop with a timeout error.
This is the html code of the disabled button :
<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…" disabled="">
Create repository
</button>
And the html code of the enabled button :
<button type="submit" class="btn btn-primary first-in-line" data-disable-with="Creating repository…">
Create repository
</button>
And this my ruby program
repo_name = gets.chomp
repo = browser.text_field(id: 'repository_name')
repo.set(repo_name)
browser.driver.manage.timeouts.implicit_wait = 3
create_button = browser.button(type: "submit")
create_button.wait_until(&:enabled?).click
I'am pretty sure that my pb comes that when i'm landing on the page, the button is disabled, and even if i'm filling the repository_name input, my prog can't access to the button.
So do you have a solution about that ? Or maybe do you know if there is an other pb ?
Edit :
When here is the code without the waiting commands :
repo_name = gets.chomp
repo = browser.text_field(id: 'repository_name')
repo.set(repo_name)
create_button = browser.button(type: "submit").click
And when I run it, I'v got a 'Watir::Exception::ObjectDisabledException' error
("element present, but timed out after 30 seconds, waiting for #
<Watir::Button: located: true; {:type=>"submit", :tag_name=>"button"}> to be enabled (Watir::Exception::ObjectDisabledException)"

I think below code might work for you
browser.text_field(id: 'repository_name').set("repo_name")
browser.send_keys(:tab)
And as Justin mentioned in his answer, click the create repository button as below:
browser.button(type: "submit", visible: true).click

The problem is that there are multiple submit buttons on the page. You can see this by retrieving a collection of buttons:
# Button text:
browser.buttons(type: 'submit').map(&:text_content)
#=> ["Set status", "Sign out", "Create repository"]
# Disabled status:
browser.buttons(type: 'submit').map(&:disabled?)
#=> [true, false, false]
browser.button(type: "submit") returns the first submit button on the page, which is the disabled "Set status" button.
The "Create repository" button is actually the last one on the page. A more specific locator for the button is required. Some options:
# By text
browser.button(text: 'Create repository')
# By visibility (since the other 2 are hidden by default)
browser.button(type: "submit", visible: true)

Related

How to click on a Popup Confirmation alert (Selenium Webdriver / Ruby)

I'm trying to click on a popup confirmation alert using Selenium Webdriver / Ruby, but even with the xpath I can't click on the OK or Cancel button.
(popup window: https://imgur.com/2E8dqKe)
HTML code:
<div>
<a onclick="$find('confirm1545915453689').close(true);" class="rwPopupButton" href="javascript:void(0);"><span class="rwOuterSpan"><span class="rwInnerSpan">OK</span></span></a>
<a onclick="$find('confirm1545915453689').close(false);" class="rwPopupButton" href="javascript:void(0);"><span class="rwOuterSpan"><span class="rwInnerSpan">Cancel</span></span></a>
</div>
Code I tried:
browser.find_element(:xpath => '//*[#id="confirm1545919261219_content"]/div/div[2]/a[2]/span/span').click
and
browser.find_element(:xpath => '//td[.="Cancel"]').click
Thanks for ur time
do you have any errors while trying to click?
try following locator:
browser.find_element(:xpath, "//span[#class='rwInnerSpan' and text()='OK']").click;
browser.find_element(:xpath, "//span[#class='rwInnerSpan' and text()='Cancel']").click;

click a button inside a form that is an image

When I do this
pp form
I get
{buttons [imagebutton:0x49cc436 type: image name: go value: ]}>
How do I submit when the button is a image? Since this won't work
button = form.image_with(:class => "go")
and the css
<input type="image" src="/images/btn_search_3.gif" name="go" class="go" alt="Sök">
Did you try:
form.submit form.button
Make an effort to look at the mechanize docs first before posting questions please.

How to show title below modal in magnific popup

I am opening an MVC view in magnific
I need to show a title (caption) below the modal
But caption is only for image type but I am using Ajax type.
I have set the title attribute on both the hyperlink that I click to open the modal and the root element of the view.
But it never shows up.
How do I do display the title (fixed or dynamic from a grid)? I wonder why author forgot to add this feature?
hyperlink:
<a class="edit-list" title="Edit List!" href="/manage/editlist/100">Edit</a>
Root element of the target page:
<form id="ListEditForm" class="white-popup" title="Edit List" action="/manage/EditList/100">
..fields go here
</form>
I followed this post, but he has markup, I don't:
http://codepen.io/dimsemenov/pen/zjtbr
Initialization:
$('.edit-list').magnificPopup({
type: 'ajax',
midClick: true,
callbacks: {
markupParse: function (template, values, item) {
values.title = item.el.attr('title');
}
}
});
This has been solved. I need to put the right container in the right place to render the title. The plugin won't drop any element to show the title.

Unable to click on 'Cancel' button

I'm writing automated script using Selenium WebDriver with Ruby. In the case, I've to click on 'Cancel' button and following is the html code for it:
<div class="ui-dialog-buttonset">
<button class="otherButtonClass" type="button" role="button" aria-disabled="false">
<span class="ui-button-text">Rename</span>
</button>
<button class="cancelButtonClass" type="button" role="button" aria-disabled="false">
<span class="ui-button-text">Cancel</span>
</button>
</div>
For clicking on 'Cancel' button, I wrote following:
driver.find_element(:xpath, "//button[#class='cancelButtonClass']").click
here click action doesn't happen. I tried sleep, wait.until { element.displayed? } still issue wasn't resolved. The error thrown is 'Element is not visible and hence may not be interacted with'
However, if I perform click action on 'Rename' button, it works:
driver.find_element(:xpath, "//button[#class='otherButtonClass']").click
Please help me to understand why this is happening. I'm confused, 'Rename' and 'Cancel' have similar html code and still clicking on 'Rename' passes and clicking on 'Cancel' fails. Why like this?
You can try the below :
script = <<-end
element = arguments[0];
element.setAttribute('aria-disabled','true');
return element;
end
# select the 'Cancel' button element
elem = driver.find_element(:css,'div.ui-dialog-buttonset>button')[1]
# setting the 'aria-disabled' to true
elem = driver.execute_script(script,elem)
#after enabling the css attribute 'aria-disabled' click on the
#cancel button
elem.click
Using CSS selection will not be a perfect solution in case if the Button CSS are dynamic for any hover actions. Also the Simple way of selecting given element is using the following xpath.
driver.find_element(:xpath, "//span[text()='Cancel']").click

Yii, Selenium and multiple submit buttons

I've got a form with two submit buttons which I want to test using Selenium.
View:
<?php
$form = $this->beginWidget('CActiveForm', array(
'id' => 'profile-form',
'enableAjaxValidation' => true,
'action' => '',
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => true,
'validateOnType' => false,
),
));
?>
<input name="cancel" type="submit" value="Cancel" />
<input name="submit" type="submit" value="Save changes" />
<?php $this->endWidget(); ?>
Controller is nothing special, you can assume it just prints "Your profile has been saved" or "Your profile was not saved" depending on what $_POST['cancel'] it gets
Test code:
<?php
$this->open('/profile_form_url');
$submit_button_selector = 'css=#profile-form input[name="submit"]';
$cancel_button_selector = 'css=#profile-form input[name="cancel"]';
$this->clickAndWait($cancel_button_selector);
$this->assertTextPresent('Your profile was not saved');
$this->open('/profile_form_url');
$this->clickAndWait($submit_button_selector);
$this->assertTextPresent('Your profile has been saved');
The problem is that code works great in browser but not when running tests in Selenium/Firefox. When running tests, it "sees" the first button only (Cancel), clicking "Save changes" has the same effect. If you place Save changes button first, it will not "see" Cancel button.
If you turn enableAjaxValidation off, it works both in browser and Selenium, but I'd like to have a more elegant solution of course. Like for example turning off the AJAX validation on clicking on Cancel.
No, the problem doesn't depend on which locator you use for buttons (xpath, css, id).
clickAndWait() calls waitForPageToLoad() - ajax form validation will generally not trigger this event (unless a page loads), so your test will never complete; this is probably why if you turn ajax validation off it works.
You might want to look at the other options selenium provides (this is an old link to the free phpunit pocket guide that describes some other options - it's based on phpunit 3.1 though) such as using click() and then using waitForCondition() with some javascript to run and return true if the new text is displayed.

Resources