简体   繁体   English

编码的用户界面:无法捕获通过测试用例的屏幕截图

[英]Coded UI: Not able to capture a screen shot for pass test case

I am using coded UI platform for automation. 我正在使用编码的UI平台进行自动化。 If test case fails system automatic take screen shot but once it pass system is not able to capture screenshot of the test case. 如果测试用例失败,系统将自动截屏,但一旦通过,系统将无法捕获测试用例的屏幕截图。 I am using C# selenium commands in the script. 我在脚本中使用C#硒命令。

Environment 环境

  • Visual studio premium 2012. Visual Studio Premium 2012。

I tried following thing. 我尝试了以下操作。

  1. Enable a log trace in QTAgent32.exe.config ( ). 在QTAgent32.exe.config()中启用日志跟踪。

  2. LoggerOverrideState = HtmlLoggerState.AllActionSnapshot; LoggerOverrideState = HtmlLoggerState.AllActionSnapshot; but getting error in LoggerOverrideState. 但是在LoggerOverrideState中出错。

     [TestMethod] public void demo2() { TestContext.WriteLine("Go to URL\\n"); driver.Navigate().GoToUrl("http://www.test.com/"); driver.Manage().Window.Maximize(); // Enter username TestContext.WriteLine("TestContext Writeline: test context \\n"); js.ExecuteScript("arguments[0].setAttribute('value','username')", driver.FindElement(By.XPath("//*[@id='txtUsername']"))); //Enter password js.ExecuteScript("arguments[0].setAttribute('value','password')", driver.FindElement(By.XPath("//*[@id='txtPassword']"))); // Click on the login Button js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[@id='btLoginNow']"))); 

You could put something like the following in your code to take a screenshot at any given point: 您可以在代码中添加以下内容,以便在任意给定的时间点截屏:

Image SubmissionPic = UITestControl.Desktop.CaptureImage();
SubmissionPic.Save(@"C:\AutomatedScreenShots\SubmissionPage_" + TestContext.DataRow["Division"].ToString() + "_" + DateTime.Now.ToString("yyyy-MM-dd") + ".bmp");

Note: The filename formatting I've used above can be changed to whatever suits your needs. 注意:我上面使用的文件名格式可以更改为适合您需要的格式。 I just pulled this code from a test I'd written a while back. 我只是从前一段时间编写的测试中提取了这段代码。

After every Testcase you can call the Take Screen shot in both the case either Pass or Fail ex. 在每个测试用例之后,您都可以在通过或失败情况下调用“拍摄屏幕快照”。 Suppose Test Method to do Successful Login 假设测试方法可以成功登录

@Test 
public void ValidUserNamePasswordLoginTest() {
    // Your code to do Assertion
}

Now use @AfterMethod in your call which will execute as soon as your test perform 现在在调用中使用@AfterMethod,它将在测试执行后立即执行

@AfterMethod
public void takeScreenShot(ITestResult testResult) throws IOException 
{
    if(testResult.getStatus() == ITestResult.SUCCESS)
    {
      File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(src, new File("Screensot\\"+filename+".jpg"));
    }
if(testResult.getStatus() == ITestResult.FAILURE)
    {
          File src = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(src, new File("Screensot\\"+filename+".jpg"));
    }

}

Couldn't you just use the [TestCleanup] and create a method using that Attribute that will take a screenshot, that way you will always get a Screenshot not matter the result? 您是否不能只使用[TestCleanup]并使用该属性创建将截取屏幕截图的方法,那样您始终可以获得截屏而不管结果如何? Is that what you require? 那是你所需要的吗?


So, You could so this on the WebDriver setup: 因此,您可以在WebDriver设置中这样做:

public static IWebDriver SetUp()
    {
        private static IWebDriver _webDriver;
        firingDriver = new EventFiringWebDriver(new ChromeDriver()); //Or whatever driver you want. You could also use the Activator class and create a Generic instance of a WebDriver. 
        var screenshot = new CustomScreenshot(firingDriver);
        firingDriver.ScriptExecuted += screenshot.TakeScreenshotOnExecute;

        _webDriver = firingDriver;
        return _webDriver;
    }

Then for the screenshot something like: 然后,对于屏幕截图,类似:

public class CustomScreenshot
    {
    private IWebDriver _webDriver;
    public CustomScreenshot(IWebDriver webDriver)
    {
        _webDriver = webDriver;
    }

public void TakeScreenshotOnExecute(object sender, WebDriverScriptEventArgs e)
    {
        var filePath = "Where you want to save the file.";
        try
        {
            _webDriver.TakeScreenshot().SaveAsFile(filePath, ImageFormat.Png);
        }
        catch (Exception)
        {
            //DO something
        }
    }
}

The whole issue of taking a screen shot of a browser window & naming it based on unique properties (eg, browser title, date-time stamp, etc) proved to be extremely difficult because of the tool (CodedUI) not what I wanted to do. 由于没有使用我想做的工具(CodedUI),事实证明,为浏览器窗口拍摄屏幕快照并根据独特的属性(例如,浏览器标题,日期时间戳等)将其命名的问题非常困难。 。 I won't editorialize concerning my opinion of the functionality (lack of) of this tool but it is definitely not as robust as Selenium. 我不会就此工具的功能(缺乏)发表看法,但绝对不如Selenium健壮。 So how I accomplished this was I got a high level 'browser title' from this-> and used the following for taking & storing the screen shot in my MyTestCleanup() method. 因此,我是如何从this->中获得一个高级“浏览器标题”的,并使用以下内容在MyTestCleanup()方法中获取并存储屏幕截图。

    public void MyTestCleanup()
    {
        //Playback.PlaybackSettings.WaitForReadyLevel = WaitForReadyLevel.AllThreads;
        //--------------------------------------------------------------------------------------------
        DateTime time = DateTime.Now;
        this.UIMap
        var ttest = time.Millisecond.ToString();
        var timeSecs = DateTime.Now.Millisecond;
        var Window = this; 
        var ext = ".png";
        Image screen = UITestControl.Desktop.CaptureImage();
        string dirPath = string.Format("C\\:Users\\smaretick\\Desktop\\SCREENS\\{0}{1}", ttest, ext);
        string dirPathN = string.Format("/C copy C:\\Users\\smaretick\\Desktop\\SCREENS\\CUIT.png C:\\Users\\smaretick\\Desktop\\SCREENS\\{0}{1}", ttest, ext);
        string dirPathF = string.Format("/C copy C:\\Users\\smaretick\\Desktop\\SCREENS\\CUIT.png C:\\Users\\smaretick\\Desktop\\SCREENS\\{0}{1}{2}", Window, ttest, ext);
        //string dirPathF = string.Format("/C copy C:\\Users\\smaretick\\Desktop\\SCREENS\\{0}{1}{2}", Window, ttest, ext);
        UITestControl.Desktop.CaptureImage().Save("C:\\Users\\smaretick\\Desktop\\SCREENS\\CUIT.png");
        string cmdF = string.Format("C\\:Users\\smaretick\\Desktop\\SCREENS\\{0}{1}", ttest, ext);
        Process.Start("CMD.exe", dirPathF);
        //--------------------------------------------------------------------------------------------
        LogOut();

contact me if you need any more help (scottmaretick51@gmail.com) 如果需要更多帮助,请与我联系(scottmaretick51@gmail.com)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM