gpt4 book ai didi

PHPUnit & Selenium2 如果一个案例失败,所有案例都会失败

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:38:42 25 4
gpt4 key购买 nike

我正在使用 PHPUnit 和 Selenium2 服务器。我正在使用 PageObject 模式。对于页面对象,我获得了 webdriver 的实例并执行必要的功能。

为了让单个浏览器保持运行,我实现了一个粗略的解决方案,我在网上找到了一个在静态类中初始化驱动程序的解决方案:

class SessionHelper {
public static $first;
}
SessionHelper::$first = 0;

然后在我的测试用例类的setup()方法中;

public function setUp(){
if (SessionHelper::$first == 0 )
{
$this->setHost('localhost');
$this->setPort((int)4444);
$this->setBrowser('firefox');
$this->setBrowserUrl('http://domain.com/lucky/web');
$this->shareSession(TRUE);
$this->prepareSession();
SessionHelper::$first = 1 ;
}
}

通过这种方式,我设法在单个浏览器中执行所有测试。但是,如果一个测试用例失败;比如说试图找到一个不存在的元素,所有其他测试用例都会失败并显示消息“ undefined index :browserUrl”。如果我更改它以在页面中查找已知元素,它就可以正常工作。例如;

test_method_1 :如果在此测试失败后未找到任何元素,并显示“ undefined index :browserUrl”。

如果 test_method_1 正常,其余测试将执行,直到另一个测试用例失败。

那么,我得到这个错误的原因是什么?当一个测试用例失败时,我的 session 会被销毁吗?

最佳答案

当一个测试用例失败时,您的 session 将被销毁:

The session will be reset in the case of not successul tests (failed or incomplete); it is up to the user to avoid interactions between tests by resetting cookies or logging out from the application under test (with a tearDown() method)

来自 Chapter 17. PHPUnit and Selenium (底部)

方法 onNotSuccessfulTestflag 标记 session 将 session 设置为空。在下次测试运行时,PHPUnit 调用 prepareSession() ,但参数为空。这就是在 SessionStrategy_Shared 中出现“未定义索引”错误的原因.

您可以编写onNotSuccessfulTest 方法:

public function onNotSuccessfulTest(Exception $e){
throw $e;
}

有了它 session 不会被破坏。

关于PHPUnit & Selenium2 如果一个案例失败,所有案例都会失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19908101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com