PHPUnit和Webdriver(Selenium2) - 无法将字符串参数传递给waitUntil()方法

时间:2014-03-11 05:43:50

标签: selenium-webdriver phpunit

环境:
- Selenium 2.39独立服务器
- PHP 5.4.11
- PHPUnit 3.7.28
- Chrome V31& ChromeDriver v2.7

我想在阅读样本here后封装一种用waitUntil()方法查找元素的方法。我尝试了几次如下:

代码1:

public function findElement($elementCssPath){
$this->waitUntil(function($testCase) {
try {
$testCase->byCssSelector($elementCssPath);
} catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
return TRUE;
}
}, 8000);
}

代码2:

public function findElement($elementCssPath){
$this->waitUntil(function () {
if ($this->byCssSelector($elementCssPath)) {
return true;
}
return null;
}, 20000);
}

CODE3:

public function findElement($elementCssPath){
$this->waitUntil(function($elementCssPath) {
try {
$this->byCssSelector($elementCssPath);
} catch (PHPUnit_Extensions_Selenium2TestCase_WebDriverException $e) {
return TRUE;
}
}, 8000);
}

但他们都报告了一个错误:

Undefined variable: elementCssPath 

我在互联网上搜索了很多,但没有进一步的信息 请帮忙,谢谢!

1 个答案:

答案 0 :(得分:0)

这段代码最终适合我。

public function findElementWaitUntil($elementCssPath){
    $this->waitUntil(function() use($elementCssPath){
        if ($this->byCssSelector($elementCssPath)) {
                return true;
            }
                return null;
            }, 10000);
    try{
        $object=$this->byCssSelector($elementCssPath);
        return $object;
    }
    catch(Exception $e){
        throw $e;
    }   
}