Phantomjs in handling onclick events using ruby-selenium - javascript

<INPUT onclick="return verifyCheckedForEdit('UserAdminForm', 'itemDelUserId','hiddenUserId', 'Disable')" id=disableButton class=buttonBlue type=submit value=Disable name=Disable>
The above is my tag.I tried various options:
disable=$driver.find_element(:xpath,"html/body/div[4]/div[1]/div[2]/form/table/tbody/tr[2]/td/input[2]")
disable.submit
$driver.find_element(:name,"Disable").send_keys :enter
$driver.action.move_to(disable).double_click(disable).perform
$driver.action.move_to(disable).click(disable).perform
Nonone of the above work to click this button.

I am taking a screenshot after the click however I notice that the screenshot does not have the modal window using phantom js:
I tried to maximize the screen:
$driver.manage.window.maximize
And then I tried to click confirm on the window. But nothing happens.So I am stuck with not knowing if phantomjs actually rendered the modal window and also is it actually clicking on the modal popup.
I found a solution for this if anybody needs it:
$driver.execute_script('window.alert = function(){};')
$driver.execute_script('window.confirm = function(confirmationText) {return true; };')
delete=$driver.find_element(:name,"Delete").click
Before you click on the disable/delete button please create a modal window and then click on it.

Related

Cypress - How to redirect without opening a new window

I have a cypress test that on clicking a button the system redirects to a new window.
I've tried some solutions (invoke,stub) without success.
I need to do some testing but I need it to always be redirected on the same screen as cypress doesn't support multiple windows.
My button code:
<button id="cmdEnviar" name="cmdEnviar" type="button" onclick="TrPage._autoSubmit('_id5','cmdEnviar',event,1);return false;" class="x7j">Enviar</button>
My code:
it('register new user', function() {
cy.visit('/')
cy.get('#txtLogin').type(Cypress.env('login'))
cy.get('#txtSenha').type(Cypress.env('pass'))
cy.get('#btnEnviar').click()
cy.get('#cmbSistemas').select(11)
cy.get('#cmdEnviar').click() //here is the button
//here is redirected to another window
cy.contains('Colaborador').click()
cy.contains('Cadastro').click()
})
Could anyone help me, if there is any solution?
Unfortunately, without seeing what your onclick event actually does, I can't recommend a strategy for validating the behavior of onclick is functioning as intended.
Instead, we can validate that the button has the correct onclick attribute value.
...
cy.get('#cmdEnviar')
.should('have.attr', 'onclick', "TrPage._autoSubmit('_id5','cmdEnviar',event,1);return false;");
...

Selenium test - OK/Cancel popup disappearing immediately

Odd one here. When testing this manually I click the delete button, wait for the popup
Then I click OK and the record is removed as expected.
But when I try to do the same in Java/Selenium it goes like this->
WebElement element = _driver.findElement(By.id("btnDeletePatient"));
JavascriptExecutor executor = (JavascriptExecutor)_driver;
executor.executeScript("arguments[0].click();", element);
or
_driver.findElement(By.id("btnDeletePatient")).click();
Both have the same response, the OK/Cancel popup will appear and then immediately vanish.
This is the code for the button
<input type="submit" name="ctl00$cpContentLeft$btnPatient"
value="Delete User" onclick="return userDelete();"
id="ctl00_cpContentLeft_btnPatient"
tabindex="81" class="btn btn-outline-primary btn-sm mr-3">
And this is the code for the function userDelete
function userDelete() {
if (confirm("Are you sure you wish to delete this user?")) {
return true;
}
else {
return false;
}
}
Also I have now tried the same in Edge, same thing so this does not appear to be a Chrome issue.
Anyone got any ideas what is causing this please?
Further tests as follows.
I place a breakpoint just at the point the script will click the delete run and run the script. The page loads as expected and I get to the problem point.
I manually click the Delete button, the popup appears and then stays until I click cancel.
Using this code I step through and the popup appears and then instantly disappears.
_driver.findElement(By.id("ctl00_cpContentLeft_btnDelete")).click();
Using this code the response is the say as 2.
_driver.findElement(By.id("ctl00_cpContentLeft_btnDeletePatient")).sendKeys(Keys.SPACE);
Then I try a double click and nothing happens.
On all tests I see no errors in the console.
I hope you are clicking the delete button before page loads completely, that is why pop is disappearing suddenly. please add wait before clicking delete button.
Steps:
Load the page completely (add wait here)
Click the delete button
click Okay button.
please add some more codes here and screenshots too. that would be more helpful
This looks like a java script alert. In selenium, we have specific way to handle alert. You do not need to click on ok. You can first wait for alert to be present. If it is present, then switch to that alert.
Then, We have specific methods to handle alerts in selenium.
alert.accept() clicks on ok.
alert.dismiss() clicks on cancel.
Sample code from selenium documentation:
driver.findElement(By.linkText("See a sample prompt")).click();
//Wait for the alert to be displayed and store it in a variable
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
//Type your message
alert.sendKeys("Selenium");
//Press the OK button
alert.accept();
//Press the Cancel button
alert.dismiss();
//Store the alert in a variable for reuse
String text = alert.getText();
Ok found it
The issue was within the chromedriver, this line fixed it
chromeOptions.setCapability("unexpectedAlertBehaviour", "ignore");
Now the popup remains until actioned in the script.
We live and learn, thanks for all your help.

