selenium.chooseOkOnNextConfirmation() is not working - selenium-rc

I'm stuck in one of the confirmation message. Below is the code.
boolean bFlag = selenium.isConfirmationPresent(); // which gives bFlag= false
selenium.chooseOkOnNextConfirmation();
selenium.click("//input[#value='Approve']");
// On clicking Approve button confirmation messasge is displayed.
boolean Flag = selenium.isConfirmationPresent(); // Which gives flag= true
Selenium is unable to click OK in the confirmation. I'm unable to continue with further coding. Nun of the selenium commands is working after clicking on approve button.
Can any one help me on this
Regards

I only have a reference to a rather old piece of documentation, but I guess what is said there is still true
http://release.seleniumhq.org/selenium-core/1.0.1/reference.html#chooseOkOnNextConfirmation
every time a confirmation comes up, you must consume it with a corresponding getConfirmation, or else the next selenium operation will fail.
So your code should look like this:
selenium.chooseOkOnNextConfirmation();
selenium.click("//input[#value='Approve']");
String confirmation = selenium.getConfirmation();
assertEquals("expected text of confirmation", confirmation); // check content of confirmation, optional
// now continue with more selenium commands

Related

Blocking code in custom message box in VB6

In my vb6 project, I created my own msgbox using a form, due to difficulties in changing
the font/language of in-built msgbox. A search with google gave the idea of own msg-
box rather than trying msgbox of VB. Now the problem is : - when the user exits the
program, 3 options are given: to close, to restart and to cancel exit. The user need not
again go through the process of giving password etc in restart option. If I give cancel =
true in the QueryUnload event, then 2nd option does not work, 3rd option works. If
cancel = true is not given, 2nd option works, but 3rd option does not. It appears that
the main form does not get unloaded if cancel = true. Unless & until the main form
unloads, the program will not work with the fresh data to be given by the user in the
initial Form. Since the code after "msgbox.show" depends on options, it is not possible
to write that code in the same sub, not even in the same form code. Is there any way
to stop the subsequent code after "msgbox.show" and continue the same after getting
option? (like in the in-built msgbox of VB.) I am not an expert in VB, so please correct
if I made some mistake; also help with advice/suggestions.
EDIT:- [Extended explanation]
The 3 forms in my project:
Initial form for password, data etc. This is input Form for user.
Main Form. This Form shows the results after process of input.
frmMsgBox. This is a custom msgbox created using a form.
Main Form code portion. Code for closing the program:
Private Sub Form_QueryUnload(Cancel as.......)
cancel = True
frmMsgBox.Label1.caption = Do you wish to 1.Exit 2.Restart
3.Cancel the exit?
frmMsgBox.Show
End sub
(The above msgbox is almost like an in-built msgbox in VB with
vbYesNoCancel buttons) The message is in regional language,
which was the main reason forced me to use my own msgbox.
After MsgBox appears, the user selects one of the above options
using 3 commandButtons placed in that Form. The code after
clicking these buttons is written in the code portion of frmMsgBox:
Command1_Click 'This is for Exit from the Program.
All Forms.unload, All forms set to nothing, end.
Command2_click 'This is for restarting the Program.
Unload Main Form, set to nothing
Load Initial Form
Initial Fom.show
frmMsgBox.Hide
Command3_Click 'This is for cancelling the exit request.
Main Form.Show
frmMsgBox.Hide
With the above code, I have no problem with options 1 & 3,
i.e; to exit from the program or to start. The frmMsgBox hides,
the initial form shows - these are OK, but the main form does
not unload nor it is removed from memory. Because of this,
whatever new data is given by the user in the initial form now
is not being processed, the main form is struck with the old results.
If cancel = true is removed from the code above, Options 1 & 2
are OK, but option 3 does not work. Then the Main Form loses
all its results (all labels, texts etc in that Form turn blank.)

Coded UI Test - Set UI property not returning (hanging indefinitely)

