Skip to content

Commit

Permalink
Workaround E2E keyboard shortcuts on Windows
Browse files Browse the repository at this point in the history
I swapped Ctrl and CapsLock on Windows using PowerToys, which makes java.awt.Robot unable to send CTRL properly. Therefore, change to Alt.
  • Loading branch information
yorkxin committed Jun 29, 2024
1 parent 88a8a5e commit 44bd495
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,9 @@ protected void preGrantAllPermissionsInChrome() throws AWTException {
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_SPACE);
}

(new Robot()).delay(200);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public void configureKeyboardShortcuts() throws InterruptedException, AWTExcepti

public void configureKeyboardShortcutsInChrome() throws InterruptedException, AWTException {
openChromeKeyboardShortcutsPage();
setShortcutKeyInChrome("current tab: [title](url)", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "q");
setShortcutKeyInChrome("Copy Selection as Markdown", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "p");
setShortcutKeyInChrome("current tab: [title](url)", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "q");
setShortcutKeyInChrome("Copy Selection as Markdown", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "p");
}

public void configureKeyboardShortcutsInFirefox() throws InterruptedException, AWTException {
Expand All @@ -40,7 +40,7 @@ public void configureKeyboardShortcutsInFirefox() throws InterruptedException, A
@Test
public void currentTabLink() throws AWTException, IOException, UnsupportedFlavorException {
driver.get("http://localhost:5566/qa.html");
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_Q);
runShortcutKeys(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_SHIFT}, KeyEvent.VK_Q);

String expected = "[[QA] \\*\\*Hello\\*\\* \\_World\\_](http://localhost:5566/qa.html)";
assertEquals(clipboard.getData(DataFlavor.stringFlavor),expected);
Expand All @@ -51,7 +51,7 @@ public void copySelectionAsMarkdown () throws AWTException, IOException, Unsuppo
driver.get("http://localhost:5566/selection.html");
selectAll();

runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_P);
runShortcutKeys(new int[]{KeyEvent.VK_ALT, KeyEvent.VK_SHIFT}, KeyEvent.VK_P);
Thread.sleep(500);

String expected = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

