Verification Controls Methods in Selenium WebDriver

Verification Controls Methods

 The verification controls methods are used to check whether the web element is displayed, enabled, and selected.

Verification Controls Methods

Some of the most commonly used verification controls are as follow:

Verification controls Description Return Type CommandSyntax
isDisplayed() It is a method, and it is used to check whether the element is available in GUI. It return Boolean true value if the object is available. isDisplayed() : boolean   element.isDisplayed();    
isEnabled() It is used to check whether the element/object is active or performable or not It returns true value, if the object is enabled. isEnabled() : boolean   element.isEnabled();    
IsSelected() It is used to check whether the checkbox or radio button is already selected or not. It returns true, if element is selected already. isSelected() : boolean   element.isSelected();    

Let us take a sample test script in which we will try to cover mostly used verification_controls Commands:

For our testing purpose, we are using the login page of yahoo to perform mainly used data capture commands of web element controls.

In this test, we will automate the following test scenarios:

Steps Action Method Used Input Excepted Result
1. Open the Chrome Browser System.setProperty()   The Chrome browser must be opened.
2. Navigate to the yahoo login page. get() www.yahoo.com The login page must be displayed.
3. Identify the logo image, which is above an email edit box. And verify the logo image.   isDisplayed()   The logo image should be displayed.
4. Verify that the stay sign-in box is selected or not isSelected()   Stay sign-in box should be selected by default.  
5. Verify that the Button is enabled or not.   isEnabled()     The button should be enabled.  
6. Close the browser close()   The browser should be closed.

Open the Eclipse IDE and the existing test suite new_test, which we have created in WebDriver Installation section of WebDriver tutorial.

Then, right-click on the folder called src and create a new Class File from¬†New → Class.

click on the folder called src and create a new Class File
  • And give the Class name as verification_controls and click onthe Finish button.
Class name as verification_controls

We are creating our test case step by step to give you a complete understanding of verification _controls Commands in WebDriver.

Step1:

To launch the Google Chrome browser, we need to download the ChromeDriver.exe file and set the system property to the path of ChromeDriver.exe file.

Hence, here is the code for setting the system property for the google chrome:

//set the system property of Google Chrome
System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe");  

To initialize the Chrome driver using ChromeDriver class:

// create driver object for CHROME browser
WebDriver driver=new ChromeDriver(); 

Step2:

Launch the desired website using get() method.

//navigate to yahoo login page
driver.get("https://login.yahoo.com"); 

Step3:

Identify the logo image, which is above an email edit box, and verify the logo image using isdisplayed() verification method.

//identify the logo image, which is above the email edit box
WebElement iwb=driver.findElement(By.xpath("(//img[@class='logo'])[2]"));
//image should be display
boolean status=iwb.isDisplayed();
System.out.println("image status is => "+status); 

Step4:

  • Verify that the stay sign-in box is selected or not using isSelected() verification method.
// stay sign-in box should be selected by default
WebElement iwb1=driver.findElement(By.id("persistent"));
boolean status1 =iwb1.isSelected();
System.out.println("check box is selected =>" +status1);

Step5:

Verify that the Button is enabled or not using isenabled() verification control method.

//element should be enabled
WebElement iwb2=driver.findElement(By.xpath("//input[@id='login-signin']"));
boolean status2=iwb2.isEnabled();
System.out.println("button is enabled =>" +status2); 

Step6:                                                                                 

Finally, we terminate the process and close the browser.

 //Close the browser  
driver.close();   

The our final test script will look like this

package testpackage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class verification_controls {
public static void main(String[] args) { System.setProperty("webdriver.chrome.driver","C:\\Users\\JTP\\Downloads\\chromedriver_win32\\chromedriver.exe"); 
// create driver object for CHROME browser
WebDriver driver=new ChromeDriver();
//navigate to yahoo login page
driver.get("https://login.yahoo.com");
//identify the logo image which is above the email edit box
WebElement iwb=driver.findElement(By.xpath("(//img[@class='logo'])[2]"));
//image should be display 
boolean status=iwb.isDisplayed();
System.out.println("image status is => "+status);
// stay sign in box should be selected by default
WebElement iwb1=driver.findElement(By.id("persistent"));
boolean status1 =iwb1.isSelected();
System.out.println("check box is selected =>" +status1);
//element should be enabled 
WebElement iwb2=driver.findElement(By.xpath("//input[@id='login-signin']"));
boolean status2=iwb2.isEnabled();
System.out.println("button is enabled =>" +status2);
//close the driver
driver.close();
}
} 
  • To run the test script in Eclipse, right-click on the window and then click Run as → Java application.
To run the test script in Eclipse
  • The test script will be launched in the chrome browser and automate all the test scenarios.
  • And the output console window will show the results for all the print commands.
test script will be launched in the chrome browser



ADVERTISEMENT
ADVERTISEMENT