对于具有behat或codeception的每个元素

Jer*_*oen 5 foreach automated-tests behat codeception

我想测试一个具有动态菜单结构的网站.我想遍历所有菜单项并在每个页面上运行相同的测试系列.我们谈论的是100多页,这些页面会发生变化.

我想用behat或codeception做到这一点.

有没有人知道如何做到这一点?

stm*_*llr 1

当 Behat 与 Mink 一起使用时,您可以使用 findAll() 获取菜单项,然后对其进行迭代:

/**
 * @When /^I run my test series for all menu items$/
 */
public function iRunMyTestSeriesForAllMenuItems() {

    $result = TRUE;
    $this->getSession()->visit('http://www.example.com/');
    $links = $this->getSession()->getPage()->findAll('css', '#menu ul li a');
    foreach ($links as $link) {
        $url = $link->getAttribute('href');
        if (FALSE === $this->yourTestHere($url)) {
            $result = FALSE;
        }
    }

    return $result;
}
Run Code Online (Sandbox Code Playgroud)

  • 与其返回 true/false,不如直接抛出异常。Behat 会将其视为步骤失败。 (2认同)