I am getting crazy with a problem that I found when executing a Visual Studio Coded UI Test.
The scenario is as follows.
I recorded a Coded UI Tests that do the following steps in a Web Application (Microsoft Dynamics CRM 2011):
Login into an application
Navigate into a page
On the page set the selected value of a html combobox
The test is able to do all those steps without a problem, even selecting the value in the combobox.
The web application have a piece of Javascript that is executed when the selected item changes.
if one of the values is selected then an alert message is presented to the user and the application will set the selected item to a default one!
The javascript code look like this:
switch (Xrm.Page.getAttribute("status").getValue()) {
case 3: //Authorised
alert("Please use the method XPTO to update the record status to Authorised!");
Xrm.Page.getAttribute("status").setValue(1);
Xrm.Page.getControl("tatus").setFocus(true);
return false;
The UI Test method that is performing the change in the combobox is as follows:
/// <summary>
/// Select a value in the Status dropdown box
/// </summary>
public void SelectStatus()
{
#region Variable Declarations
HtmlComboBox uIStatusComboBox = this.UIHttpsappWindow200.UIModuleUITEDocument5.UIWorkplaceDashboardsFrame.UIModuleUITEDocument.UIStatusComboBox;
// Select 'Authorised' in 'Status' combo box
uIStatusComboBox.SelectedItem = this.SelectStatus_SPParams.UIStatusComboBoxSelectedItem;
}
The test method is able to change the value in the combobox, and an alert message is displayed to the user. However this part of the code uIStatusComboBox.SelectedItem = this.SelectStatus_SPParams.UIStatusComboBoxSelectedItem;
never returns and the test just hangs there until it timeouts!
I have no ideia how to work arround this issue! I was thinking that maybe the problem could be in the fact that we have javascript code that is executed after the alert is displayed to the user. I changed the JS so that the alert message is the last thing to be displayed but it also didn´t help!
I also noticed that if I click Ok on the alert message the test Pass!
If I select other value that dont trigger any JS the test also Pass!
Does anyone have any idea about this issue?
Edit 1:
I noticed another thing, I can use the BrowserWindow object to send a JS script to the browser. If I try to create an alert message the call also gets blocked until I click on the Ok button, on the alert!
BrowserWindow bw = BrowserWindow.Locate("My window");
bw.ExecuteScript("alert('This is just a simple alert.');");
The ExecuteScript statement also gets blocked until I click on the OK button!
This seems very very strange!
I believe I have found a workaround.
I was googling to find anything that could help me with this issue and I end up finding this question on StackOverflow Coded ui test - Select an item from a combobox without using mouse coordinates
I try to select the same item in the Dropdown without using the "SelectedItem" property of the HtmlComboBox element.
I try to use the keyboard to select the element in the dropdown box, first I click on the Dropdown and then I use the Down key and Enter key to select the element that I want:
public void SelectStatus_SP(string SelectedItem)
{
#region Variable Declarations
HtmlComboBox uIStatusComboBox = this.UIHttpsappWindow200.UIpopupUITEDocument5.UIWorkplaceDashboardsFrame.UIpopupUITEDocument.UIStatusComboBox;
#endregion
Mouse.Click(uIStatusComboBox);
Keyboard.SendKeys(uIStatusComboBox, "{Down}{Down}{Enter}", ModifierKeys.None);
}
In my case the element that I want to select is the 3rd one, so I go down 2 times and then hit enter on the element I want to select.
This workaround is not so good because if the element changes position I will select the worng one! But this was the only way I got for the test not to be stock in that part!
I wonder if anyone had a similar issue. Because to me this seems like a bug in the Coded UI Test engine, It doesn´t make any scence for the test to be stocked while the alert is not closed!

How to handle Modals in cucumber + Capybara + Selenium

So I am trying to click a forgot password link (which causes a modal pop up) and confirm the pop up link so I can perform a test on the sent out email.
My code looks like this:
page.find(:css, '#launch-modal-link').click # code fails on this line, after clicking the link
page.driver.browser.switch_to.alert.accept # does not get to this line of code.
What am I doing wrong exactly when trying to click the "Ok" button in the modal pop up?
Do I need to add a try catch block (or whatever it is called in Ruby) around the link
Solved it - Found the answer somewhere else. Its a hack though, and not something done via cucumber directly.
page.evaluate_script('window.confirm = function() { return true; }')
This works because it over writes the confirm() to always return true and the confirm function seems to be a common javascript function to return the button clicked in a dialog box. Could be wrong about that. (read the javascript function being performed onclick. Might not always work)

Selenium Webdriver - How do I skip the wait for page load after a click and continue

I have an rspec test using webdriver that clicks on a button... after clicking the button, the page never fully loads (which is expected and correct behaviour). After clicking the button, I want to wait 2 seconds and then navigate to a different URL... despite the fact that the page has not loaded. I don't want to throw an error because the page hasn't loaded, I want to just ignore that, and continue on as if everything is good. The page should never load, that is the expected and correct behaviour.
How can I avoid waiting until the timeout, and secondly, how can I have that not throw an error which casuses the test to fail.
Thank you!
WebDriver has a blocking API and it will always wait for page to be loaded. What you can do instead, is to press the button via JavaScript, i.e. trigger its onclick event. I am not familiar with Ruby, but in Java it would be:
WebDriver driver = ....; // Init WebDriver
WebElement button = ....; // Find your element for clicking
String script = "if (document.createEventObject){"+
"return arguments[0].fireEvent('onclick');"+
"}else{"+
"var evt = arguments[0].ownerDocument.createEvent('MouseEvents');"+
"evt.initMouseEvent('click',true,true,"+
"element.ownerDocument.defaultView,1,0,0,0,0,false,"+
"false,false,false,1,null);"+
"return !element.dispatchEvent(evt);}" ;
((JavascriptExecutor)driver).executeScript(script, button);
After this you can wait for 2 seconds and continue
I had this similar problem, I tried this solution which #Sergii mentioned but was getting below error:
org.openqa.selenium.JavascriptException: javascript error: element is not defined (Session info: chrome=84.0.4147.89)
As a workaround had to initialize element within java script code.
String script = "var element = document.querySelector('button[type=submit]');" +
"if (document.createEventObject){"+
"return element.fireEvent('onclick');"+
"}else{"+
"var evt = element.ownerDocument.createEvent('MouseEvents');"+
"evt.initMouseEvent('click',true,true,"+
"element.ownerDocument.defaultView,1,0,0,0,0,false,"+
"false,false,false,1,null);"+
"return !element.dispatchEvent(evt);}" ;
Thank you #Sergii for your answer, I was not able to add this as a comment due to limited characters, so added it as a separate answer.
why don't you try a simple trick of using "waiting()" after waitForPageToLoad() which makes it to neglect the previous command in Selenium and never fails that step
What if you used RSpec's expect method:
expect {
Thread.new() {
sleep(2)
raise RuntimeError
}
theButton.click()
}.to raise_error
Send the 'Enter' key to the link or button in question instead of a click, webdriver won't wait for the page to load and will return instantly (this is in C#, just translate to Ruby):
element.SendKeys(Key,Return);

Google Chrome Extension - How can I include a content script more than once?

I've been working on Chrome Extension for a website for the past couple of days. It's coming along really nicely but I've encountered a problem that you might be able to help with.
Here's an outline of what the extension does (this functionality is complete):
A user can enter their username and password into the extensions popup - and verify their user account for the particular website
When a user browses http://twitter.com a content script is dynamically included that manipulates the DOM to include an extra button next to each tweet displayed.
When a user clicks this button they are presented with a dialog box
I've made a lot of progress but here is my problem:
When a user visits Twitter the content script is activated and all tweets on the page get my new button - but if the user then clicks 'More...' and dynamically loads the next 20 tweets... these new additions to the page DOM do not get affected by the content script (because it is already loaded).
I could add an event listener to the 'More...' button so it then triggers the original content script again (and adds the new button) but i would have to predict the length of twitter's ajax request response.
I can't tap into their Ajax request that pulls in more tweets and call my addCurateButton() function once the request is complete.
What do you think is the best solution? (if there is one)
What you want to do is to re-execute your content-script every time the DOM is changed. Luckily there is an event for that. Have a look at the mutation event called DOMNodeInserted.
Rewrite your content script so that it attaches an event listener to the body of the DOM for the DOMNodeInserted event. See the example below:
var isActive = false;
/* Your function that injects your buttons */
var inject = function() {
if (isActive) {
console.log('INFO: Injection already active');
return;
}
try {
isActive = true;
//inject your buttons here
//for the sake of the example I just put an alert here.
alert("Hello. The DOM just changed.");
} catch(e) {
console.error("ERROR: " + e.toString());
} finally {
isActive = false;
}
};
document.body.addEventListener("DOMNodeInserted", inject, false);
The last line will add the event listener. When a page loads the event is triggered quite often so you should define a boolean (e.g. var isActive), that you initialize to false. Whenever the inject function is run check whether isActive == true and then abort the injection to not execute it too often at the same time.
Interacting with Ajax is probably the hardest thing to coax a content script to do, but I think you’re on the right track. There are a couple different approaches I’ve taken to solving this problem. In your case, though, I think a combination of the two approaches (which I’ll explain last) would be best.
Attach event listeners to the DOM to detect relevant changes. This solution is what you’ve suggested and introduces the race condition.
Continuously inspect the DOM for changes from inside a loop (preferably one executed with setInterval). This solution would be effective, but relatively inefficient.
The best-of-both-worlds approach would be to initiate the inspection loop only after the more button is pressed. This solution would both avoid the timing issue and be efficient.
You can attach an event-handler on the button, or link that is used for fetching more results. Then attach a function to it such that whenever the button is clicked, your extension removes all the buttons from DOM and starts over inserting them, or check weather your button exists in that particular class of DOM element or not and attach a button if it doesn't.

Resources