Skip to content

Instantly share code, notes, and snippets.

@alb-i986
Created June 10, 2020 21:21
Show Gist options
  • Save alb-i986/6d3ee27fabb839fa9e46742a0e1aff09 to your computer and use it in GitHub Desktop.
Save alb-i986/6d3ee27fabb839fa9e46742a0e1aff09 to your computer and use it in GitHub Desktop.
A test showing an issue with Selenium's Keys.COMMAND.
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
public class SeleniumCommandIssue {
private WebDriver driver;
@After
public void after() {
driver.quit();
}
@Test
public void workingWithFF() throws Exception {
driver = new FirefoxDriver();
driver.get("http://google.it");
driver.findElement(By.name("q")).sendKeys("asd");
Thread.sleep(1000);
driver.findElement(By.name("q")).sendKeys(Keys.chord(Keys.COMMAND, "a"));
Thread.sleep(1000);
driver.findElement(By.name("q")).sendKeys("dsa");
assertThat(driver.findElement(By.name("q")).getAttribute("value"),
equalTo("dsa"));
}
@Test
public void failingWithChrome1() throws Exception {
driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
driver.get("http://google.it");
driver.findElement(By.name("q")).sendKeys("asd");
Thread.sleep(1000);
driver.findElement(By.name("q")).sendKeys(Keys.chord(Keys.COMMAND + "a"));
Thread.sleep(1000);
driver.findElement(By.name("q")).sendKeys("dsa");
assertThat(driver.findElement(By.name("q")).getAttribute("value"),
equalTo("dsa"));
}
@Test
public void failingWithChrome2() throws Exception {
driver = new ChromeDriver(new ChromeOptions().setHeadless(true));
driver.get("http://google.it");
driver.findElement(By.name("q")).sendKeys("asd");
Thread.sleep(1000);
new Actions(driver)
.keyDown(Keys.COMMAND)
.sendKeys("a")
.keyUp(Keys.COMMAND)
.perform();
Thread.sleep(1000);
driver.findElement(By.name("q")).sendKeys("dsa");
assertThat(driver.findElement(By.name("q")).getAttribute("value"),
equalTo("dsa"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment