如何抓取截图名称

时间:2016-05-02 04:14:44

标签: c# .net selenium

我上课时会截取页面截图并保存图像的日期和时间格式,这样我就有了独特的截图。但我无法弄明白如何在课外调用此方法来截取屏幕并抓取名称在我的控制台上打印。这是我的Utility类我执行截图:

public class Utility
{        
    public static void TakeScreenshot()
    {
        String now = DateTime.Now.ToString("MM-dd-yyy hh-mm tt ");
        try
        {
            Screenshot ss = ((ITakesScreenshot)Driver.Instance).GetScreenshot();
            ss.SaveAsFile(@".\Screenshots\"+now+"Screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            throw;
        }
    }
}

现在我可以在这个方法中调用我的截图类,但是如何获得新创建的截图名称?

[Test]
public void ScreenshotTest()
{
    Utility.TakeScreenshot(); //Here I can perform screenshot but how can I grab screenshot name
    Console.Write("");        // So I can print here
}

2 个答案:

答案 0 :(得分:0)

您可以更改方法而不是返回void,返回字符串然后返回刚拍摄的截图的名称

  string ssName= now+"Screenshot.png";
            Screenshot ss = ((ITakesScreenshot)Driver.Instance).GetScreenshot();
            ss.SaveAsFile(@".\Screenshots\"+now+"Screenshot.png",  System.Drawing.Imaging.ImageFormat.Png);

在方法结束时使用return ssName; 这将允许您将名称作为方法的返回

String ssName= Utility.TakeScreenshot(); 
    Console.Write("ssName");   

答案 1 :(得分:0)

这与Csharls的答案相同。刚解释..

实用程序类

public class Utility
{
    public static string TakeScreenshot()
    {
        String now = DateTime.Now.ToString("MM-dd-yyy hh-mm tt ");
        string FileName = now + "Screenshot.png";
        try
        {
            Screenshot ss = ((ITakesScreenshot)Driver.Instance).GetScreenshot();
            ss.SaveAsFile(@".\Screenshots\" + FileName, System.Drawing.Imaging.ImageFormat.Png);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            throw;
        }
        return FileName;
    }
}

致电位置

[Test]
    public void ScreenshotTest()
    {
        string FileName;
        FileName = Utility.TakeScreenshot(); //Here I can perform screenshot but how can I grab screenshot name
        Console.Write(FileName);        // So I can print here
    }