1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package SeleniumSessions; 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.interactions.Actions; import org.testng.Assert; import org.testng.annotations.Test; /** * * @author Sarang * Created on 19/04/2019 * */ public class KeyPressEventInSelenium { static WebDriver driver; @Test public void keyPressEventSpace() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://the-internet.herokuapp.com/key_presses"); Actions action = new Actions(driver); action.sendKeys(Keys.SPACE).build().perform();; String text = driver.findElement(By.id("result")).getText(); System.out.println(text); Assert.assertEquals(text, "You entered: SPACE"); Thread.sleep(2000); driver.quit(); } @Test public void keyPressEventEnter() throws InterruptedException { System.setProperty("webdriver.chrome.driver", "chromedriver.exe"); driver = new ChromeDriver(); driver.get("http://the-internet.herokuapp.com/key_presses"); Actions action = new Actions(driver); action.sendKeys(Keys.ENTER).build().perform();; String text = driver.findElement(By.id("result")).getText(); System.out.println(text); Assert.assertEquals(text, "You entered: ENTER"); Thread.sleep(2000); driver.quit(); } } |