硒 php-webdriver 点击后检查属性


<a class="lnk" href="http://www.google.com">Go Google</a>

单击此链接时,将附加另一个 css 类"加载"。在重定向到 google.com 之前,我们如何对此进行测试。

$element = $driver->findElement(WebDriverBy::className('lnk'));
$element->click();

有没有办法在重定向到目的地之前检查类属性是否包含"加载"?

您可以使用 JavaScript 编辑href属性并添加它超时

$element = $driver->findElement(WebDriverBy::className('lnk'));
$href = $element->getAttribute('href');
$script = "javascript:setTimeout( function() { window.location = {$href} }, 5000 );"
$driver->executeScript("arguments[0].setAttribute('href', arguments[1]);", $element, $script);

现在你有足够的时间来检查class属性是否包含loading

$element = $driver->findElement(WebDriverBy::className('lnk'));
$class = $element->getAttribute('class');
if (strpos($class, 'loading') !== false) { }

我不熟悉Selenium和JavaScript(包括英语,所有的机器都是从英语翻译过来的(。 如果有任何错误,请纠正我。

我认为默认情况下,测试单击事件很难通过单击访问连接。因此,只要我们在测试点击之前删除默认点击,您就可以执行所需的测试。 这是代码: .html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<style>
.loading{
font-size: 40px;
}
</style>
</head>
<body>
<a id="google" onclick="loading()" href="https://google.com">Google</a>
</body>
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
function loading()
{
$("#google").attr('class', 'loading');
}
</script>
</html>

测试代码:

<?php
use FacebookWebDriverFirefoxFirefoxDriver;
use FacebookWebDriverFirefoxFirefoxProfile;
use FacebookWebDriverRemoteDesiredCapabilities;
use FacebookWebDriverRemoteRemoteWebDriver;
include './vendor/autoload.php';
$profile = new FirefoxProfile();
$caps = DesiredCapabilities::firefox();
$caps->setCapability(FirefoxDriver::PROFILE, $profile);
$driver = RemoteWebDriver::create('localhost:4444/wd/hub', $caps);
$driver->get("http://localhost/test.php");
$element = $driver->findElement(FacebookWebDriverWebDriverBy::id('google'));
//Cancel the default click event to perform the test
$driver->executeScript('   document.testLinkClick = function (event){ event.preventDefault() }   ');
$driver->executeScript('   $("#google").bind( "click", document.testLinkClick ); ');
$element->click();
$driver->wait(3, 500)->until(function () use ($element){
$class = $element->getAttribute('class');
if (strpos($class, 'loading') !== false) return true;
}, 'error');
//Restore the default click event to perform subsequent tests
$driver->executeScript('$("#google").removeClass("loading")');
$driver->executeScript('$("#google").unbind("click", document.testLinkClick)');
$element->click();    //继续你的测试

原谅我的机器翻译英语

英语

最新更新