WD Validation Methods:
-------------------------------
WebDriver provided following methods to validate webelements
i. isSelected:
ii. isEnabled:
iii. isDisplayed:
iv. size:
i. isSelected():
This method is used to check specified webelement is selected or not,
based on which it will return true/false
Exp: Check box, Radiobutton...etc.,
Syntax:
WebElement.isSelected();
ii. isEnabled():
This method is used to check specified webelement is enabled or not,
based on which it will return true/false
Exp: Pushbutton etc...,
Syntax: WebElement.isEnabled();
***iii. isDisplayed();
This method is very important, is used to check specified webelement displayed or
not, based on whcich it will return true/false (i.e. visible/invisible webelement)
Exp: Create script to check "Return" webelement visible or not BEFORE
and AFTER clicking on "Multi-City" on www.makemytrip.com home page
WebDriver driver= new FirefoxDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
System.out.println("Before selection of Multi-city");
//to check Return field displayed or not
if(driver.findElement(By.id("hp-widget__return")).isDisplayed()){
System.out.println("Return -field is Displayed");
}
else{
System.out.println("Return -field is invisible");
}
//to select "multicity"
driver.findElement(By.xpath("//label[text()='multi-city']")).click();
System.out.println("After selection of Multi-city");
//to check Return field displayed or not
if(driver.findElement(By.id("hp-widget__return")).isDisplayed()){
System.out.println("Return -field is Displayed");
}
else{
System.out.println("Return -field is invisible");
}
iv. Size():
To check specified webelement code availability in a page source we can use
size() method
Syntax:
driver.findElements(By.locator(“locator value”)).size();
Exp: Write script to check “Afrikaans” language link availability in Google
home page before & after click on that link
WebDriver driver= new FirefoxDriver();
driver.get("http://google.co.in");
driver.manage().window().maximize();
System.out.println("Before clicking on Afrikaans link");
//to check Afrikaans link availability
int n=driver.findElements(By.linkText("Afrikaans")).size();
if(n !=0){
System.out.println("Number of Afrikaans links are: "+n);
}
else{
System.out.println("There are no Afrikaans links");
}
//to click on Afrikaans link
driver.findElement(By.linkText("Afrikaans")).click();
//to pause execution
Thread.sleep(3000);
System.out.println("After click on Telugu link");
//to check Afrikaans link availability
n=driver.findElements(By.linkText("Afrikaans")).size();
if(n !=0){
System.out.println("Number of Afrikaans links are: "+n);
}
else{
System.out.println("There are no Afrikaans links");
}
-------------------------------
WebDriver provided following methods to validate webelements
i. isSelected:
ii. isEnabled:
iii. isDisplayed:
iv. size:
i. isSelected():
This method is used to check specified webelement is selected or not,
based on which it will return true/false
Exp: Check box, Radiobutton...etc.,
Syntax:
WebElement.isSelected();
ii. isEnabled():
This method is used to check specified webelement is enabled or not,
based on which it will return true/false
Exp: Pushbutton etc...,
Syntax: WebElement.isEnabled();
***iii. isDisplayed();
This method is very important, is used to check specified webelement displayed or
not, based on whcich it will return true/false (i.e. visible/invisible webelement)
Exp: Create script to check "Return" webelement visible or not BEFORE
and AFTER clicking on "Multi-City" on www.makemytrip.com home page
WebDriver driver= new FirefoxDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
System.out.println("Before selection of Multi-city");
//to check Return field displayed or not
if(driver.findElement(By.id("hp-widget__return")).isDisplayed()){
System.out.println("Return -field is Displayed");
}
else{
System.out.println("Return -field is invisible");
}
//to select "multicity"
driver.findElement(By.xpath("//label[text()='multi-city']")).click();
System.out.println("After selection of Multi-city");
//to check Return field displayed or not
if(driver.findElement(By.id("hp-widget__return")).isDisplayed()){
System.out.println("Return -field is Displayed");
}
else{
System.out.println("Return -field is invisible");
}
iv. Size():
To check specified webelement code availability in a page source we can use
size() method
Syntax:
driver.findElements(By.locator(“locator value”)).size();
Exp: Write script to check “Afrikaans” language link availability in Google
home page before & after click on that link
WebDriver driver= new FirefoxDriver();
driver.get("http://google.co.in");
driver.manage().window().maximize();
System.out.println("Before clicking on Afrikaans link");
//to check Afrikaans link availability
int n=driver.findElements(By.linkText("Afrikaans")).size();
if(n !=0){
System.out.println("Number of Afrikaans links are: "+n);
}
else{
System.out.println("There are no Afrikaans links");
}
//to click on Afrikaans link
driver.findElement(By.linkText("Afrikaans")).click();
//to pause execution
Thread.sleep(3000);
System.out.println("After click on Telugu link");
//to check Afrikaans link availability
n=driver.findElements(By.linkText("Afrikaans")).size();
if(n !=0){
System.out.println("Number of Afrikaans links are: "+n);
}
else{
System.out.println("There are no Afrikaans links");
}