Skip to content

Commit

Permalink
Table.selectRows() doesn't work on MacOS #23
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniil Tsarev committed Jan 27, 2020
1 parent 620c46c commit 66b42a2
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.haulmont.masquerade.components.DataGrid;
import com.haulmont.masquerade.conditions.SpecificCondition;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
Expand All @@ -27,6 +28,7 @@
import static com.haulmont.masquerade.Conditions.VISIBLE;
import static com.haulmont.masquerade.Selectors.byChain;
import static com.haulmont.masquerade.Selectors.byCubaId;
import static com.haulmont.masquerade.components.impl.TableImpl.MAC_OS_PLATFORM;
import static com.haulmont.masquerade.sys.VaadinClassNames.selectedClass;
import static com.haulmont.masquerade.sys.matchers.ConditionCases.componentApply;
import static com.haulmont.masquerade.sys.matchers.InstanceOfCases.hasType;
Expand Down Expand Up @@ -270,9 +272,11 @@ public SelenideElement deselectRow(By rowBy) {
WebDriver webDriver = WebDriverRunner.getWebDriver();
Actions action = new Actions(webDriver);

action.keyDown(Keys.CONTROL)
Keys controlKey = getControlKey();

action.keyDown(controlKey)
.click(row.getWrappedElement())
.keyUp(Keys.CONTROL)
.keyUp(controlKey)
.build()
.perform();

Expand All @@ -293,9 +297,11 @@ public ElementsCollection selectRows(By rowBy) {
for (SelenideElement row : rows) {
row.shouldNotHave(selectedClass);

action.keyDown(Keys.CONTROL)
Keys controlKey = getControlKey();

action.keyDown(controlKey)
.click(row.getWrappedElement())
.keyUp(Keys.CONTROL)
.keyUp(controlKey)
.build()
.perform();
}
Expand Down Expand Up @@ -415,4 +421,28 @@ protected int getSortClickCount(DataGrid.SortDirection current, DataGrid.SortDir

return 2;
}

/**
* @return control key depending on operating system
*/
protected Keys getControlKey() {
Keys controlKey = Keys.CONTROL;

WebDriver webDriver = WebDriverRunner.getWebDriver();
if (webDriver instanceof JavascriptExecutor) {
// check if working on MacOS
Object result = ((JavascriptExecutor) webDriver)
.executeScript("return window.navigator.platform");

if (result instanceof String) {
String platform = (String) result;

if (MAC_OS_PLATFORM.equals(platform)) {
controlKey = Keys.COMMAND;
}
}
}

return controlKey;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.haulmont.masquerade.conditions.SpecificCondition;
import com.haulmont.masquerade.sys.TagNames;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
Expand All @@ -50,6 +51,8 @@

public class TableImpl extends AbstractComponent<Table> implements Table {

public static final String MAC_OS_PLATFORM = "MacIntel";

public TableImpl(By by) {
super(by);
}
Expand Down Expand Up @@ -286,9 +289,11 @@ public SelenideElement deselectRow(By rowBy) {
WebDriver webDriver = WebDriverRunner.getWebDriver();
Actions action = new Actions(webDriver);

action.keyDown(Keys.CONTROL)
Keys controlKey = getControlKey();

action.keyDown(controlKey)
.click(row.getWrappedElement())
.keyUp(Keys.CONTROL)
.keyUp(controlKey)
.build()
.perform();

Expand All @@ -309,9 +314,11 @@ public ElementsCollection selectRows(By rowBy) {
for (SelenideElement row : rows) {
row.shouldNotHave(selectedClass);

action.keyDown(Keys.CONTROL)
Keys controlKey = getControlKey();

action.keyDown(controlKey)
.click(row.getWrappedElement())
.keyUp(Keys.CONTROL)
.keyUp(controlKey)
.build()
.perform();
}
Expand Down Expand Up @@ -412,4 +419,28 @@ protected int getSortClickCount(SortDirection current, SortDirection target) {

return 2;
}

/**
* @return control key depending on operating system
*/
protected Keys getControlKey() {
Keys controlKey = Keys.CONTROL;

WebDriver webDriver = WebDriverRunner.getWebDriver();
if (webDriver instanceof JavascriptExecutor) {
// check if working on MacOS
Object result = ((JavascriptExecutor) webDriver)
.executeScript("return window.navigator.platform");

if (result instanceof String) {
String platform = (String) result;

if (MAC_OS_PLATFORM.equals(platform)) {
controlKey = Keys.COMMAND;
}
}
}

return controlKey;
}
}

0 comments on commit 66b42a2

Please sign in to comment.