Question

I'm trying to use Codeception (acceptance testing with webdriver/selenium) to test my application and would like to be able to grab the value of a link after I've performed an action.

Basically I want to log out of the system and then click the link so I need to store the url in a variable for use after the log out action.

It's kind of a CMS operation like this:

  1. Log in as admin
  2. Create content
  3. Content created and link displayed to user

I want to be able to then

  1. Grab the href value of the link
  2. Log out
  3. Go to the URL and check for certain page content.
Was it helpful?

Solution

Codeception has no method to grab href of the link because it too special case, but you can define your own action in WebHelper class to grab href using Mink API. Read more about Modules and Helpers.

Here is example of action that grabs href of a link:

public function grabHref($locator)
{
    $webDriver = $this->getModule('Selenium2');

    $link = $webDriver->session->getPage()->findLink($locator);
    if ($link) {
        return $link->getAttribute('href');
    }

    $this->fail("Link '{$locator}' not found");           
}

Place this function in _helpers/WebHelper.php file, then enable WebHelper module in your test suit config:

class_name: WebGuy
    modules:
        enabled: [Selenium2, WebHelper] 

Then run codecept build to rebuild your WebGuy class.

Now, you can use your grabHref action in your cept/cest files:

$I = new WebGuy($scenario);
$I->wantTo('Grab Href');

$I->amOnPage('/');

$href = $I->grabHref('Link text or ID or Title');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top