是的,你可以试试这样的:
$asserts = new \Codeception\Module\Asserts;
// Example 1: position, element found with XPath
$foo = $I->executeInSelenium(function(\WebDriver $webdriver) {
return $webdriver->findElement(WebDriverBy::xpath('//*[@id="sidebar"]'))->getCSSValue('right');
});
codecept_debug( $foo );
$asserts->assertEquals( $foo, '-360px' );
// Example 2: background colour, element found with CSS
$bar = $I->executeInSelenium(function(\WebDriver $webdriver) {
return $webdriver->findElement(WebDriverBy::cssSelector('#sidebar'))->getCSSValue('background-color');
});
codecept_debug( $bar );
$asserts->assertEquals( $bar, 'rgba(73, 73, 73, 1)' );
注意 codecept_debug 行,您需要执行 /path/to/codecept run --debug 才能使它们生效,您将获得如下额外输出:
* I execute in selenium "lambda function"
-360px
* I execute in selenium "lambda function"
rgba(73, 73, 73, 1)
PASSED
除了将 Asserts 作为新类加载(将其添加到 acceptance.suite.yml 中启用的模块对我没有帮助)之外,必须有更好的替代方法——但重要的是,您的结果仍然正确:
OK (1 test, 2 assertions)
日志记录:
请记住,您可以查看 Selenium 日志以验证发生了什么,例如
17:19:02.236 INFO - Executing: [find element: By.xpath: //*[@id="sidebar"]])
17:19:02.243 INFO - Done: [find element: By.xpath: //*[@id="sidebar"]]
17:19:02.246 INFO - Executing: [get value of css property: 0 [[FirefoxDriver: firefox on MAC (64131329-819d-8f4a-8e11-bff91a410f13)] -> id: sidebar], right])
17:19:02.254 INFO - Done: [get value of css property: 0 [[FirefoxDriver: firefox on MAC (64131329-819d-8f4a-8e11-bff91a410f13)] -> id: sidebar], right]
17:19:02.325 INFO - Executing: [find element: By.selector: #sidebar])
17:19:02.334 INFO - Done: [find element: By.selector: #sidebar]
17:19:02.336 INFO - Executing: [get value of css property: 0 [[FirefoxDriver: firefox on MAC (64131329-819d-8f4a-8e11-bff91a410f13)] -> id: sidebar], background-color])
17:19:02.345 INFO - Done: [get value of css property: 0 [[FirefoxDriver: firefox on MAC (64131329-819d-8f4a-8e11-bff91a410f13)] -> id: sidebar], background-color]