1.

How to get a typed text from a text box?

Answer»

Additional information about an HTML is stored in Attributes and comes in pair name =”value”. In <div class=”my-class”></div> example div is tag and class is attribute with value ‘my-class’. The property PRESENTS an attribute in the HTML DOM tree. In the above example, the attribute would have a property named className and value my-class. HTML defines attributes and DOM defines properties. Attributes may have 1:1 mapping with property or may not have. The ID is an example of 1:1 mapping. Attributes are stored in HTML text document and properties are in the HTML DOM tree. Attributes carry initial default value and the attribute does not change whereas HTML properties can change.

To verify test cases, during automation we need to retrieve values of attributes or properties of web elements.

In case of a bus ticket booking application,  Colour of the available seat colour is green and BOOKED seat colour is red. To book the ticket first we need to check whether a seat is available or not. And to test this, we need to fetch colour attribute value using the script and based on output value we need to perform further operations.

To fetch attribute value, selenium WebDriver has predefined getAttribute(value) method which returns the value of the attribute of the web element as a string. If the attribute exists, getAttribute method returns value of the property with the given name, and if it does not exist then returns value of the attribute with the given name.

Please NOTE, when the given name is “class”, then “className” property is returned and when given name is “readOnly”, the “readOnly” property is returned.

To use getAttribute() method on a specific element, first, locate the web element and then call getAttribute() method on it by specifying the attribute.

Below is the code explaining getAttribute() method –

WebElement we = driver.findElement(By.class("class Value")); String value = we.getAttribute("id");

IMPORT org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class GetAttributes { public WebDriver driver; private By bySrchButton = By.name("btnK"); @BeforeClass public VOID setUp() { driver = new FirefoxDriver(); driver.get("http://www.google.com"); } @Test public void getAttribute_ButtonName() { WebElement googleSrchBtn = driver.findElement(bySrchButton); System.out.println("Name of the button is:- " +googleSrchBtn.getAttribute("name")); } @Test public void getAttribute_Id() { WebElement googleSrchBtn = driver.findElement(bySrchButton); System.out.println("Id of the button is:- "+ googleSrchBtn.getAttribute("id")); } @Test public void getAttribute_class() { WebElement googleSrchBtn = driver.findElement(bySrchButton); System.out.println("Class of the button is:- "+ googleSrchBtn.getAttribute("class")); } @Test public void getAttribute_InvalidAttribute() { WebElement googleSrchBtn = driver.findElement(bySrchButton); //Negative test as 'Status' attribute does not exist,will return null value System.out.println("Invalid Attribute status of the button is:- "+ googleSrchBtn.getAttribute("status")); } @Test public void getAttribute_ButtonLabel() { WebElement googleSrchBtn = driver.findElement(bySrchButton); System.out.println("Label of the button is:- "+ googleSrchBtn.getAttribute("aria-label")); } @AfterClass public void tearDown() { driver.quit(); } }


Discussion

No Comment Found