SeleniumException: ERROR: There was an unexpected Alert! - java

I get this error while running selenium RC tests against IE7.
com.thoughtworks.selenium.SeleniumException: ERROR: There was an unexpected Alert! [error:[object Error]]
It happens whenever I attempt to click a link or a tab or anything clickable. Any Ideas guys. This is really frustrating.

Checking the Google search results for your error message, and the selenium development mailing list, it sounds as if this is not a bug in Selenium but rather it is Selenium telling you that an unexpected window.alert() function was called in your web application, and that Selenium does not know how to handle it because you did not tell it how to.

I'd be surprised if the same issue isn't occurring in other browsers. Perhaps your application behaves differently depending on the browser? Basically Selenium is reporting that it can't move onto further commands after you are clicking on these links or tabs because it has detected an unexpected JavaScript alert. You wont see these alerts because Selenium consumes them. There are a few things you can do to work out what's going on.
The first thing I'd suggest is to just check your application in IE7. Manually complete the steps of your tests - do you see the JavaScript alerts? If so, you will need to add the appropriate commands to your Selenium test.
If for some reason you can't replicate the alerts manually you can either dismiss the alert by using the the getAlert command, or use the response from getAlert to find out the text of the unexpected alert.
Java/TestNG example for finding out the message of the alert:
assertEquals(selenium.getAlert(), "Hello World");
The above will still cause your test to fail (unless the alert really does say 'Hello World'), but will fail with a message similar to "expected 'Hello World' but was 'Your unexpected alert message'".

I'm getting a similar thing.
No error in IE, JS error popup in Chrome, nothing in FF...until I run my Selenium script and it reports an unexpected error.
Amusingly when I do selenium.IsAlertPresent() it returns false, even though it says there was one.

I'm seeing the same error. Normally we run our test using C#, but in this case we are setting up some simple tests using the Selenium IDE.
Since Selenium IDE records your events and then allows you to play them back you would expect it to handle the alert. When looking at the events it records it appears it's capturing the alert. However, when you play it back the events are out of order.
First it captures the click event. Then is uses assertAlert to make sure the alert happened.
The problem occurs when you play it back. It does the click event and then errors out. If you move the assertAlert statement before the click event it doesn't work. If you change the assertAlert to waitForAlert that doesn't work either. I can't seem to figure out anyway to get the playback working in the Selenium IDE. If I didn't need this runnable for a non-techy I'd just put it into my regular tests and be done with it.
Edit: After further review it appears the alert message isn't matching. Even though it grabbed the alert itself when doing the record. Visual inspection doesn't do anything to help me determine what it considers different.

I've seen this error also. In my case the issue was the latency in an Ajax response object was causing Selenium IDE error. The solution that worked for me was to add a pause command right after any Ajax calls in the test script.
The call below was taking few seconds to return with valid Ajax response
click
css=#form3 > div.addButton > button.btn.btn-primary
By adding a pause clause, the "Unexpected alert" issue was resolved
pause
5000

I Suggest you to test manually those steps which are there in your selenium test.If it is not reproducible then it might not be the problem in the page.
please debug your selenium script with assert function for identifying where is it finding the unexpected alert.
if (selenium.isAlertPresent()) {
assertEquals(selenium.getAlert(), "Hello World");
}
which will help you identify the problem. I suggest you to repeat the test because you may find that assertion occurring in different part of your code.

Related

how do i work around 429 errors in selenium?

I run into 429 errors a lot in selenium, because the driver is too fast for the server. this causes the website to get stuck (with no error message presented to the user). I use waits and I've tried using the sleep method but it doesn't always work. Is there anything I can do or is this a design flaw on the website - shouldn't the error be presented to the user so I can try to reload the page?
I am using incognito mode so cookies aren't an issue.
try adding the bellow line of code after declaring the driver
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
also using sleep having assigned it a fair amount of time? make sure that it's using millescondes
thread.sleep(3000);
It might be an issue that has to be handled in the source code of the application. I would do the following:
Run a performance test and see if I can replicate it.
Check the logs of the application to see if this happening frequently on production.
Please see following article for dev to handle 429 errors --> link
Otherwise the solution provided by #hamza belatra should be sufficient.
Unless it is a problem in your Test Architecture code, how to the driver is handled and so on.

Two versions of the same locator in automation testing: one works, the other causes numerous errors

There is a list box that contains three options: "In Progress", "Complete", and "Clear Status".
When I call:
page_object.zs_out_btn(driver).click();
new Select(page_object.zs_out_btn(driver)).selectByVisibleText("In Progress");
...this works perfectly. This clicks the dropdown button, and selects 'In Progress'.
After this is where I start having issues. I have two versions of the same thing I am trying select in the sense that one works and the other doesn't; I am not running both of these at the same time.
page_object.zs_out_btn(driver).click();
//new Select(page_object.zs_out_btn(driver)).selectByVisibleText("Complete");
//new Select(driver.findElement(By.id("zs-status-select"))).selectByVisibleText("Complete");
The second line doesn't work. The third line does. I used Katalon and two other tools to verify that this path is indeed valid. They even work inside of those tools and despite hundreds of attempts at sampling this, it always gives this id. byClass couldn't be found even through manual definition. The HTML for this looks like this:
<select id="zs-status-select" class="form-control" style=""><option>In Progress</option><option>Complete</option><option value="">Clear Status (Blank)</option></select>
The way that I defined the second line in POM is such:
public WebElement zs_out_btn(WebDriver driver) {
element = driver.findElement(By.id("zs-outpost-select"));
return element;
}
What I am seeing is that TestNG doesn't report this as a failure and goes directly to my #AfterTest. So I disabled this to see if it would just hang on that element and it did. I tried implicit, explicit, and even a thread.sleep(I know I'm not supposed to use this, it is only for testing) waits to make sure everything on page was loaded and to give the driver time to find it.
Depending on how many times I run this, every now and then it will work, sometimes the browser will just time out and quit on its own even with the #AfterTest disabled, sometimes eclipse oxygen will freeze and give some errors that I've never seen before then quit. After restarting eclipse numerous times, it will start to work and then come back to the same issue I am having.
So, I reinstalled eclipse, rebuilt the project from scratch, three times, included the correct libraries for selenium and TestNG. Same behavior. I am able to run all my other tests perfectly fine.
Why is this particular element, so troublesome and why would it force the browser or eclipse to time out and close? Any help with this would be very much appreciated.

