Skip to content

Commit

Permalink
introduce setValueChangeEvent option (#718)
Browse files Browse the repository at this point in the history
* introduce setValueChangeEvent option
* add tests for setValueChangeEvent option
  • Loading branch information
MikeShysh authored and rosolko committed Apr 25, 2018
1 parent 688db05 commit 7715163
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/main/java/com/codeborne/selenide/Configuration.java
Expand Up @@ -301,6 +301,18 @@ private static String getJenkinsReportsUrl() {
*/
public static boolean versatileSetValue = Boolean.parseBoolean(System.getProperty("selenide.versatileSetValue", "false"));

/**
* If set to true, 'setValue' and 'val' methods of SelenideElement trigger changeEvent after main manipulations.
*
* Firing change event is not natural and could lead to unpredictable results. Browser fires this event automatically
* according to web driver actions. Recommended behaviour is to disable this option.
* Make its true by default for backward compatibility.
*
* Can be configured either programmatically or by system property "-Dselenide.setValueChangeEvent=true".
* Default value: true
*/
public static boolean setValueChangeEvent = Boolean.parseBoolean(System.getProperty("selenide.setValueChangeEvent", "true"));

/**
* Choose how Selenide should retrieve web elements: using default CSS or Sizzle (CSS3)
*/
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/com/codeborne/selenide/commands/Append.java
Expand Up @@ -5,14 +5,17 @@
import com.codeborne.selenide.impl.WebElementSource;
import org.openqa.selenium.WebElement;

import static com.codeborne.selenide.Configuration.setValueChangeEvent;
import static com.codeborne.selenide.impl.Events.events;

public class Append implements Command<WebElement> {
@Override
public WebElement execute(SelenideElement proxy, WebElementSource locator, Object[] args) {
WebElement input = locator.getWebElement();
input.sendKeys((String) args[0]);
events.fireChangeEvent(input);
if (setValueChangeEvent) {
events.fireChangeEvent(input);
}
return proxy;
}
}
5 changes: 4 additions & 1 deletion src/main/java/com/codeborne/selenide/commands/SetValue.java
Expand Up @@ -8,6 +8,7 @@
import org.openqa.selenium.WebElement;

import static com.codeborne.selenide.Configuration.fastSetValue;
import static com.codeborne.selenide.Configuration.setValueChangeEvent;
import static com.codeborne.selenide.Selenide.executeJavaScript;
import static com.codeborne.selenide.impl.Events.events;

Expand Down Expand Up @@ -55,7 +56,9 @@ private void setValueForTextInput(WebElement element, String text) {
} else {
element.clear();
element.sendKeys(text);
events.fireChangeEvent(element);
if (setValueChangeEvent) {
events.fireChangeEvent(element);
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/test/java/integration/PageWithJQuery.java
@@ -1,8 +1,10 @@
package integration;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import com.codeborne.selenide.Configuration;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
Expand All @@ -24,9 +26,26 @@ public void setValueTriggersOnChangeEvent() {
$("h2").shouldHave(text("john bon-jovi"));
}

@Test
public void setValueDoesNotTriggerOnChangeEvent() {
Configuration.setValueChangeEvent = false;
$("#username").setValue("john");
$("h2").shouldNotHave(text("john"));

$("#username").append(" ");
$("#username").append("bon-jovi");

$("h2").shouldNotHave(text("john bon-jovi"));
}

@Test
public void selectByXpath() {
$(By.xpath("html/body/div[2]/form[1]/fieldset[1]//input[@name='username']")).val("Underwood");
$(By.xpath("/html//h2[1]")).shouldHave(text("Underwood"));
}

@After
public void tearDown() {
Configuration.setValueChangeEvent = true;
}
}
28 changes: 27 additions & 1 deletion src/test/java/integration/PageWithoutJQuery.java
@@ -1,19 +1,25 @@
package integration;

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import com.codeborne.selenide.Configuration;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.WebDriverRunner.isHtmlUnit;
import static org.junit.Assume.assumeFalse;

public class PageWithoutJQuery extends IntegrationTest {
@Before
public void openTestPageWithJQuery() {
public void openTestPageWithOutJQuery() {
openFile("page_with_selects_without_jquery.html");
}

@Test
public void setValueTriggersOnChangeEvent() {
assumeFalse(isHtmlUnit());

$("#username").setValue("john");
$("#username-mirror").shouldHave(text("john"));

Expand All @@ -22,4 +28,24 @@ public void setValueTriggersOnChangeEvent() {

$("#username-mirror").shouldHave(text("john bon-jovi"));
}

@Test
public void setValueDoesNotTriggerOnChangeEvent() {
assumeFalse(isHtmlUnit());

Configuration.setValueChangeEvent = false;

$("#username").setValue("john");
$("#username-mirror").shouldHave(text("_"));

$("#username").append(" ");
$("#username").append("bon-jovi");

$("#username-mirror").shouldHave(text("_"));
}

@After
public void tearDown() {
Configuration.setValueChangeEvent = true;
}
}

0 comments on commit 7715163

Please sign in to comment.