In some scenarios we faced problem to automate windows dialog boxes Selenium with C# or Selenium with Java. There are different ways to automate windows popup. One thing is clear that we can not automate using selenium because selenium is only use for browser based applications.
So we have only option is to use our scripting languages only.
Using C#:
To identify control id use UISpy. To automate we need to use "System.Windows.Automation" namespace in our project.
using System;
using System.Threading;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class WindowPopUpHandler
{
private bool _isCancelClicked = false;
public bool Authenticate(string windowTitle, string userName, string password)
{
string checkProperty = "AutomationId";
string userNameId = "1003";
string passwordId = "1005";
string okButoonId = "1";
try
{
AutomationElement desktopRoot = AutomationElement.RootElement;
AutomationElement app = desktopRoot.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, windowTitle));
if (app == null)
throw new AutomationException("Window '" + windowTitle + "' is not found.");
this.WaitForCondition(app, checkProperty, userNameId, 5000);
AutomationElement userNameTextBoxElement = GetRequiredElement(app, checkProperty, userNameId); var userNameTextBox = (ValuePattern)userNameTextBoxElement.GetCurrentPattern (ValuePattern.Pattern);
userNameTextBox.SetValue(userName);
AutomationElement passwordTextBoxElement = GetRequiredElement(app, checkProperty, passwordId); var passwordTextBox = (ValuePattern)passwordTextBoxElement.GetCurrentPattern (ValuePattern.Pattern);
AutomationElement okButton = GetRequiredElement(app, checkProperty, okButoonId);
var okButtonelement = (InvokePattern)okButton.GetCurrentPattern(InvokePattern.Pattern);
okButtonelement.Invoke(); return true;
}catch (Exception e)
{
throw new AutomationException("Authenticate Method Failed due to exception '" + e.Message);
}
}
}
Using Java: Need to import Robot jar into our application.
So we have only option is to use our scripting languages only.
Using C#:
To identify control id use UISpy. To automate we need to use "System.Windows.Automation" namespace in our project.
using System;
using System.Threading;
using System.Windows.Automation;
using Microsoft.VisualStudio.TestTools.UnitTesting;
public class WindowPopUpHandler
{
private bool _isCancelClicked = false;
public bool Authenticate(string windowTitle, string userName, string password)
{
string checkProperty = "AutomationId";
string userNameId = "1003";
string passwordId = "1005";
string okButoonId = "1";
try
{
AutomationElement desktopRoot = AutomationElement.RootElement;
AutomationElement app = desktopRoot.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, windowTitle));
if (app == null)
throw new AutomationException("Window '" + windowTitle + "' is not found.");
this.WaitForCondition(app, checkProperty, userNameId, 5000);
AutomationElement userNameTextBoxElement = GetRequiredElement(app, checkProperty, userNameId); var userNameTextBox = (ValuePattern)userNameTextBoxElement.GetCurrentPattern (ValuePattern.Pattern);
userNameTextBox.SetValue(userName);
AutomationElement passwordTextBoxElement = GetRequiredElement(app, checkProperty, passwordId); var passwordTextBox = (ValuePattern)passwordTextBoxElement.GetCurrentPattern (ValuePattern.Pattern);
AutomationElement okButton = GetRequiredElement(app, checkProperty, okButoonId);
var okButtonelement = (InvokePattern)okButton.GetCurrentPattern(InvokePattern.Pattern);
okButtonelement.Invoke(); return true;
}catch (Exception e)
{
throw new AutomationException("Authenticate Method Failed due to exception '" + e.Message);
}
}
}
Using Java: Need to import Robot jar into our application.
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;
import org.apache.log4j.xml.DOMConfigurator;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
public class BaseTest extends CommonFunction {
@Test
public void AdminPageLogin() throws InterruptedException, AWTException, java.io.IOException
{
try{
LaunchBrowser(readConfig("urlAdmin"));
}
catch(Exception e){ //Setting UserName and Password by Sending Keys using Robot class
Robot robot = new Robot();
Utilities.CopyPaste(readConfig("adminUserId"), robot);
Utilities.SendKeys(KeyEvent.VK_TAB, robot);
Utilities.CopyPaste(readConfig("adminPwd"), robot);
Utilities.SendKeys(KeyEvent.VK_TAB, robot);
Utilities.SendKeys(KeyEvent.VK_ENTER, robot);
}
}