Check maxlength and readonly property of inputbox with selenium webdriver

selenium

To check the maxlength of inputbox and to verify the field is readonly or not we can use the following code –

package com.webdriver_13;

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

public class MyVerification_02 {

	public WebDriver driver;
	private String baseUrl;
	
	public static void main(String[] args) {
		MyVerification_02 mv = new MyVerification_02();
		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);
			 
			 // retrieve max length 
			 
			 String flength = driver.findElement(By.xpath("//input[contains(@id,'ts_first_name')]")).getAttribute("maxlength");
			 System.out.println("First name's max length is - " +flength);
			 
			 String llength = driver.findElement(By.xpath("//input[contains(@id,'ts_last_name')]")).getAttribute("maxlength");
			 System.out.println("Last name's max length is - " +llength);
	
			 String addlength = driver.findElement(By.xpath("//textarea[contains(@id,'ts_address')]")).getAttribute("maxlength");
			 System.out.println("Address max length is - " +addlength);
	
			 // Read only ?
			 String add = driver.findElement(By.xpath("//textarea[contains(@id,'ts_address')]")).getAttribute("readonly");
			 if(add == null)
				 System.out.println("Address has NO such property");
			 else if(add.contentEquals("true"))
					System.out.println("Address has readonly Property");
			 else
				 System.out.println("Address is -" + add);
			
			 
			 String terms = driver.findElement(By.xpath("//textarea[contains(@id,'ts_terms')]")).getAttribute("readonly");
			 if(terms == null)
				 System.out.println("Terms has NO such property");
			 else if(terms.contentEquals("true"))
					System.out.println("Terms has readonly Property");
			 else
				 System.out.println("Terms is -" + terms);
	
			 driver.quit();
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}

and the output should be –

Opening http://demo.tanmaysarkar.com/sample_03.html
First name’s max length is – 15
Last name’s max length is – 15
Address max length is – 225
Address has NO such property
Terms has readonly Property

Related posts:

Leave a Reply

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