How to assert page/tab/window title in Behat + Mink

只谈情不闲聊 提交于 2019-12-07 11:40:29

You should use a regular find by css for the title tag and use getText() to get the title.

The css should be: "head title"

Your solution is almost ok, you need to watch for possible exception, especially fatal ones that can stop your suite if encountered.

For example find() method will return an object or null, if null is returned and you are using getText() on it it will result in a fatal exception and your suite will stop.

Slightly improved method:

/**
 * @Given /^the page title should be "([^"]*)"$/
 */
public function thePageTitleShouldBe($expectedTitle)
{
    $titleElement = $this->getSession()->getPage()->find('css', 'head title');
    if ($titleElement === null) {
        throw new Exception('Page title element was not found!');
    } else {
        $title = $titleElement->getText();
        if ($expectedTitle !== $title) {
            throw new Exception("Incorrect title! Expected:$expectedTitle | Actual:$title ");
        }
    }
}

Improvements:

  • handled possible fatal exception
  • throw exception if element not found
  • throw exception with details if titles do not match

Note that you can also use other methods to check the title like: stripos, strpos or simply compare strings like i did. I prefer a simple compare if i need exact text or strpos/stripos method of php and I personally, avoid regular exceptions and associated methods like preg_match which are usually a bit slower.

One major improvement you could do is to have a method for waiting the element and handle the exception for you and use that instead of simple find, find you can use when you need to take decision based on the presence of the element like: if element exists do this else..

Thanks Lauda. Yes, that indeed worked. Wrote the function below:

/**
     * @Given /^the page title should be "([^"]*)"$/
     */
    public function thePageTitleShouldBe($arg1)
    {
        $actTitle = $this->getSession()->getPage()->find('css','head title')->getText();
        if (!preg_match($arg1, $actTitle)) {
            throw new Exception ('Incorrect title');
        }
    }

This didn't work for me in cases where the title is manipulated using Javascript and history.pushState/replaceState

Here an implementation that works for Javascript:

  /**
   * @Then /^the title is "([^"]*)"$/
   */
  public function theTitleIs($arg1) {
    $title = $this->getSession()->evaluateScript("return document.title");
    if ($arg1 !== $title) {
      throw new \Exception("expected title '$arg1', got '$title'");
    }
  }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!