Valor_
Valor_

Reputation: 3591

Continue test if element not present (using try catch)

I have been playing around with Selenium and i have encountered on problem. I'm having a problem continuing test if element is not present.

This is my php code

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;
    }

And this is error from selenium server

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}

So the question is how to properly skip/suppress this error if elements is not present? I want to continue my test, but it just stops... Maybe there is a problem with selenium standalone version? If you need any additional information's please let me know and i will provide. Thank you

Upvotes: 1

Views: 960

Answers (1)

sunomad
sunomad

Reputation: 1793

The method you are using looks very similar to one I use myself. I am using the Codeception framework, which has made writing tests for Selenium a lot more easier for me.

With Codeception, to get a value from an element only if it is visible on the page, this works fine for me:

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;
}

If you are not using Codeception, maybe you need to rewrite the method into something like this (note that you can try to catch different types of exceptions at the same time):

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;
}

On the other hand, if you want the test to fail, but just not want the test to stop, again Codeception comes to the rescue. All the see() methods have corresponding canSee() methods. With the canSee() methods the test will fail, but execution will not stop.

Upvotes: 2

Related Questions