, пожалуйста, спросите его, как получить код состояния запроса ajax и данные ответа?
$capabilities = array(WebDriverCapabilityType::BROWSER_NAME => 'chrome');
$this->webDriver = RemoteWebDriver::create('http://localhost:4444/wd/hub', $capabilities);
$this->webDriver->get('http://www.test.com/login');
$username = $this->webDriver->findElement(WebDriverBy::id('name'));
$username->clear();
$username->sendKeys('admin');
$password = $this->webDriver->findElement(WebDriverBy::id('password'));
$password->sendKeys('1234567');
//send ajax request
$this->webDriver->executeScript('loginSub()');
$this->assertEquals('username or password error.', $this->getAlertText());
$password->clear();
$password->sendKeys('123456');
//send ajax request
$this->webDriver->executeScript('loginSub()');
как получить переменные функции обратного вызова ajax "data" и "response" в php?
//javascript code
$.post('/login', {'name':name, 'password':password}, function(data,statusText, response){
//how to get the variable "data" and "response" in php ?
console.log(response.status);
},"json");
следующий код - мой способ подтвердить успешность входа в систему.
try {
$element = $this->getElement(WebDriverBy::cssSelector('.layui-layer-page .layui-layer-content'));
$this->assertEquals('login success', $element->getText());
} catch (NoSuchElementException $e) {
//is anything i should to do here?
print_r('timeout');
}
sleep(2);
$this->webDriver->executeScript("common.showMessage(200, 'test finish, the broswer will close in a sec.')");
sleep(4);
$this->webDriver->close();
получить элемент после запроса ajax.
private function getElement(WebDriverBy $webDriverBy)
{
static $count = 0;
try {
return $this->webDriver->findElement($webDriverBy);
} catch (NoSuchElementException $e) {
$count++;
sleep(1);
}
if ($count > 5) {
throw new NoSuchElementException($webDriverBy->getValue());
}
return $this->getElement($webDriverBy);
}