From e3c59de91a9c797cb5a909436f73ab8243b520ea Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Fri, 15 Jun 2018 11:59:27 +0200 Subject: [PATCH 1/8] Prevent firing the "change" event twice --- src/Selenium2Driver.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index cabdb606..666f45da 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -677,13 +677,10 @@ public function setValue($xpath, $value) if (in_array($elementName, array('input', 'textarea'))) { $existingValueLength = strlen($element->attribute('value')); - // Add the TAB key to ensure we unfocus the field as browsers are triggering the change event only - // after leaving the field. $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value; } $element->postValue(array('value' => array($value))); - $this->trigger($xpath, 'change'); } /** From 03ff29b4a70f8ae1269d055db3835b4158bc6f01 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Fri, 15 Jun 2018 13:20:29 +0200 Subject: [PATCH 2/8] Test with chrome --- bin/run-selenium-remote.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/run-selenium-remote.sh b/bin/run-selenium-remote.sh index 9d2205d1..0ed95de5 100644 --- a/bin/run-selenium-remote.sh +++ b/bin/run-selenium-remote.sh @@ -2,6 +2,6 @@ set -e echo ' Downloading selenium' -docker pull selenium/standalone-firefox:2.53.1 +docker pull selenium/standalone-chrome:2.53.1 echo ' Running selenium' -docker run -d -p 4444:4444 --network=host selenium/standalone-firefox:2.53.1 +docker run -d -p 4444:4444 --network=host selenium/standalone-chrome:2.53.1 From 626d7d6e0e3904bc790ba56d1d0f7f0e7ed65971 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Fri, 15 Jun 2018 13:37:39 +0200 Subject: [PATCH 3/8] changing testing browser to chrome --- phpunit.xml.dist | 1 + 1 file changed, 1 insertion(+) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 58a01533..ca45f9d0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,6 +13,7 @@ + From 0b33f770559957b4eef9920d83e9af5ca26eea57 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Wed, 20 Jun 2018 09:57:08 +0200 Subject: [PATCH 4/8] Revert "changing testing browser to chrome" This reverts commit 626d7d6 --- phpunit.xml.dist | 1 - 1 file changed, 1 deletion(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index ca45f9d0..58a01533 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -13,7 +13,6 @@ - From 3a051a573246c7569d5f1a4f146e09d2b293e249 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Wed, 20 Jun 2018 09:58:09 +0200 Subject: [PATCH 5/8] Revert "Test with chrome" This reverts commit 03ff29b --- bin/run-selenium-remote.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/run-selenium-remote.sh b/bin/run-selenium-remote.sh index 0ed95de5..9d2205d1 100644 --- a/bin/run-selenium-remote.sh +++ b/bin/run-selenium-remote.sh @@ -2,6 +2,6 @@ set -e echo ' Downloading selenium' -docker pull selenium/standalone-chrome:2.53.1 +docker pull selenium/standalone-firefox:2.53.1 echo ' Running selenium' -docker run -d -p 4444:4444 --network=host selenium/standalone-chrome:2.53.1 +docker run -d -p 4444:4444 --network=host selenium/standalone-firefox:2.53.1 From b2d26287292764850e9df9905afcef5b2fa8fc17 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Wed, 20 Jun 2018 09:58:27 +0200 Subject: [PATCH 6/8] Revert "Prevent firing the "change" event twice" This reverts commit e3c59de --- src/Selenium2Driver.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index 666f45da..cabdb606 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -677,10 +677,13 @@ public function setValue($xpath, $value) if (in_array($elementName, array('input', 'textarea'))) { $existingValueLength = strlen($element->attribute('value')); + // Add the TAB key to ensure we unfocus the field as browsers are triggering the change event only + // after leaving the field. $value = str_repeat(Key::BACKSPACE . Key::DELETE, $existingValueLength) . $value; } $element->postValue(array('value' => array($value))); + $this->trigger($xpath, 'change'); } /** From 982c1a183cd93398be1749530a28b18cf3769d2b Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Wed, 20 Jun 2018 10:13:49 +0200 Subject: [PATCH 7/8] Trigger the change event for an element by calling the blur method on the currently focused element instead by triggering the change event by xpath which might lead to multiple change events for the same element --- src/Selenium2Driver.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index cabdb606..e8ed412b 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -683,7 +683,10 @@ public function setValue($xpath, $value) } $element->postValue(array('value' => array($value))); - $this->trigger($xpath, 'change'); + // Trigger the change event only if the field still has focus. + // @todo we still have to ensure that the focus is still on the element + // we've set the value to. + $this->executeScript("document.activeElement && document.activeElement.blur()"); } /** From 68732285445256694667d35fa5af512a8328c0b5 Mon Sep 17 00:00:00 2001 From: Hristo Chonov Date: Wed, 20 Jun 2018 19:09:05 +0200 Subject: [PATCH 8/8] Remove the focus of the element on which we've set the value, but only in case that element still has the focus. This will trigger the change event. --- src/Selenium2Driver.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Selenium2Driver.php b/src/Selenium2Driver.php index e8ed412b..1e7b7a84 100755 --- a/src/Selenium2Driver.php +++ b/src/Selenium2Driver.php @@ -683,10 +683,20 @@ public function setValue($xpath, $value) } $element->postValue(array('value' => array($value))); - // Trigger the change event only if the field still has focus. - // @todo we still have to ensure that the focus is still on the element - // we've set the value to. - $this->executeScript("document.activeElement && document.activeElement.blur()"); + // Remove the focus from the element if the field still has focus in + // order to trigger the change event. By doing this instead of simply + // triggering the change event for the given xpath we ensure that the + // change event will not be triggered twice for the same element if it + // has lost focus in the meanwhile. If the element has lost focus + // already then there is nothing to do as this will already have caused + // the triggering of the change event for that element. + $script = <<executeJsOnElement($element, $script); } /**