How to force onClick event with Selenium? - testing

I am testing an application that have the following html code:
...
<input
type="button"
class="picto modif"
onclick="hideDiv('divGlobalResult');showDiv('divForm')"
/>
When the user clicks on this button, the div divGlobalResult is hidden and the div divForm is shown.
I want to simulate this action with Selenium. Here is how I did:
focus css=input.picto.modif
fireEvent css=input.picto.modif click
However, the divForm appears while the second div divGlobalResult stays on the screen...
What am I missing?
function hideDiv(idDivElement) {
$("#" + idDivElement).css("display", "none");
}
function showDiv(idDivElement) {
$("#" + idDivElement).css("display", "");
}

thats happening because Selenium click function cannot simulate a real "Click" action performed with the mouse.
I have also faced this problem and the solution that best suited me was to take control of the mouse by using java robot and performing the click with it.
I hope this helps.

Related

Clicking button with selenium doesn't throw exception but the button isn't actually clicking?

I am trying to click a button, which once clicked some text should appear. Selenium is not throwing any errors which means the buttons should have been clicked however the text is not appearing (it works manually).
<button class = "redButton one">
<img src = "images/name.png" class = redImage">
"Name"
</button>
I tried to click the button with the xpath: "//button[contains(text(), 'Name')]". I don't understand why the text is not appearing.
What is the expected text to be displayed after button click event? In order to help with this request, provide the HTML snippet of <button> before & after click event. Since this is working fine manually, did you try adding adequate wait time for the text to display?

Geb : Unable to click on an element

Geb automation : Failing to click on element. Can someone help me to resolve this.
Note : Button is available at left bottom of the page but it is visible.
data-haspromo="" data-element="add" id="qa_button_0" type="button" class="amain btn primary" data-formname="" data-partnumber="12314" data-count="0" data-crossitem="" data-noncompliant="" data-partpreferred="false">Add
Here is my code to click on a button in page. It is failing to identify the element and click on it.
def clickAdd() {
Thread.sleep(3000)
waitFor (60){$("#qa_button_0").displayed}
$('#qa_button_0').click()
Thread.sleep(3000)
}

How to scroll a modal window in Capybara

