Java: focus is not on pop-window during window handling - selenium-webdriver

I have opened the website and applied the Login then popup window opens, i want to click from window popup but i am not able to switch on popup.
driver.get("https://hdfcbank.com/");
driver.findElement(By.id("loginsubmit")).click();
String loginWindow = driver.getWindowHandle();
driver.switchTo().window(loginWindow);
driver.findElement(By.xpath("//*[#id='wrapper']/div[6]/a/img")).click();
I am not able click on popup element at line 5. can you check the code.

Check the accepted answer for a similar question
How to handle Pop-up in Selenium WebDriver using Java
You need to - getWindowHandles - & then iterate over them.
Here is the working solution in case you still haven't figured it out (this is for the HDFC example)...
String test_URL = "http://www.hdfcbank.com/";
String css_login = "img#loginsubmit";
String css_popup_continue = "img[alt='Continue']";
browser = new FirefoxDriver();
browser.navigate().to(test_URL);
List<WebElement> objLogin = browser.findElements(By.cssSelector(css_login));
if (objLogin.size() > 0) {
objLogin.get(0).click();
String parentWindowHandle = browser.getWindowHandle(); // save the current window handle.
WebDriver popup = null;
Iterator<String> windowIterator = browser.getWindowHandles().iterator();
while(windowIterator.hasNext()) {
String windowHandle = windowIterator.next();
popup = browser.switchTo().window(windowHandle);
if (popup.getTitle().contains("NetBanking")) {
List<WebElement> objPopupElement = popup.findElements(By.cssSelector(css_popup_continue));
if(objPopupElement.size() > 0){
System.out.println("Switched to Popup and found element...");
objPopupElement.get(0).click();
//Do any other operations...
break;
}
}
}
//always safe to switch back to parent window to avoid any null pointers, unless parent process got closed...
browser.switchTo().window(parentWindowHandle);
}
else {
System.out.println("Logon button not found...");
}

Related

How to code an iPhone style popup menu in CN1?

It has probably been covered before, but I couldn’t google anything. What is the best approach for making an iPhone-style pop-up selection menu like attached picture? I've tried with a Dialog, but I haven't found an elegant way to add the Commands so they appear nicely and both trigger the action and close the dialog at the same time. And showing a Cancel entry separately is not supported by a ComponentGroup.
See this sample:
Form hi = new Form("Pop");
Button pop = new Button("Pop");
pop.addActionListener(e -> {
Dialog dlg = new Dialog();
// makes the dialog transparent
dlg.setDialogUIID("Container");
dlg.setLayout(BoxLayout.y());
Command optionACmd = new Command("Option A");
Command optionBCmd = new Command("Option B");
Command optionCCmd = new Command("Option C");
Command cancelCmd = new Command("Cancel");
dlg.add(
ComponentGroup.enclose(
new Button(optionACmd),
new Button(optionBCmd),
new Button(optionCCmd)
)).
add(ComponentGroup.enclose(new Button(cancelCmd)));
Command result = dlg.showStretched(BorderLayout.SOUTH, true);
ToastBar.showMessage("Command " + result.getCommandName(), FontImage.MATERIAL_INFO);
});
hi.add(pop);
hi.show();
Which results in this:
Thanks Shai!
I made it into a component in case anybody has a similar need:
class MyPopupMenu extends Dialog {
private Command cancelCmd = null;
MyPopupMenu(boolean includeCancel, Command... commands) {
this(includeCancel?new Command("Cancel"):null, commands);
}
MyPopupMenu(Command cancelOptional, Command... commands) {
super();
setDialogUIID("Container");
setLayout(BoxLayout.y());
setDisposeWhenPointerOutOfBounds(true); //close if clicking outside menu
ComponentGroup group = new ComponentGroup();
for (Command cmd : commands) {
group.add(new Button(cmd));
}
add(group);
this.cancelCmd = cancelOptional;
if (cancelCmd != null) {
add(ComponentGroup.enclose(new Button(cancelCmd)));
}
/**
* show the menu and execute the selected Command,
* or do nothing if Cancel is selected
*/
public void popup() {
Command choice = showStretched(BorderLayout.SOUTH, true);
if (choice != null && choice != cancelCmd) {
choice.actionPerformed(null);
}
}
}
This is awesome, thanks guys. Any reason my buttons seem to be so small? Trying to figure out which style needs to change to increase the height. Changing the Button padding did not seem to change anything. I used the Business theme as a starting point.

WinForms ShowDialog() - No modal dialog box opens

I want to open a dialog as a Login-Box in my Explorer-NamespaceExtension. When I call ShowDialog() the first time the box opens but not as a modal dialog. I can click elements in the Explorer. If I close these dialog and open it again, it is a modal dialog and interaction with the Explorer isn`t possible. Thats what I want to achieve with the first open.
My idea is that I call the form first from the wrong thread. Thats the reason why I used the following code, but it doesn`t solve the problem :/
public delegate void myDelegate();
public void ShowDialogThreadSave()
{
if (this.InvokeRequired)
{
myDelegate d = new myDelegate(ShowDialogThreadSave);
this.Invoke(d);
}
else
{
this.ShowDialog();
}
}
I hope you have an idea :-)
Thanks!
Edit:
The call is fired from a background class. I have 3 possibilities to login into the extension, so i encapsulated the call:
public bool LogIn()
{
bool connected = BackEnd.isConnected();
if(loginDialog == null)
{
LogIn logIn = new LogIn();
}
else
{
if (!connected && !Utils.AlreadyLoggedIn() && !loginDialog.IsAccessible && !loginDialog.Visible)
loginDialog.ShowDialogThreadSave();
else if (!connected && !Utils.AlreadyLoggedIn() && !loginDialog.Visible)
loginDialog.ShowDialogThreadSave();
else if (!connected && !Utils.AlreadyLoggedIn() && loginDialog.Visible)
LOG.DebugFormat("error");
else
Utils.ConnectWithoutLoginWindow();
}
connected = BackEnd.isConnected();
return connected;
}
Edit2: With debugging I found out that the ShowDialogThreadSave() is always called by the UI Thread and I will never use the if... What is the problem?

