getAttribute using Selenium

package webdriverTestNG;

import org.testng.annotations.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;

public class GetAttribute {
    WebDriver driver;
    private By bySearch = By.name("q");
        
    @Test
    public void getAttribute_ButtonName() {
        WebElement googlesearch = driver.findElement(bySearch);
        System.out.println("Name of the search is:- " +googlesearch.getAttribute("name"));
    }

    @Test
    public void getAttribute_Id() {
        WebElement googlesearch = driver.findElement(bySearch);
        System.out.println("Id of the search is:- "+ googlesearch.getAttribute("id"));
    }

@Test
    public void getAttribute_class() {

        WebElement googlesearch = driver.findElement(bySearch);
        System.out.println("Class of the search is:- "+ googlesearch.getAttribute("class"));

    }

    @Test
    public void getAttribute_InvalidAttribute() {

        WebElement googlesearch = driver.findElement(bySearch);
        //Will return null value as the 'status' attribute doesn't exists
        System.out.println("Invalid Attribute status of the search is:- "+ googlesearch.getAttribute("status"));
    }
    
    @Test
    public void getAttribute_ButtonLabel() {

        WebElement googlesearch = driver.findElement(bySearch);
        System.out.println("Label of the search is:- "+ googlesearch.getAttribute("aria-label"));
    }
  @BeforeTest
  public void beforeTest() {
          driver = new FirefoxDriver();
    //    System.setProperty("webdriver.chrome.driver","D:\\lib\\chromedriver.exe"); //--->chrome browser path
    //    driver=new ChromeDriver();
        driver.manage().window().maximize();  
        driver.get("http://www.google.com");
  }

}

Output :-
Label of the search is:- Search
Name of the search is:- q
Id of the search is:- lst-ib
Invalid Attribute status of the search is:- null
Class of the search is:- gsfi lst-d-f
PASSED: getAttribute_ButtonLabel
PASSED: getAttribute_ButtonName
PASSED: getAttribute_Id
PASSED: getAttribute_InvalidAttribute
PASSED: getAttribute_class

Comments