MessageDialog not closing - mono

I'm using the following code to show a message dialog in my application :
MessageDialog dialog = new MessageDialog(null,
DialogFlags.Modal,
MessageType.Error,
ButtonsType.Ok,
"An error occured: " );
dialog.Run();
Problem is the Ok button on the window doesn't do anything.... The window only disappears when i hit the X button on top right corner.
Any ideas?

You need to call dialog.Destroy(); after your call to dialog.Run();

You can also hook the Response event to get notified of a button click:
var dialog = new MessageDialog (this,
DialogFlags.Modal,
MessageType.Info,
ButtonsType.YesNo,
"The Hulk could totally take Super Man");
dialog.Response += (object o, ResponseArgs args) => {
if (args.ResponseId == ResponseType.Yes) {
Console.WriteLine("Yes clicked");
} else if (args.ResponseId == ResponseType.No) {
Console.WriteLine("No clicked");
} else if (args.ResponseId == ResponseType.DeleteEvent) {
Console.WriteLine("Dialog closed without clicking a button");
}
dialog.Destroy();
};
dialog.Run();

Related

selenium Webdriver file upload

How to upload file in a pop up..?
First i want to click ACT button
Then a pop up will be displayed
3.Then want to upload a file in that pop up after clicking file upload button present in that pop
try to switch to the pop-up
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String childWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
childWindowHandler = iterator.next();
}
driver.switchTo().window(childWindowHandler); // switch to popup window
// perform operations on popup
try {
assertTrue(isElementPresent(By.xpath("//div[#id='mainDocumentContainer']/div/div[2]/div/div/div[2]/div/table/tbody/tr/td")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
try {
assertEquals("Numéro d'opération : Epicure 1", driver.findElement(By.xpath("//div[#id='mainDocumentContainer']/div/div[2]/div/div/div[2]/div/table/tbody/tr[2]/td")).getText());
} catch (Error e) {
verificationErrors.append(e.toString());
//Back to main window
driver.switchTo().window(parentWindowHandler); String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String childWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
childWindowHandler = iterator.next();
}
driver.switchTo().window(childWindowHandler); // switch to popup window
// perform operations on popup
try{
}catch{
}
//Back to main window
driver.switchTo().window(parentWindowHandler);
and use this to add your file
driver.findElement(by.Yourlocator).sendKeys("pathToYourFile");
Hope this will help you :)
This pop us not a new window..it's kind of alert or a small pop up where we need to upload documents
You can try driver.switchTo().alert().yourAction();

How to click “ok” in popup “Message from webpage” in selenium c#?

I have an application in which after clicking button, a popup comes titled Message from Webpage showing the error message.I want to click Ok button on it and verify the error message.
I tried the following code
public void clickButtonWithOk()
{
try
{
Thread.Sleep(Convert.ToInt32(GlobalVariables.WaitTime.ToString()) * 1000);
// Get element at runtime
inputData = "45";
waitForElementDisplayed();
int runTimeObjectSize = driver.FindElements(getApplicationObject(currentObjectName, currentObjectType, currentObjectUniqueId)).Count;
if (runTimeObjectSize == 0)
STEPLogger.logmessage(STEPLogger.FAIL, "FAILED to click button " + currentObjectName + " Object NOT Found");
else
{
IWebElement element = driver.FindElements(getApplicationObject(currentObjectName, currentObjectType, currentObjectUniqueId))[0];
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("window.confirm = function(msg){return true;};");
executor.ExecuteScript("arguments[0].click();", element);
STEPLogger.logmessage(STEPLogger.PASS, "SUCCESSFULLY clicked on button " + currentObjectName);
}
}
catch (Exception oException)
{
STEPLogger.logmessage(STEPLogger.FAIL, "FAILED clickButton:" + oException.ToString());
}
} // End of CLICKBUTTONWithOk

How to verify whether new tab opened or not in selenium webdriver?

I have a situation where I have to check whether after clicking on a link a new tab is opened or not. If the tab opens I want to check the title as well.
Does anyone have any idea about this.
Here is the same for C#; this is tested and will work whether the link opens in new tab or new window.
var browserTabs = driver.WindowHandles;
driver.SwitchTo().Window(browserTabs[1]);
//check is it correct page opened or not (e.g. check page's title or url, etc.)
// ...
//close tab and get back
driver.Close();
driver.SwitchTo().Window(browserTabs[0]);
Hope this helps anyone who comes across this thread.
Try to switch to a new tab and then verify whether is it correct page or not.
In Java it can be look like:
//get window handlers as list
List<String> browserTabs = new ArrayList<String> (driver.getWindowHandles());
//switch to new tab
driver.switchTo().window(browserTabs .get(1));
//check is it correct page opened or not (e.g. check page's title)
//...
//then close tab and get back
driver.close();
driver.switchTo().window(browserTabs.get(0))
You can use below method to implement the desired wait until the tab is fully loaded.
public static void switchTabs(WebDriver driver, int expectedWindowsCount,int SwitchtoWindow) throws Exception {
(new WebDriverWait(driver, 30)).until(ExpectedConditions.numberOfWindowsToBe(expectedWindowsCount));
ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs2.get(SwitchtoWindow));
}
The method will check how many Active window handlers are available and wait until desired handlers are present and switch to the tab afterwards.
Use wait for no of tabs to open to make sure a new tab is opened
(new WebDriverWait(driver, 30)).until(ExpectedConditions.numberOfWindowsToBe(2));
Then switch to the new tab
protected void switchTabsUsingPartOfUrl(String platform) {
String currentHandle = null;
try {
final Set<String> handles = driver.getWindowHandles();
if (handles.size() > 1) {
currentHandle = driver.getWindowHandle();
}
if (currentHandle != null) {
for (final String handle : handles) {
driver.switchTo().window(handle);
if (currentUrl().contains(platform) && !currentHandle.equals(handle)) {
break;
}
}
} else {
for (final String handle : handles) {
driver.switchTo().window(handle);
if (currentUrl().contains(platform)) {
break;
}
}
}
} catch (Exception e) {
System.out.println("Switching tabs failed");
}
}
Call this method and pass parameter a substring of url of the tab you want to switch to
driver.getTitle().equals("Title");
Then verify page title
This would help you if you are doing test automation in Groovy
def browserTabs = driver.getWindowHandles()
driver.switchTo().window(browserTabs[1])
assert //Validate your new page title//

I am not able close alert in selenium

I have drop down and selected value from dropdown ,select is having onchange javascript function ,it will raise alert window .I am not able close alert in selenium.
handle alert by using below code:
#Test
public void testAlertOk()
{
//Now we would click on AlertButton
try {
//Now once we hit AlertButton we get the alert
Alert alert = driver.switchTo().alert();
//Text displayed on Alert using getText() method of Alert class
String AlertText = alert.getText();
//accept() method of Alert Class is used for ok button
alert.accept();
//Verify Alert displayed correct message to user
assertEquals("this is alert box",AlertText);
} catch (Exception e) {
e.printStackTrace();
}
}
If I am reading this right, you aren't looking for anything contained in the alert, just a way around the alert and continue with the test.
try
{
driver.SwitchTo().Alert().Accept();
//driver.SwitchTo().Alert().Dismiss();
}
catch (NoAlertPresentException)
{
}

MessageDialog in Windows 8 xaml

My Code:
MessageDialog msg = new MessageDialog("Are you sure to cancel the booking?", "Confirmation");
msg.Commands.Add(new UICommand("Confirm", new UICommandInvokedHandler(CommandHandler)));
msg.Commands.Add(new UICommand("Cancel", new UICommandInvokedHandler(CommandHandler)));
msg.DefaultCommandIndex = 1;
msg.CancelCommandIndex = 1;
await msg.ShowAsync();
private async void CommandHandler(IUICommand command)
{
var commandLabel = command.Label;
switch (commandLabel)
{
case "Confirm":
CancelBookingTickets();
break;
case "Cancel":
break;
}
}
protected async void CancelBookingTickets()
{
MessageDialog msg1 = new MessageDialog("The cancellation process is complete", "Complete");
await msg1.ShowAsync();
}
I am trying to use the nested MessageDialog box in my Windows 8 xaml app but when I reach to the msg1.ShowAsync(), it fires an exception saying "Access is denied".
Can anybody help me out with this problem?
You are facing problem of multiple MessageDialog at once.
How to allow for multiple popups at once in WinRT?