gpt4 book ai didi

ajax - 这些 MinkExtension 基础上下文适应与 ajax 请求相关吗?

转载 作者:行者123 更新时间:2023-12-02 07:50:46 25 4
gpt4 key购买 nike

实际上,当我们想要处理ajax时,我们必须添加一些 sleep 指令,我真的不喜欢这样,即使最终用户实际上正在“等待”某些东西,我认为它应该是显而易见的基础知识非-ajax 请求。

我真的很想知道为什么它还不存在,我写了一些改编,并希望在可能的 PR 之前得到您的反馈:

https://gist.github.com/lenybernard/6f6c2678a9a2594f923b

diff --git a/src/Behat/MinkExtension/Context/MinkContext.php b/src/Behat/MinkExtension/Context/MinkContext.php
index 90ea038..8d6537b 100644
--- a/src/Behat/MinkExtension/Context/MinkContext.php
+++ b/src/Behat/MinkExtension/Context/MinkContext.php
@@ -238,22 +238,34 @@ class MinkContext extends RawMinkContext implements TranslatableContext

/**
* Checks, that page contains specified text.
+ * @param string $text the text to check
+ * @param integer $timeout in milliseconds
*
* @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)"$/
*/
- public function assertPageContainsText($text)
+ public function assertPageContainsText($text, $timeout = 10000)
{
- $this->assertSession()->pageTextContains($this->fixStepArgument($text));
+ $element = $this->findAfterAjax($this->getSession()->getPage(), $text, $timeout);
+ if (!$element) {
+ $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
+ throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
+ }
}

/**
- * Checks, that page doesn't contain specified text.
+ * Checks, that page does not contain specified text.
+ * @param string $text the text to check
+ * @param integer $timeout in milliseconds
*
* @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)"$/
*/
- public function assertPageNotContainsText($text)
+ public function assertPageNotContainsText($text, $timeout = 10000)
{
- $this->assertSession()->pageTextNotContains($this->fixStepArgument($text));
+ $element = $this->findAfterAjax($this->getSession()->getPage(), $text, $timeout);
+ if ($element && $element->isVisible()) {
+ $message = sprintf('The text "%s" was found in the text of the current page although it should not.', $text);
+ throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
+ }
}

/**
@@ -297,16 +309,6 @@ class MinkContext extends RawMinkContext implements TranslatableContext
}

/**
- * Checks, that element with specified CSS contains specified text.
- *
- * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
- */
- public function assertElementContainsText($element, $text)
- {
- $this->assertSession()->elementTextContains('css', $element, $this->fixStepArgument($text));
- }
-
- /**
* Checks, that element with specified CSS doesn't contain specified text.
*
* @Then /^(?:|I )should not see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
@@ -483,4 +485,66 @@ class MinkContext extends RawMinkContext implements TranslatableContext
{
return str_replace('\\"', '"', $argument);
}
+
+ /**
+ * Checks, that element with specified CSS contains specified text.
+ * @param string $element the element where search
+ * @param string $text the text to check
+ * @param integer $timeout in milliseconds
+ *
+ * @Then /^(?:|I )should see "(?P<text>(?:[^"]|\\")*)" in the "(?P<element>[^"]*)" element$/
+ *
+ * @return null|boolean
+ */
+ public function assertElementContainsText($element, $text, $timeout = 5000)
+ {
+ if ($timeout <= 0) {
+ $message = sprintf('The element "%s" was not found in the page.', $element);
+ throw new \Behat\Mink\Exception\ResponseTextException($message, $this->getSession());
+ }
+ $selectorType = 'css';
+
+ $node = $this->getSession()->getPage()->find($selectorType, $element);
+
+ if (is_object($node)) {
+ $item = $this->findAfterAjax($node, $text);
+ if (!$item) {
+ $this->assertElementContainsText($element, $text, 0);
+ }
+ } else {
+ $this->getSession()->wait(100);
+
+ return $this->assertElementContainsText($element, $text, $timeout - 100);
+ }
+ }
+
+ /**
+ * Try to select element, return null after 15 sec
+ * @param string $element the element where search
+ * @param string $value the value to check
+ * @param integer $timeout in milliseconds
+ *
+ * @return boolean
+ */
+ public function findAfterAjax($element, $value, $timeout = 5000)
+ {
+ if ($timeout <= 0) {
+ return false;
+ }
+
+ // Hack to be able to get an element case insensitively...
+ $alphabetLower = '"'.implode('', range('a', 'z')).'"';
+ $alphabetUpper = '"'.implode('', range('A', 'Z')).'"';
+
+ $item = $element->find('xpath', '/descendant-or-self::*[contains(translate(text(), '.$alphabetUpper.', '.$alphabetLower.'), translate("'.$value.'", '.$alphabetUpper.', '.$alphabetLower.'))]');
+
+ if ($item) {
+ return $item;
+ } else {
+ $this->getSession()->wait(100);
+
+ return $this->findAfterAjax($element, $value, $timeout - 100);
+ }
+
+ }
}

最佳答案

出于好奇,我没有发送 PR,但最终实现了一个 Trait https://github.com/Troopers/BehatContexts/blob/master/src/Troopers/BehatContexts/Context/SpinMinkContextTrait.php )以实现行为文档建议的内容:

正如 Behat 所说:

通常,尤其是在使用 Mink 测试 Web 应用程序时,您会发现 Behat 的运行速度比您的 Web 应用程序能够跟上的速度要快 - 它会在页面有机会加载之前尝试单击链接或执行操作,因此导致结果在失败的测试中,否则就会通过。为了缓解这个问题,我们可以使用旋转函数来重复尝试和操作或测试条件,直到它起作用。本文着眼于将其应用于 Mink,但该技术适用于使用 Behat 的任何测试。阅读文档

关于ajax - 这些 MinkExtension 基础上下文适应与 ajax 请求相关吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com