首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在安卓/iOS手机测试- C#中拍摄截图?

如何在安卓/iOS手机测试- C#中拍摄截图?
EN

Stack Overflow用户
提问于 2016-11-22 13:46:14
回答 2查看 158关注 0票数 1

如何用C#语言对安卓/iOS手机执行自动化测试的不同步骤进行屏幕截图?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-22 14:25:47

这里似乎有关于如何做到这一点的许多建议

这是OnCreateView()中的代码之一

代码语言:javascript
运行
复制
public static Android.Content.Context Context { get; private set; }

public override View OnCreateView(View parent, string name, Context context, IAttributeSet attrs)
{
MainActivityContext = context;
return base.OnCreateView(parent, name, context, attrs);
}`

Then, I wrote a service implemenatation in which I take a screen capture, by using the static Context of the MainActivity, like this :

`public class SnapshotService : ISnapshotService
{
public void Capture()
{
var screenshotPath = 
Android.OS.Environment.GetExternalStoragePublicDirectory("Pictures").AbsolutePath +
Java.IO.File.Separator + 
"screenshot.png";
var rootView = ((Android.App.Activity)MainActivity.Context).Window.DecorView.RootView;

    using (var screenshot = Android.Graphics.Bitmap.CreateBitmap(
            rootView.Width, 
            rootView.Height, 
            Android.Graphics.Bitmap.Config.Argb8888))
    {
        var canvas = new Android.Graphics.Canvas(screenshot);
        rootView.Draw(canvas);

        using (var screenshotOutputStream = new System.IO.FileStream(
                    screenshotPath, 
                    System.IO.FileMode.Create))
        {
            screenshot.Compress(Android.Graphics.Bitmap.CompressFormat.Png, 90, screenshotOutputStream);
            screenshotOutputStream.Flush();
            screenshotOutputStream.Close();
        }
    }
}
}`
票数 1
EN

Stack Overflow用户

发布于 2016-11-30 20:20:57

C#

您可以使用以下代码:

代码语言:javascript
运行
复制
ScenarioContext.Current.Get<IApp>("Application").Screenshot(ScenarioStepContext.Current.StepInfo.Text);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40743499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档