|
| 1 | +package SeleniumSessions; |
| 2 | + |
| 3 | +import java.util.concurrent.TimeUnit; |
| 4 | + |
| 5 | +import org.openqa.selenium.By; |
| 6 | +import org.openqa.selenium.WebDriver; |
| 7 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 8 | + |
| 9 | +public class CheckVisibilityElement { |
| 10 | + |
| 11 | + /** |
| 12 | + * |
| 13 | + * Learn difference between: |
| 14 | +
|
| 15 | +isDisplayed() v/s isEnabled() v/s isSelected() |
| 16 | +
|
| 17 | +Notes: |
| 18 | +• isDisplayed() is the method used to verify presence of a web element within the webpage. |
| 19 | + The method returns a “true” value if the specified web element is present on the web page and a “false” value if the web element is not present on the web page. |
| 20 | +• isDisplayed() is capable to check for the presence of all kinds of web elements available. |
| 21 | +• isEnabled() is the method used to verify if the web element is enabled or disabled within the webpage. |
| 22 | +• isEnabled() is primarily used with buttons. |
| 23 | +• isSelected() is the method used to verify if the web element is selected or not. isSelected() |
| 24 | +method is pre-dominantly used with radio buttons, dropdowns and checkboxes. |
| 25 | +
|
| 26 | + * |
| 27 | + */ |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + public static void main(String[] args) throws InterruptedException { |
| 33 | + |
| 34 | + System.setProperty("webdriver.chrome.driver", "/Users/naveenkhunteta/Downloads/chromedriver"); |
| 35 | + |
| 36 | + WebDriver driver = new ChromeDriver(); // launch chrome |
| 37 | + |
| 38 | + driver.manage().window().maximize(); // maximize window |
| 39 | + driver.manage().deleteAllCookies(); // delete all the cookies |
| 40 | + |
| 41 | + // dynamic wait |
| 42 | + driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); |
| 43 | + driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); |
| 44 | + |
| 45 | + driver.get("https://www.freecrm.com/register/"); // enter URL |
| 46 | + |
| 47 | + //isDiplayed() Method: |
| 48 | + boolean b1 = driver.findElement(By.id("submitButton")).isDisplayed(); |
| 49 | + System.out.println(b1); //true |
| 50 | + |
| 51 | + //before selecting checkbox --I agree |
| 52 | + //isEnabled() method: |
| 53 | + boolean b2 = driver.findElement(By.id("submitButton")).isEnabled(); |
| 54 | + System.out.println(b2); //false |
| 55 | + |
| 56 | + //lets make submit button enabled: |
| 57 | + driver.findElement(By.name("agreeTerms")).click(); //check I Agree checkbox |
| 58 | + |
| 59 | + //after selecting checkbox --I agree |
| 60 | + //isEnabled() method: |
| 61 | + boolean b3 = driver.findElement(By.id("submitButton")).isEnabled(); |
| 62 | + System.out.println(b3); //true |
| 63 | + |
| 64 | + |
| 65 | + //isSelected() method: only applicable for checkbox, dropdown, radiobutton |
| 66 | + boolean flag1 = driver.findElement(By.name("agreeTerms")).isSelected(); |
| 67 | + System.out.println(flag1); //true |
| 68 | + |
| 69 | + //de-select the checkbox-- I Agree |
| 70 | + driver.findElement(By.name("agreeTerms")).click(); //check I Agree checkbox |
| 71 | + |
| 72 | + boolean flag2 = driver.findElement(By.name("agreeTerms")).isSelected(); |
| 73 | + System.out.println(flag2); //false |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + } |
| 79 | + |
| 80 | +} |
0 commit comments