This is a migrated thread and some comments may be shown as answers.

Coded screenshot of Alert Dialog

1 Answer 78 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 2
Daniel asked on 12 Sep 2013, 08:45 PM
Using the code provided to make a custom Dialog handler, I am now trying to capture the Alert dialog as a screenshot and have been unsuccessful using both Manager.ActiveBrowser.Capture() and Manager.ActiveBrowser.Window.GetBitmap().  Both attempts save the window behind the Alert dialog.

Any help would be appreciated!

public override void OnBeforeTestStarted()
{
    AlertDialog myAlertDialog = new AlertDialog(ActiveBrowser, DialogButton.OK);
    myAlertDialog.HandlerDelegate = new DialogHandlerDelegate(MyCustomAlertHandler); 
    //Manager.DialogMonitor.AddDialog(new AlertDialog(ActiveBrowser, DialogButton.OK));
    Manager.DialogMonitor.AddDialog(myAlertDialog);
    Manager.DialogMonitor.Start();
}
public void MyCustomAlertHandler(IDialog dialog)
{
    // Simply close the dialog 
    System.Threading.Thread.Sleep(1500);
    System.Drawing.Bitmap image = Manager.ActiveBrowser.Capture();
    //System.Drawing.Bitmap image = Manager.ActiveBrowser.Window.GetBitmap();
      
    image.Save(this.ExecutionContext.DeploymentDirectory.ToString() + @"\Data\alert.png", System.Drawing.Imaging.ImageFormat.Png);
    dialog.Window.Close();
    try
    {
        // Wait until it is closed 
        dialog.Window.WaitForVisibility(false, 50);
        Log.WriteLine("Alert Handled!");
    }
    catch
    {
        Log.WriteLine("Failed to handle alert!");
    }
}

1 Answer, 1 is accepted

Sort by
0
Daniel
Top achievements
Rank 2
answered on 13 Sep 2013, 12:25 PM
Solved.

Instead of trying to user Manager.ActiveBrowser.Window.GetBitmap() or Manager.ActiveBrowser.Capture(), had to use dialog.Window.GetBitmap().

public void MyCustomAlertHandler(IDialog dialog)
{
    System.Drawing.Bitmap image = dialog.Window.GetBitmap();         
    image.Save(this.ExecutionContext.DeploymentDirectory.ToString() + @"\Data\alert.png", System.Drawing.Imaging.ImageFormat.Png);
    dialog.Window.Close();
    try
    {
        // Wait until it is closed 
        dialog.Window.WaitForVisibility(false, 50);
        Log.WriteLine("Alert Handled!");
    }
    catch
    {
        Log.WriteLine("Failed to handle alert!");
    }
}
Tags
General Discussions
Asked by
Daniel
Top achievements
Rank 2
Answers by
Daniel
Top achievements
Rank 2
Share this question
or