2017-05-13 47 views
1

我一直在玩Selenium,并遇到过问题。如果元素不存在,我遇到问题继续测试。如果元素不存在,则继续测试(使用try catch)

这是我的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; 
    } 

这是硒服务器

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} 

所以现在的问题是如何正确地跳过/制止这种错误,如果内容不存在错误?我想继续我的测试,但它只是停止...也许硒独立版本有问题? 如果您需要任何其他信息,请让我知道,我会提供。谢谢

+0

我对Selenium一无所知,但我的教育猜测是,您正试图捕获错误类型的'Exception',即:'PHPUnit_Framework_AssertionFailedError'。然而,抛出的'Exception'类型是'.. \ NoSuchElementException'。所以,除非后者扩展前者,否则应该捕获'.. \ NoSuchElementException'(也是),或者更通用的'Exception'。 –

回答

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()方法,测试将失败,但执行不会停止。

+0

干得好... thx求助 –

+0

不客气! – sunomad