Selenium Web Driver click action

I have a code in selenium which dynamically clicks on the tabs in menu page. And sometimes happens that tab is not clickable(it is just a plain td with span inside) and when Web Driver tries to click on this tab, my program gets frozen (no exception thrown or anything).
I can't avoid clicking on tabs like that but I would like to somehow prevent that freeze after click. So can I set some timeout or tell to selenium what to do if element is not clickable?
driver.findElement(By.xpath(
format("//span[#class='rf-tab-lbl'][text()='{0}']",
navigation.getGroup()))).click();
//if the tab is currently selected (hence is not clickable) selenium won't click
// on it and program freezes
Please try another page from a different website. It might be a JS looping issue.
I encountered such an issue in HtmlUnit for some URLs.
I raised issue in HtmlUnit user group.
They told me that JS infinite loop was causing freez.
Time out did not work for me as well.
I tried to apply my own time out. That did not work too.
Refer following question for applying own timeout
HtmlUnit WebClient Timeout
If it works for another website, problem might be site specific.
You can attach source of Selenium in eclipse and check / debug where it is getting stuck.
I did same for HtmlUnit. I reached to parse method, which did not come out.
If still does not work, contact Selenium support.

Selenium RC and Internet Explorer

I am trying to create a fully-automated test suite for a web application, using Selenium RC and test cases written in Java. However, I have encountered a few problems that I have not been able to solve. Please let me know if you have a suggestion about any of these issues.
Single window mode. I would like to run single window mode because I think it will provide a significant performance improvement, and probably solve problem 2. I am able to run my test cases in single window mode with Firefox and everything works as expected, with much faster execution. However, I have not been able to get my test cases to execute when using single window mode with Internet Explorer; button clicks do not work at all, so the tests fail due to the browser never advancing to the next screen. The exact same test case executes fine in multi-window Internet Explorer. Is there some kind of trick I can use to get this working?
When running in multi-window mode, if there is an error in the test, or if the user exits the Selenium GUI window, the browser never closes. I know that it is possible to get the process id and kill it, but this seems rather dangerous, especially if the user is running multiple instances of the browser. This is part of the reason that I would like to use single window mode, if possible. Is there some other way, possibly by handling the window close event for the Selenium GUI, to solve this?
I am unable to use the waitForPageToLoad command in any of my test scripts because it will never recognize that the page has loaded and resume execution. I think this is because the pages use Javascript, but I am not sure; I don't have too much experience in webpage development. Right now, I am using the wait command and specifying the amount of time to wait. However, this is very unreliable, sometimes if the Internet connection is slow, my tests fail because the wait times are not long enough. Other times the tests are excessively slow, due to long wait times. Any ideas for how to handle this problem?
Thank you! I appreciate any answers or suggestions you can give. Please let me know if you would like some more information.
For multi widow close issue, you need to handle the exceptions. Call the close function in the finally block will resolve the window close issue.
For Page refresh, you need to check that manually and call in correct places. Usually clicking on links will cause page refresh before showing next screen.
Have you tried IE HTA mode? When I used to mess with this stuff it was the only reliable way to run tests on IE.
Not sure
You need to use the wait class. In essence you make an action then poll on an element which isn't present yet but you know will be when its safe to continue with the test. So you only ever wait the minimum time needed.

Testing onbeforeunload events from Selenium

I'm trying to write a Selenium test for a web page that uses an onbeforeunload event to prompt the user before leaving. Selenium doesn't seem to recognize the confirmation dialog that comes up, or to provide a way to hit OK or Cancel. Is there any way to do this? I'm using the Java Selenium driver, if that's relevant.
You could write a user extension (or just some JavaScript in a storeEval etc) that tests that window.onbeforeunload is set, and then replaces it with null before continuing on from the page. Ugly, but ought to get you off the page.
I've just had to do this for an application of mine where the onbeforeunload handler brings up a prompt if a user leaves a page while a document is in an unsaved state. Python code:
driver.switch_to.alert.accept()
The Java equivalent would be:
driver.switchTo().alert().accept();
If the alert does not exist, the code above will fail with a NoAlertPresentException so there is no need for a separate test to check the existence before accepting the prompt.
I'm running Selenium 2.43.0 but I think this has been doable for a while now.
In cases where I don't want the prompt to come up at all because that's not what I'm testing, I run custom JavaScript in the browser to set window.onbeforeunload to null before leaving the page. I put this in the test teardown code.
faced same problem with "beforeunlaod" event listner, LUMINUS! a chrome addon that helps me just block the event listener in the plugin thats all..
When I was confronted with limited control which I had over browser using Selenium, I turned to MozLab plugin which solved my problem if only for one browser platform.

Categories

Resources