how to hold dialogbox when check textbox is not empty in dialog box

I am opening a dialog box in a winform the problem is that at dialog box i have a textbox where i am checking if the user has filled information or not. Below is the code that opens the dialog box
frmRescheduleNotes _scAttempt = new frmRescheduleNotes(_rdate, _rtime, BttnText, ModelType, _UserName, eventId, EventTextid, _OrderId, delDate);
DialogResult dr = _scAttempt.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
}
else
{
DataSet _dt = _commonDAC.GetOrderDetailByOrderIdEventTypeId(_OrderId, 3);
dataGridReschedule.DataSource = _dt.Tables[0];
dataGridReschedule.AutoGenerateColumns = false;
naviBar1.ActiveBand = this.naviBand1;
}
The problem is that I give a message to user if he did not fill any information in the textbox " Fill information in Textbox" but after this it return on main function .
if (txtNotes.Text == string.Empty)
{
MessageBox.Show("Please Fill Notes");
return;
}
result = _commonDAC.SaveBuilderChanges(_date, _time, _eventId, _eventTextId, txtNotes.Text, User, _date, _OrderId, EventDetail.ToString());
if (result > 0)
{
int UpdateResule = _commonDAC.UpdateOrderDateTimeWithDeliveryBy(Convert.ToDateTime(_readyDate), Convert.ToDateTime(_readyTime), _OrderId, Convert.ToDateTime(_DeliverBy));
this.Close();
}
How do I check do that.
I can't tell where the code that you posted resides (in the main form or in the dialog box), but typically, you would run your validation code when the user goes to submit the dialog box, and if the validation code fails (and you've informed the user as such), then you would simply not set a dialog result (OK or otherwise) and you would not close the dialog. Thus, you're effectively cancelling the submission of the dialog box.

Prevent multiple instances of windows

How could I prevent opening multiple windows from a wpf application?
I need to open a window from the menu and if I click to open it again, I want the already opened window to become active.
Any suggestions?
You could use the Application.Current.Windows collection for that. Just check whether this collection contains the window you are about to open and if it does, activate it, otherwise create new window and show it:
var existingWindow = Application.Current.Windows.Cast<Window>().SingleOrDefault(w => /* return "true" if 'w' is the window your are about to open */);
if (existingWindow != null) {
existingWindow.Activate();
}
else {
// Create and show new window
}
Here is one way to do it:
private Window _otherWindow;
private void OpenWindow()
{
if (_otherWindow == null)
{
//Pass in a reference to this window so OtherWindow can call WindowClosed when it is closed..
_otherWindow = new OtherWindow(this);
_otherWindow.Show();
}
else
_otherWindow.Activate(); //Or whatever the method is to bring a window to the front
}
public void WindowClosed()
{
_otherWindow = null;
}

Is that possible to have open dialog window inside child window in Silverlight?

I need to have OpenDialog window being inside browser window of Silverlight application. I am wondering if it possible. Any help is highly appreciated!
Below is the code I have to open child window and OpenDialog:
private void openChildWindow_Click(object sender, System.Windows.RoutedEventArgs e)
{
Add_ChildWindow ap = new Add_ChildWindow();
ap.Show();
OpenFileDialog openFileDialog1 = new OpenFileDialog();
// Set filter options and filter index.
openFileDialog1.Filter = "Packages (*.sprj)|*.sprj|Packages (*.sprj)|*.sprj";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = true;
// Call the ShowDialog method to show the dialog box.
bool? userClickedOK = openFileDialog1.ShowDialog();
// Process input if the user clicked OK.
if (userClickedOK == true)
{
// Open the selected file to read.
//textBox1.Text = openFileDialog1.File.Name;
System.IO.Stream fileStream = openFileDialog1.File.OpenRead();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream))
{
// Read the first line from the file and write it the textbox.
// tbResults.Text = reader.ReadLine();
}
fileStream.Close();
}
}
An Open Dialog window is a system window. Silverlight being sandboxed has to use the system open dialog I believe.
In other words, I don't think this is possible.

Resources