public class BaseTest extends org.yorkxin.copyasmarkdown.e2e.keyboardshortcut.BaseTest {
public static CommandDescriptor[] allCommandDescriptors = new CommandDescriptor[]{
new CommandDescriptor("all tabs: - [title](url)", "all-tabs-link-as-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "w"),
new CommandDescriptor("all tabs: - [ ] [title](url)", "all-tabs-link-as-task-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "e"),
new CommandDescriptor("all tabs: - title", "all-tabs-title-as-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "r"),
new CommandDescriptor("all tabs: - url", "all-tabs-url-as-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "t"),
new CommandDescriptor("selected tabs: - [title](url)", "highlighted-tabs-link-as-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "y"),
new CommandDescriptor("selected tabs: - [ ] [title](url)", "highlighted-tabs-link-as-task-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "u"),
new CommandDescriptor("selected tabs: - title", "highlighted-tabs-title-as-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "i"),
new CommandDescriptor("selected tabs: - url", "highlighted-tabs-url-as-list", new CharSequence[]{Keys.CONTROL, Keys.SHIFT}, "o"),
new CommandDescriptor("all tabs: - [title](url)", "all-tabs-link-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "w"),
new CommandDescriptor("all tabs: - [ ] [title](url)", "all-tabs-link-as-task-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "e"),
new CommandDescriptor("all tabs: - title", "all-tabs-title-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "r"),
new CommandDescriptor("all tabs: - url", "all-tabs-url-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "t"),
new CommandDescriptor("selected tabs: - [title](url)", "highlighted-tabs-link-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "y"),
new CommandDescriptor("selected tabs: - [ ] [title](url)", "highlighted-tabs-link-as-task-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "u"),
new CommandDescriptor("selected tabs: - title", "highlighted-tabs-title-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "i"),
new CommandDescriptor("selected tabs: - url", "highlighted-tabs-url-as-list", new CharSequence[]{Keys.ALT, Keys.SHIFT}, "o"),
};

public static CommandDescriptor getCommandDescriptor(String command) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.openqa.selenium.By;
import org.testng.annotations.*;
import org.yorkxin.copyasmarkdown.e2e.keyboardshortcut.CommandDescriptor;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
Expand All @@ -28,11 +29,10 @@ public void teardown() {
@Test(dataProvider = "listStyles")
public void allTabsLink(String listStyle) throws AWTException, IOException, UnsupportedFlavorException, InterruptedException {
setListStyle(listStyle);
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_W;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-link-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{0} [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -48,11 +48,10 @@ public void allTabsLink(String listStyle) throws AWTException, IOException, Unsu

@Test
public void allTabsTaskList() throws AWTException, IOException, UnsupportedFlavorException {
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_E;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-link-as-task-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = """
- [ ] [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -68,11 +67,10 @@ public void allTabsTaskList() throws AWTException, IOException, UnsupportedFlavo
@Test(dataProvider = "listStyles")
public void allTabsTitle(String listStyle) throws AWTException, IOException, UnsupportedFlavorException {
setListStyle(listStyle);
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_R;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-title-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{0} Page 0 - Copy as Markdown
Expand All @@ -89,11 +87,10 @@ public void allTabsTitle(String listStyle) throws AWTException, IOException, Uns
@Test(dataProvider = "listStyles")
public void allTabsUrl(String listStyle) throws AWTException, IOException, UnsupportedFlavorException {
setListStyle(listStyle);
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_T;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-url-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{0} http://localhost:5566/0.html
Expand All @@ -110,11 +107,10 @@ public void allTabsUrl(String listStyle) throws AWTException, IOException, Unsup
@Test(dataProvider = "listStyles")
public void highlightedTabsLink(String listStyle) throws AWTException, IOException, UnsupportedFlavorException, InterruptedException {
setListStyle(listStyle);
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_Y;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-link-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{0} [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -127,11 +123,10 @@ public void highlightedTabsLink(String listStyle) throws AWTException, IOExcepti

@Test
public void highlightedTabsTaskList() throws AWTException, IOException, UnsupportedFlavorException {
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_U;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-link-as-task-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = """
- [ ] [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -144,11 +139,10 @@ public void highlightedTabsTaskList() throws AWTException, IOException, Unsuppor
@Test(dataProvider = "listStyles")
public void highlightedTabsTitle(String listStyle) throws AWTException, IOException, UnsupportedFlavorException {
setListStyle(listStyle);
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_I;
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-title-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{0} Page 0 - Copy as Markdown
Expand All @@ -162,11 +156,10 @@ public void highlightedTabsTitle(String listStyle) throws AWTException, IOExcept
@Test(dataProvider = "listStyles")
public void highlightedTabsUrl(String listStyle) throws AWTException, IOException, UnsupportedFlavorException {
setListStyle(listStyle);
int[] modifiers = new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT};
int key = KeyEvent.VK_O;
openDemoTabs(false);
openDemoTabs(false);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(modifiers, key);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-url-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{0} http://localhost:5566/0.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.openqa.selenium.By;
import org.testng.annotations.*;
import org.yorkxin.copyasmarkdown.e2e.keyboardshortcut.CommandDescriptor;

import java.awt.*;
import java.awt.datatransfer.DataFlavor;
Expand Down Expand Up @@ -32,7 +33,8 @@ public void allTabsLink(String indentation, String listStyle) throws AWTExceptio
setListStyle(listStyle);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_W);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-link-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{1} [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -53,7 +55,8 @@ public void allTabsTaskList(String indentation) throws AWTException, IOException
setTabGroupIndentation(indentation);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_E);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-link-as-task-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
- [ ] [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -75,7 +78,8 @@ public void allTabsTitle(String indentation, String listStyle) throws AWTExcepti
setListStyle(listStyle);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_R);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-title-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{1} Page 0 - Copy as Markdown
Expand All @@ -97,7 +101,8 @@ public void allTabsUrl(String indentation, String listStyle) throws AWTException
setListStyle(listStyle);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_T);
CommandDescriptor cmd = getCommandDescriptor("all-tabs-url-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{1} http://localhost:5566/0.html
Expand All @@ -119,7 +124,8 @@ public void highlightedTabsLink(String indentation, String listStyle) throws AWT
setListStyle(listStyle);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_Y);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-link-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{1} [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -137,7 +143,8 @@ public void highlightedTabsTaskList(String indentation) throws AWTException, IOE
setTabGroupIndentation(indentation);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_U);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-link-as-task-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
- [ ] [Page 0 - Copy as Markdown](http://localhost:5566/0.html)
Expand All @@ -156,7 +163,8 @@ public void highlightedTabsTitle(String indentation, String listStyle) throws AW
setListStyle(listStyle);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_I);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-title-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{1} Page 0 - Copy as Markdown
Expand All @@ -175,7 +183,8 @@ public void highlightedTabsUrl(String indentation, String listStyle) throws AWTE
setListStyle(listStyle);
openDemoTabs(true);
driver.findElement(By.id("switch-to-demo")).click();
runShortcutKeys(new int[]{KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT}, KeyEvent.VK_O);
CommandDescriptor cmd = getCommandDescriptor("highlighted-tabs-url-as-list");
runShortcutKeys(cmd.getRobotModifiers(), cmd.getRobotKey());

String expected = MessageFormat.format("""
{1} http://localhost:5566/0.html
Expand Down

0 comments on commit 44bd495

Please sign in to comment.