Reset modal window when closed

I'm using wordpress with visual composer and I have built this demo popup example https://www.air8cushion.com/index.php/popup-test/ when I click on "More Info" button the popup shows up. However when I scroll content inside of it, and close the modal window, and again click on "More Info" I need to reset the content to appear from the top again.
I know its possible to do it with JS but I had fails many times to make it work. Most of examples of similar things online are mostly for Bootstrap and none of them worked for me.
Thanks.
You can use this on click of the close button of your popup
this function will scroll viewport of the popup div to top when clicked
jQuery(document).on('click','.vc_general',function(){
var myDiv = document.getElementById('myNav');
myDiv.scrollTop = 0;
});
".vc_general" is one of the class in your close button. You can add an extra class if you think it will affect the other functionality.

Fancybox default close does not work after form redirect

I've created my page using bootstrap and have a link to fire off a fancybox box as below:
<a class="iframe" href="AForm.asp?ID=<%=Request("ID")%>"><button class="btn-warning">Action Form</button></a>
JQuery
<script type="text/javascript">
$(document).ready(function() {
$("a.iframe").fancybox({
'type' : 'iframe',
'width':500,
'height':500,
'afterClose':function () {
window.location.reload(); }
});
});
</script>
The default functionality works fine to begin with: i can trigger fancybox to load, click the default cross icon and it closes the box - all working as expected.
When i submit a form within the fancybox (html/classic asp) and it does a redirect to a standard HTML page that literally just says "Please close this page" the default close cross icon does nothing. It is visible and in the correct place but there is no click functionality.
I have tried custom close buttons within the HTML page but nothing works - it just doesn't want to close at all. Anyone got any ideas? Thanks
Not so much an answer but after trying to fix this all day i gave up on Fancybox and found Colorbox to work pretty much out of the box for what i was trying to do. Colorbox

jquery tabbed ui - javascript in loaded code not working anymore

I am trying to use the jQuery tabbed widget as a menu for my application and have managed to set up the tabbed interface so that when you click a tab it shows the first page of that section. According to the docs, if you click on a link on this page and want it to load in the same tab then you need to use the code below. This partially works because now when I click on an image-hyperlink on that page it renders the new page in the tabbed window.
However, when I click on a button which has javascript the button no longer works. This is the case for all of the javascript. How can I get the javascript working again under the new tabbed interface?
$('#example').tabs({
load: function(event, ui) {
$(ui.panel).delegate('a', 'click', function(event) {
$(ui.panel).load(this.href);
event.preventDefault();
});
}
});
EDIT: So if you add the following line to LeftyX's code in the page 1 file, then the button causes page4.html to open but does not do it in the tab
<button onclick="location.href='Page4.html'">Test button </button>
It must work.
If you show us your HTML we might try to help.
You can check my sample and download the code.
Try and check if there are any errors in your script using some Dev Tools (ex: Google Chrome Developer Tools)
UPDATE:
if you're using a button you can use some HTML5 attribute:
<button data-pagelink="Page4.html">Test button</button>
and trap the click event:
$(ui.panel).delegate('button', 'click', function(event, o) {
$(ui.panel).load(this.dataset.pagelink);
event.preventDefault();
});
or use an hyperlink and transform it in a button:
<a id="thisIsAButton" href="Page4.html">go to page 4</a>
with this:
$("#thisIsAButton").button();

Categories