سؤال

I am using ASP.net, C#.net Intranet application. Selenium Webdriver to test application. one page enter same name it showing alert message "Already Exists" (server side alert using ajax) , I want to capture that alert message with screenshort.

**Selenium webdriver version: 2.35.0.0
dotnet frame work: 4.0**

i am writing code like bellow :

private static void SaveScreenShot(string screenshotFirstName)
{
    var folderLocation = Environment.CurrentDirectory.Replace("\\Out", "") + "\\ScreenShot\\";
    if (!Directory.Exists(folderLocation))
    {
        Directory.CreateDirectory(folderLocation);
    }
    var screenshot = ((ITakesScreenshot)WebDriver).GetScreenshot();
    var filename = new StringBuilder(folderLocation);
    filename.Append(screenshotFirstName);
    filename.Append(DateTime.Now.ToString("dd-mm-yyyy HH_mm_ss"));
    filename.Append(".png");
    screenshot.SaveAsFile(filename.ToString(), System.Drawing.Imaging.ImageFormat.Png);
}

But it showing error message like

"unexpected alert open
(Session info: chrome=31.0.1650.57)
(Driver info: chromedriver=2.3,platform=Windows NT 6.1 x86)"

please help me . it working fine without alert message.

هل كانت مفيدة؟

المحلول

Have tried getting WebDriver to ignore the Alert?

This can be done the following way:

    var options = new InternetExplorerOptions();
    options.UnexpectedAlertBehavior = OpenQA.Selenium.IE.InternetExplorerUnexpectedAlertBehavior.Ignore;

You then start WebDriver like this:

driver = new InternetExplorerDriver(options);

This is how I would do it for IE. I'm not testing Chrome but I should imagine there is a similar method for Chrome.

نصائح أخرى

Going off of OP's original question, "I want to capture that alert message with screenshort" and not just ignoring it. I had a similar situation that I needed a screenshot of the "Alert".

I basically made another method and took the screenshot with CopyFromScreen using the System.Drawing.Graphics class.

And to tell if it was an "alert" and which screenshot method I'd use for "alert" and non-"alert". I just created a simple boolean "Detection" method that tells if an "alert" is present. If it is, return true, if it's not, return false.

public static bool IsAlertPresent()
{
    try
    {
        SeleniumDrivers.driver.SwitchTo().Alert();
        return true;
    }
    catch (Exception)
    {
        return false;
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top