I'm testing with Selenium WebDriver, RSpec and Capybara.
I let the program successfully fill in some fields in a modal window. Now I want to click on a button that is at the bottom of this modal window. At the first glance, I cannot see this button, so Capybara needs to scroll down in the modal window.
The two relevant code snippets of the webpage:
<div class = “modal”> </div>
<button class=”btn …..”> TextOnButton ::after </button>
I tried:
within('.modal') do
find('.btn', text: ‘TextOnButton').scrollIntoView(true)
end
but received the error message.
Unable to find visible css ".btn" with text "TextOnButton"
I tried:
within('.modal’) do
page.execute_script 'window.scrollBy(0,100)'
end
but then he scrolls the main window but not the modal window.
Assuming your HTML snippet is incorrect and the button element is actually contained in the modal (as in your text description), then you can try something like
within('.modal') do
btn = find(:button, 'TextOnButton', visible: :all)
execute_script('arguments[0].scrollIntoView(true)', btn)
btn.click
end
This work:
execute_script('arguments[0].scrollIntoView(true)', element)
I understand you are already executing code on the modal, but just wondering have you used driver.switchTo().window(handle) to switch to that modal first?

How to keyboard navigate to reCAPTCHA in a modal dialog?

I need to open Google's latest reCAPTCHA widget in a popup (modal) dialog, a Dojo Dialog in my case, and I've got that working fine, but I just realized that the user cannot keyboard navigate to it.
When the reCAPTCHA widget is displayed in the main view, not a modal dialog, then of course the user can easily keyboard navigate to it.
Has anyone found a way to set focus on the reCAPTCHA widget so that the user can access it without a mouse when the reCAPTCHA is in a Dojo Dialog?
I did see that reCAPTCHA is generated within an <iframe>. Is that part of the hurdle - that keyboard navigation can't reach content within an iframe? I've even tried to call document.getElementById("recaptcha-anchor") since I saw that that's the id of the <span> that holds the "checkbox" - but that is returning null. How to reach an element within an iframe?
I have a jsfiddle example available for demonstration at
https://jsfiddle.net/gregorco/xqs8w5pm/5/
<script>
var onloadCaptchaCallback = function() {
console.log("jsfiddle: rendering captcha");
globalRecaptchaWidgetId = grecaptcha.render('captchaDiv', {
'sitekey' : '6LcgSAMTAAAAACc2C7rc6HB9ZmEX4SyB0bbAJvTG',
'callback' : verifyCaptchaCallback,
'tabindex' : 2
});
grecaptcha.reset();
}
var verifyCaptchaCallback = function(g_recaptcha_response) {
console.log("Response validated. Not a robot.");
};
</script>
<script src='https://www.google.com/recaptcha/api.js?onload=onloadCaptchaCallback&render=explicit' async defer></script>
<div id="testDiv">
<button type="dojo/form/Button" onClick="captchaPopup.show();">Open reCAPTCHA</button>
</div>
<div data-dojo-type="dijit/Dialog" data-dojo-id="captchaPopup" title="Human Verification" style="width:350px;">
Cannot keyboard navigate to the checkbox!
<table>
<tr>
<td>
<div id="captchaDiv"></div><br/>
</td>
</tr>
</table>
</div>
Give this fiddle a try. Normally Dijit dialogs don't work too well with iframes in them because it doesn't know how to parse the content inside an iframe. In this case, we can use some of Dojo's functions to work around it. One notable thing to point out is that I've disabled autofocus of the Dijit Dialog so that it won't automatically focus the closeNode inside the dialog.
After the dialog loads, tab>space will select the captcha.
This may help others facing similar issue, but with Bootstrap modal dialog. I found the following solution on GitHub. Add the following Javascript to override Bootstrap:
Bootstrap 3x
$.fn.modal.Constructor.prototype.enforceFocus = function () { };
Bootstrap 4x
$.fn.modal.Constructor.prototype._enforceFocus = function () { };

Watin AttachTo popup window

We are using Watin to automate the website testing and try to implement the following: Click on a button to bring up popup, click on the Generate button on the popup window to generate file. We wasnt able to get the popup window using watin. ERROR: "Could not find an IE window matching constraint: Attribute 'href' matches 'Regenerate'. Search expired after '30' seconds." Please give a hint on what has gone wrong. Your help would be much appreciated.
The code snippet as below:
ieInstance.Button(Find.ByName(Button1)).Click();
ieInstance.WaitForComplete();
var popupUrl = new Regex("Regenerate");
//part of popup URL: "RegenerateDialog.aspx?Type="
var popupBrowser = Browser.AttachTo<IE>(Find.ByUrl(popupUrl));
if (popupBrowser != null)
popupBrowser.Button(Find.ById("RegenerateDialogBtn")).Click();
The popup window code as below:
function ShowEXPFileWindow(Type)
{
//do some stuff
var oWnd = window.radopen("RegenerateDialog.aspx?Type=" + Type, "RegenerateDialog");
oWnd.set_title("");
oWnd.center();
}
HTML to bring up popup window:
<td><input type="submit" name="ctl00$Body$ConfigRadGrid" value="Regenerate" onclick="ShowEXPFileWindow('OD'); return false;" class="submit" /></td>
Instead of
ieInstance.Button(Find.ByName(Button1)).Click(), use
ieInstance.Button(Find.ByName(Button1)).ClickNoWait()
and check. Is it possible to post the image of the pop-up window? or when you click on button, which control is selected by default? If it is 'Regenerate' then try to press 'Enter' Key and check.
Is the pop window is another IE window? If yes, then attach it using
Find.ByUrl(url => url.Contains(expectedURL))