I'm having issues with logging into specific website via HtmlUnit methods.
The site form's submit button looks like this:
<td>
<input type="button" value="Login!" onclick="encPass(UTM_STUDIO_ADMIN);" class="normalButton">
</td>
Mine code snippet:
final HtmlButtonInput submitLogin = form.getInputByValue("Login!");
HtmlPage returnPage = submitLogin.click();
System.out.println(returnPage.asText());
Yet, it prints the logging site with username and password fields fulfilled, that's all.
WebClient config:
wclient.getOptions().setPrintContentOnFailingStatusCode(false);
wclient.getOptions().setCssEnabled(false);
wclient.getOptions().setThrowExceptionOnFailingStatusCode(false);
wclient.getOptions().setThrowExceptionOnScriptError(false);
wclient.getOptions().setUseInsecureSSL(true);
wclient.getOptions().setJavaScriptEnabled(false);
I've been already trying to log via my own added button, and played with ideas of waiting, enabling JS, redirecting etc., but I'm new in the topic so it does not guarantee I can uncheck any ideas as already tried.
Your button is of type button (not submit). In this case the browser renders only a button but the application developer is responsible for doing something if the user clicks the button. In you case the button will trigger some javascript.
But you have disabled Javscript by
wclient.getOptions().setJavaScriptEnabled(false);
Maybe your problem goes away if you enable javascript.
In general it is a good idea to start with the default setting when facing any problems. The defaults are set in a way to be as close as possible to the real browsers.
Related
I am new at using Selenium WebDriver to automate test cases. So far (using Selenium and Java), I am able to open the testing website, enter the username and password, and log in. After logging in, however, the user is re-directed to a screen with a security warning that must be accepted before they can access the actual website. To do this, they must click on a button called "I Agree". I can't get Selenium to click the button and, without it, I can't get to the rest of the site to automate. Here is the HTML for the button:
<form name="landingHandlerSF" method="post" action="/apps/bap/secLandingHandler.do">
<input name="userAgreedTerms" value="" type="hidden">
<input name="submit" value="landing" type="hidden">
<input name="buttonAction" value="I Agree" onclick="setValue('agreetoTerms', 'Y')" type="submit">
</form>
Here is the code I have tried (which doesn't work):
WebElement button = driver.findElement(By.name("buttonAction"));
button.click();
Could someone please help me with this?
As per my understanding, page is redirecting to new html page but driver will be pointing to parent page(Login page in your case) , so you may have to switch to child window in order to click on I Agree button.
The following code will switch the driver away from the current window (ie, the login window) to the new window (ie, the security warning). After clicking I Agree that security warning will be closed and the driver will switch back automatically
String thisWindow = driver.getWindowHandle();
Set<String> windowHandles = driver.getWindowHandles();
for (String windowHandle : windowHandles) {
if (!windowHandle.contains(thisWindow)) {
driver.switchTo().window(windowHandle);
}
}
If it is not navigating to new page then it must be under some iframes , in that case you may need to switch to frame and click the button.
Hope this will work.
Try this and let me know what happened.
Did you try to inject JavaScript code into the browser?
driver.executeScript("setValue('agreetoTerms', 'Y')");
or
driver.executeScript("document.getElementsByName('buttonAction')[0].click()");
I am in bit of a delicate situation here. In my organization we design stock management systems and it is a web application based on JSP pages and servlets which handles them.
I have been asked to fix a specific problem. We have a JSP page with an HTML form table where there are stock details. When user enters the details manually and submit the form, stock details updated in the database and it works fine.
Problem is this : When the user press the browser's back button, user can come to the previous page where he submitted the details. And when the user submit this, data is saved once more to the database.I need to prevent this behaviour.(Something likeclear and reload the page.)
Things I did so far : clear the browser cache.Code works fine but not the expected result.
Unfortunately I cannot share the code due to company regulations. What I need is a help to prevent this behaviour or a workaround.
Thanks in advance..
You can use a javascript function with the help of a hidden attribute to reload the web page. When the user press the back button,based on the value of the hidden attribute, page will be reloaded without loading the cached page.
Your approach of clearing cache is correct. Coupled with that, you can use this approach.
<input type="hidden" id="refreshed" value="no">
<script type="text/javascript">
onload=function(){
var e=document.getElementById("refreshed");
if(e.value=="no")e.value="yes";
else{e.value="no";location.reload();}
}
</script>
One drawback of this approach is if your clients' browsers have disabled JS, this will not work.Otherwise it should work.
When the user press the browser's back button, user can come to the
previous page where he submitted the details. And when the user submit
this, data is saved once more to the database.
According to how you described it, that is based on a doGet request. Which means every time you visit that URL, it will send the request with whatever parameters were added.
As someone already mentioned, if you switch the form to a post method and switch the Servlet to a doPost, you won't have this issue anymore.
Alternatively you can circumvent this with a javascript solution. Here are some options:
You can check if the user clicked the back button, disable form if true.
Another way is by storing a cookie which you check on page load, if it exists you can disable the form.
You can use this code also
$(document).ready(function() {
function disableBack() { window.history.forward() }
window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});
You must use a Post-Redirect-Get pattern: https://en.m.wikipedia.org/wiki/Post/Redirect/Get.
Actually, every use of standard HTML forms with method="post" should be implemented with that pattern. It doesn't have any use for AJAX-posted forms, which actually could be another solution but will require more work and probably some architectural changes.
I had this same problem while building a django web app, and my solution was to not allow caching of the html that contains the form. In your request handler, do not allow the browser to cache the page. This will force the browser to get the page fresh from the document.
Which, in this case, you can just verify in your request handler if the requested form has already been submitted.
My code for reference:
from django.views.decorators.cache import never_cache
#never_cache
def GetForm(request, pk):
# Logic #
if (IsFormCompleted(pk)):
# Handle request #
Here is a solution.
give a random id in a hidden field on the form. Then on the server side, if the user resubmit, check if the random id already on the database. If so, redirect user.
I've got problem I can't deal with. I've tried to search answer in google, tried few things but it doesn't work any way.
Here is the problem:
I'm testing a log in page, where I type in login, password and click "login" button. Everything is OK, I'm logging to the page... But before log in (after click "login" button) there should appear a prompt with some info and "OK" button, the prompt is onclick() event of "login" button. I have no idea why the prompt doesn't show up, and I really need this prompt (I have similar problem with prompt "Do you want to save changes" which also doesn't appear - I assumed that selenium.click(..,..) and webelement.click() somehow bypass onclick() events. Do you have any idea what to do to have onclick() events working properly? I'm using IE (I have to) and selenium webdriver.
Ps.:if I do same actions "manually" the prompts do appear so I don't think that its a mistake in javascript.
Ps2.: Help me please I'm trying to fix it for a 5 h...:(
button code:
<input id="loginForm:loginCmdTest" type="submit" onclick="showAlertAndSubmitForm ();clear_loginForm();" value="Login" name="loginForm:loginCmdTest">
java code:
selenium.type("id=loginForm:login", login);
selenium.type("id=loginForm:pass", pass);
selenium.click("id=loginForm:loginCmdTest");
Ps.3: ANYONE?? Any idea?
Plain guess: You are using old, depreceated Selenium RC. What happens if you transfer it to webdriver approach?
Sample code:
WebDriver driver = new FirefoxDriver();
driver.get("http://your-test-site.com");
driver.findElement(By.id("loginForm:login")).sendKeys("login");
driver.findElement(By.id("loginForm:pass")).sendKeys("pass");
driver.findElement(By.id("loginForm:loginCmdTest").click();
Here is my situation: the user selects a section (for example from a dropdown) such as "Section1," "Section2" or "Section3." Then he clicks the OK button (or some link).
What I need to happen: after he clicks on that button/link, he will be redirected to the selected section, e.g. www.homepage.com/docs#section2.
So far, I have not been able to process the form from Link's onClick method, nor have I been able to call some clickLink on Link from the Button method onSubmit().
I would prefer not to use AJAX or JavaScript. How can I do this?
That's because a Link doesn't submit the form. It just acts as a link to somewhere. To access your formdata you'll need to submit the form first. Try using a SubmitLink instead of a Link and call
getRequestCycle().setRequestTarget
(new RedirectRequestTarget("www.homepage.com/docs#section2"));
from the onSubmit function of the SubmitLink.
Judging from the Javadoc this should work but I can't test it right now.
A RequestTarget that will send a redirect url to the browser. Use this if you
want to direct the browser to some external URL, like Google etc, immediately.
Or if you want to redirect to a Wicket page. If you want to redirect with a
delay the RedirectPage will do a meta tag redirect with a delay.
Did you try Link.setAnchor(Component)?
This is the submit button:
<h:commandButton
actionListener="#{regBean.findReg}"
action="#{regBean.navigate}" value="Search" />
This is the form:
<h:form onsubmit="this.disabled=true;busyProcess();return true;">
If the submit button is pressed, the page shows a "busy" icon until request is processed. The problem is, the form is never submitted and the request never reaches the backend. However, if I instead take out the "disabled" call like so:
<h:form onsubmit="busyProcess();return true;">
Then everything works. Any ideas?
JSF relies on the presence of the name-value pair of the submit button to invoke a specific action in the server side. If you disable the form (at least, the button) before submitting the form, then no information will be available in the request parameter map and JSF will not invoke any action.
Your best bet is to introduce a timeout wherein you disable the button about 50ms after submitting. E.g.
onsubmit="setTimeout(function(){document.getElementById('formId:buttonId').disabled=true;},50);busyProcess();"
The explicit return true; at end is by the way superflous. Also, disabling the the HTML form element directly ain't going to work since it doesn't have a disabled attribute, you want to disable the button (and if necessary the other input elements as well).
Here's some improvement:
<h:form onsubmit="busyProcess(this);">
with
function busyProcess(form) {
setTimeout(function() {
for (var i = 0; i < form.elements.length; i++) {
form.elements[i].disabled = true;
}
}, 50);
// Remnant of your code here.
}
Update: since I wondered that you told that it "works", I tested the form's disabled attribute in various browsers, it only works in MSIE (surprising..), but not in the other (real) browsers. You don't want to be browserdependent, so forget it and go ahead with disabling the form's individual elements.
Change this.disabled = true into [buttonref].disabled = true
I'm pretty sure that disabling the whole form will mean to the browser that you don't want it to be submittable. When you disable individual input elements, those aren't included in the parameters sent to the server when the form is posted.
You've disabled the entire form, you need to disable just the button instead.
Also you will want to ensure that your busyProcess() returns a truthy value or the submission could be disabled as well.
if you want the user not to submit anything, let a layer appear above the form and remove the focus from the active component.
Another way would be to make sure the AJAX requests use a common channel that has a queue in which only one request is processed at a time (easy using frameworks like prototype etc)
BalusC, your very creative timeout option works well. I actually came to the same conclusion when I tried the "disabled" attribute in other browsers. It didn't disable the form at all in Opera and Firefox. I just chalk it up to those browsers being more versatile than IE.
Sean K, disabling the button only without setting a timeout produces the same issue as the form disable, as info is passed to the server.
Thanks for all your help guys!