in reply to Re: (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox
in thread (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox

Thanks Corion. Validating the field values outside the form is not an issue. As I mentioned earlier, right now I am doing the form submission skipping the javascript validation. But the webpage under test which I am trying to automate, sometimes give invalid alert messages based on the values in certain fields. These messages are generated by the javascript. So if I ignore the javascript validation in the webpage, form will be submitted successfully but the error with the javascript goes unnoticed.

For example, if I submit the form manually by selecting the 'OK' button, the webpage gives an alert message which says "Please complete the red entry" even when there are no empty fields. And then webpage submission does not get initiated. This is the main reason for which I cannot ignore the alert boxes in the webpage.

Now I believe that I made my point clear to you. If it does, that justifies this lengthy reply. Please help me to sort out this issue.

  • Comment on Re^2: (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox

Replies are listed 'Best First'.
Re^3: (OT) How to override the javascript alert when submitting a form using WWW::Mechanize::Firefox
by Corion (Patriarch) on Sep 01, 2011 at 06:43 UTC

    You still don't show any Perl code, so I can't comment on how you call your Javascript and how it fails for you.

    Personally, I check Javascript validation functions without actually submitting the form:

    # Set up values to be tested ... $mech->field( $name => $value ); ... my ($value, $type) = $mech->eval_in_page('validateIpSettings()'); is($value, 'some expected value');

    As your Javascript already is structured nicely, this conveniently eliminates the need for overriding window.alert. If you still want to override window.alert, you can do so by passing in the appropriate hash in the $env parameter to ->eval_in_page():

    # create myalert as Javascript function my $myalert = $mech->declare(<<JS); function (msg) {}; JS my ($value, $type) = $mech->eval_in_page('validateIpSettings()', { + alert => $myalert });

      Corion, my script also gives me a javascript alert.

      My larger script uploads many data files. Some data trigger the alert and others don't, but I have no way of predicting which data will trigger the alert. The alert is not a problem, other than the fact that I need to get past it.

      When the alert occurs, I can use $mech->back to escape it. However, I don't know of a way to detect when it occurs.

      "$mech->is_visible()" in conditional statements don't work, as you can see if you run the script--the alert takes over the whole screen, but "form_with_fields('speciesList')" is returned as visible. Neither "$mech->submit()" nor "$mech->submit_form()" work for me on this webpage--the form just gets cleared and the page reloads without submitting.

      Any ideas? I would like to either disable the javascript alert (but not all javascript) at this step, or detect when it is triggered and use "$mech->back" to escape it.

      #!/usr/bin/perl use strict; use warnings; use WWW::Mechanize::Firefox; my $mech = WWW::Mechanize::Firefox->new(autoclose => 0); $mech->get('http://david.abcc.ncifcrf.gov/summary.jsp'); $mech->click({ xpath => ('//*[@href="Upload a Gene List or Population" +]', single => 1), synchronize => 0 }); $mech->form_name('frmManager'); $mech->set_fields( 'pasteBox' => "RRI1, YLR149C, FUN19, YBR285W, ALD3, CUE5, +RTC3, AIM3, YDL199C, FMP45, AST2, GIP2, YKL091C, YKL133C, GLG2, SYM1, VHS3, YJR124C, MCH1, MHO +1, IKS1, RKM1, STF2, SAC1, ATG7, YAK1", 'Identifier' => "OFFICIAL_GENE_SYMBOL", ); $mech->click ({ xpath => ('//input[@name="rbUploadType" and @value="li +st"]', single => 1), synchronize => 0 }); $mech->click({xpath => ('//*[@value="Submit List"]', single => 1), syn +chronize => 0}); sleep 2; if ($mech->is_visible($mech->form_with_fields('speciesList'))) { print "It's visible.\n"; }

        There is no convenient way to detect window.alert() from WWW::Mechanize::Firefox. You can assign a dummy function to window.alert to override it. This will prevent alert boxes popping up until the next page load:

        my $window= $mech->repl->expr('window'); $window->{alert}= sub {}; ...

        Alternatively, you can put the override into Javascript directly:

        $mech->eval_in_page(<<'JS'); window.alert= function() {}; JS