热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何在测试失败时使用PHPUnit和Selenium2捕获屏幕截图?

如何解决《如何在测试失败时使用PHPUnit和Selenium2捕获屏幕截图?》经验,为你挑选了2个好方法。

我正在使用PHPUnit 4.6和PHPUnit Selenium 1.4.2与PhantomJS.当selenium测试失败时,我想捕获最后一页的截图.在PHPUnit Manual中有一个Selenium 1的例子,但我正在尝试使用Selenium 2,因为我需要使用GhostDriver.

WebTestCase.php

class WebTestCase extends PHPUnit_Extensions_Selenium2TestCase
{
    protected $captureScreenshotOnFailure= TRUE;
    protected $screenshotPath = '/../../screenshots';
    protected $screenshotUrl = 'http://localhost:8080/screenshots';

    protected function setUp() {
        $this->setBrowser('phantomjs');
        $this->setBrowserUrl("http://localhost:8080");
        $this->setHost('localhost');
    }
}

test.php的

class Test extends WebTestCase
{

    public function testTitle()
    {
        $this->url('');
        assertEquals($this->title(), "My App");
    }
}

但这不是截图.

$ vendor/bin/phpunit 
PHPUnit 4.6-ge85198b by Sebastian Bergmann and contributors.

Configuration read from /MyApp/phpunit.xml

F

Time: 231 ms, Memory: 5.50Mb

There was 1 failure:

1) Test::testTitle
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-''
+'My App'

/MyApp/tests/functional/Test.php:7

FAILURES!                            
Tests: 1, Assertions: 1, Failures: 1.

Silas Palmer.. 6

结合@Jens A. Koch和@John Joseph的解决方案,我们得到了这个:

listener = new PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener($screenshots_dir);

        $this->setBrowser('firefox');
        $this->setBrowserUrl('https://netbeans.org');
    }

    public function tearDown() {
        $this->stop();
    }

    public function testNetbeansContainsHorses() {
        $this->url('https://netbeans.org');
        $this->assertContains('Equestrian', $this->title()); // Will fail on NetBeans page.
    }


    public function onNotSuccessfulTest($e) {
        $this->listener->addError($this, $e, null);        
        parent::onNotSuccessfulTest($e);
    }
}


Jens A. Koch.. 5

嗯.SeleniumTestCase和Selenium2TestCase之间的区别在PHPUnit手册中没有很好的记录.对于Selenium2,常见案例也没有明确的分离和充分的使用示例.

PHPUnit_Extensions_Selenium2TestCase上不存在$ captureScreenshotOnFailure .

无论如何,让我们尝试将它们组合在一起:

setBrowser('phantomjs');
        $this->setBrowserUrl("http://localhost:8080");
        $this->setHost('localhost');
    }

    public function testEnterText()
    {
        $this->url("/");

        try {

            $this->assertEquals($this->title(), "My App");

        } catch (Exception $e) {

            $this->screenshot( __DIR__.'/'.$this->getName().'-'.time(). '.png');    
        }
    }

    public function screenshot($file) 
    {
        $filedata = $this->currentScreenshot();
        file_put_contents($file, $filedata);
    }
}

try-catch-block:在try部分中断言完成,如果断言失败,则捕获异常.catch-block让我们有机会(抓住异常的详细信息或重新抛出它或者)制作屏幕截图.

主要功能是$ this-> currentScreenshot(),在此测试中使用 https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php#L733

ScreenshotListener

请注意,有一个ScreenshotListener,可能值得一看:https: //github.com/giorgiosironi/phpunit-selenium/blob/master/PHPUnit/Extensions/Selenium2TestCase/ScreenshotListener.php

有关使用示例,请访问https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCase/ScreenshotListenerTest.php

这可能是一个更清晰的实现,以抓住测试失败和拍摄.



1> Silas Palmer..:

结合@Jens A. Koch和@John Joseph的解决方案,我们得到了这个:

listener = new PHPUnit_Extensions_Selenium2TestCase_ScreenshotListener($screenshots_dir);

        $this->setBrowser('firefox');
        $this->setBrowserUrl('https://netbeans.org');
    }

    public function tearDown() {
        $this->stop();
    }

    public function testNetbeansContainsHorses() {
        $this->url('https://netbeans.org');
        $this->assertContains('Equestrian', $this->title()); // Will fail on NetBeans page.
    }


    public function onNotSuccessfulTest($e) {
        $this->listener->addError($this, $e, null);        
        parent::onNotSuccessfulTest($e);
    }
}



2> Jens A. Koch..:

嗯.SeleniumTestCase和Selenium2TestCase之间的区别在PHPUnit手册中没有很好的记录.对于Selenium2,常见案例也没有明确的分离和充分的使用示例.

PHPUnit_Extensions_Selenium2TestCase上不存在$ captureScreenshotOnFailure .

无论如何,让我们尝试将它们组合在一起:

setBrowser('phantomjs');
        $this->setBrowserUrl("http://localhost:8080");
        $this->setHost('localhost');
    }

    public function testEnterText()
    {
        $this->url("/");

        try {

            $this->assertEquals($this->title(), "My App");

        } catch (Exception $e) {

            $this->screenshot( __DIR__.'/'.$this->getName().'-'.time(). '.png');    
        }
    }

    public function screenshot($file) 
    {
        $filedata = $this->currentScreenshot();
        file_put_contents($file, $filedata);
    }
}

try-catch-block:在try部分中断言完成,如果断言失败,则捕获异常.catch-block让我们有机会(抓住异常的详细信息或重新抛出它或者)制作屏幕截图.

主要功能是$ this-> currentScreenshot(),在此测试中使用 https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php#L733

ScreenshotListener

请注意,有一个ScreenshotListener,可能值得一看:https: //github.com/giorgiosironi/phpunit-selenium/blob/master/PHPUnit/Extensions/Selenium2TestCase/ScreenshotListener.php

有关使用示例,请访问https://github.com/giorgiosironi/phpunit-selenium/blob/master/Tests/Selenium2TestCase/ScreenshotListenerTest.php

这可能是一个更清晰的实现,以抓住测试失败和拍摄.


推荐阅读
  • 本文详细介绍了如何在Linux系统上安装和配置Smokeping,以实现对网络链路质量的实时监控。通过详细的步骤和必要的依赖包安装,确保用户能够顺利完成部署并优化其网络性能监控。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了如何使用PHP检测AJAX请求,通过分析预定义服务器变量来判断请求是否来自XMLHttpRequest。此方法简单实用,适用于各种Web开发场景。 ... [详细]
  • 导航栏样式练习:项目实例解析
    本文详细介绍了如何创建一个具有动态效果的导航栏,包括HTML、CSS和JavaScript代码的实现,并附有详细的说明和效果图。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 扫描线三巨头 hdu1928hdu 1255  hdu 1542 [POJ 1151]
    学习链接:http:blog.csdn.netlwt36articledetails48908031学习扫描线主要学习的是一种扫描的思想,后期可以求解很 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • This document outlines the recommended naming conventions for HTML attributes in Fast Components, focusing on readability and consistency with existing standards. ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
author-avatar
GIfi炬辉_904
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有