如果元素不存在则继续测试(使用try catch)

时间:2017-05-13 17:02:47

标签: php selenium

我一直在玩Selenium,我遇到了问题。如果元素不存在,我有问题继续测试。

这是我的php代码

if ($this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl05_liPage') != false) {
    $nrOfPages = $this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl05_liPage');
}
if ($this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl04_liPage') != false) {
    $nrOfPages = $this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl04_liPage');
}
if ($this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl03_liPage') != false) {
    $nrOfPages = $this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl03_liPage');
}
if ($this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl02_liPage') != false) {
    $nrOfPages = $this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl02_liPage');
}
if ($this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl01_liPage') != false) {
    $nrOfPages = $this->seePageHasElement('ctl00_cphMain_ResultsPager_repPager_ctl01_liPage');
}

function seePageHasElement($id)
    {
        try {
            $element = $this->webDriver->findElement(WebDriverBy::id($id));
        } catch (\PHPUnit_Framework_AssertionFailedError $f) {
            return false;
        }
        return $element;
    }

这是来自selenium服务器的错误

There was 1 error:

1) MyTests::testSpiderThat
Facebook\WebDriver\Exception\NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"ctl00_cphMain_ResultsPager_repPager_ctl05_liPage"}
  (Session info: chrome=58.0.3029.110)
  (Driver info: chromedriver=2.29.461591 (62ebf098771772160f391d75e589dc567915b233),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 40 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: '123123', ip: '172.18.11.210', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.29.461591 (62ebf098771772160f391d75e589dc567915b233), userDataDir=C:\Users\myComp\AppData\Local\T
emp\scoped_dir5528_32323}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=58.0.3029.110, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, loc
ationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=}]
Session ID: 7220150b6e8bdc6a539de93f2771bf78
*** Element info: {Using=id, value=ctl00_cphMain_ResultsPager_repPager_ctl05_liPage}

所以问题是如果元素不存在,如何正确跳过/抑制此错误?我想继续我的测试,但它只是停止...也许selenium独立版本存在问题? 如果您需要任何其他信息,请告诉我,我会提供。谢谢

1 个答案:

答案 0 :(得分:1)

您使用的方法与我自己使用的方法非常相似。我正在使用Codeception框架,它使Selenium的编写测试对我来说更容易。

使用Codeception,只有当元素在页面上可见时才从元素中获取值,这对我来说很好:

if($I->seePageHasElement(['id' => 'my_first_element_id'])) {
    $nrOfPages = $I->grabTextFrom(['id' => 'my_element_id']);
}

public function seePageHasElement($element)
{
    try {
        $this->getModule('WebDriver')->seeElement($element);
    } catch (\PHPUnit_Framework_AssertionFailedError $f) {
        return false;        
    }
    return true;
}

如果您没有使用Codeception,可能需要将方法重写为类似的东西(请注意,您可以尝试同时捕获不同类型的异常):

public function seePageHasElement($element)
{
    try {
        $this->webDriver->seeElement($element);
    } catch (\PHPUnit_Framework_AssertionFailedError $f) {
        return false;
    } catch (\NoSuchElementException $f) {
        return false;
    } catch (\Exception $f) {
        return false;
    }
    return true;
}

另一方面,如果你想让测试失败,但又不想让测试停止,那么Codeception就会再次出现问题。所有的see()方法都有相应的canSee()方法。使用canSee()方法,测试将失败,但执行不会停止。