Html ACCESSKEY and selenium webdriver

selenium

To check accesskey of html webpage we can use the following code –

package com.tanmaysarkar.demo;

import java.awt.Robot;
import java.awt.event.KeyEvent;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyVerification_04 {

	public WebDriver driver;
	private String baseUrl;
	
	public static void main(String[] args) {
		MyVerification_04 mv = new MyVerification_04();
		mv.launch_test();
	}
	public void launch_test(){
		try{
			 driver = new FirefoxDriver();
			 baseUrl = "http://demo.tanmaysarkar.com/sample_03.html";
			 driver.get(baseUrl);
			 driver.manage().window().maximize();
			 System.out.println("Opening " + baseUrl);
			 Thread.sleep(2000);
			 
			 // input box ACCESSKEY check
			 Robot robot = new Robot();
			 robot.keyPress(KeyEvent.VK_ALT);
			 robot.keyPress(KeyEvent.VK_SHIFT);
			 robot.keyPress(KeyEvent.VK_L);
			 Thread.sleep(2000);
			 
			 robot.keyRelease(KeyEvent.VK_ALT);
			 robot.keyRelease(KeyEvent.VK_SHIFT);
			 robot.keyRelease(KeyEvent.VK_L);
			 Thread.sleep(2000);
			 
			 robot.keyPress(KeyEvent.VK_1);
			 robot.keyRelease(KeyEvent.VK_1);
			 Thread.sleep(2000);
			 
			 if(driver.findElement(By.xpath("//input[contains(@id,'ts_last_name')]")).getAttribute("value").equals("1"))
				 System.out.println("Last name is selected");
			 else
				 System.out.println("Last name is not selected");
			 
			 // Is check box checked ?
			 if(driver.findElement(By.xpath("//input[contains(@name,'ts_checkbox1')]")).isSelected())
			 {
				 System.out.println("Cricket is checked");
			 }
			 else
			 {
				 System.out.println("Cricket is NOT checked");
			 }
			 
			 // Checkbox box ACCESSKEY check			 
			 robot.keyPress(KeyEvent.VK_ALT);
			 robot.keyPress(KeyEvent.VK_SHIFT);
			 robot.keyPress(KeyEvent.VK_C);
			 Thread.sleep(2000);
			 
			 robot.keyRelease(KeyEvent.VK_ALT);
			 robot.keyRelease(KeyEvent.VK_SHIFT);
			 robot.keyRelease(KeyEvent.VK_C);
			 Thread.sleep(2000);
			 
			 // Is check box checked ?
			 if(driver.findElement(By.xpath("//input[contains(@name,'ts_checkbox1')]")).isSelected())
			 {
				 System.out.println("Cricket is checked");
			 }
			 else
			 {
				 System.out.println("Cricket is NOT checked");
			 }
			 
			 Thread.sleep(5000);
			 driver.quit();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

and the output should be –

Opening http://demo.tanmaysarkar.com/sample_03.html
Last name is selected
Cricket is NOT checked
Cricket is checked

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *