32. WebDriver Explicit Wait : Wait for element to be clickable in selenium web driver



In my previous post, We have seen how to wait implicitly in selenium webdriver software testing tool. Let me remind you one thing is implicit wait will be applied to all elements of test case by default while explicit will be applied to targeted element only. This is the difference between implicit wait and explicit wait.

WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton”)));

Here above syntax will wait till 15 seconds to become targeted element(#submitButton) clickable if it is not clickable or not loaded on the page of software web application. As soon as targeted element becomes clickable on the page of software web application, webdriver will go for perform next action. You can increase or decrease webdriver wait time from 15.

Let me give you practical example for the element to be clickable

package Others;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class webdriver_explicit {
                       
            @BeforeClass
            public void beforeClass() {
                        System.setProperty("webdriver.chrome.driver", "D:\\Softwares\\chromedriver_win32\\chromedriver.exe");   
                        driver = new ChromeDriver();
                        driver.manage().window().maximize();
                        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                        driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html");
                        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
            }
           
            public WebDriver driver;
            @Test
            public void testIsDiplayed_Enabled() throws Exception {
                       
                        driver.findElement(By.xpath("//input[@name='fname']")).sendKeys("My Name");
                        //Wait for element to be clickable for submit button
                        WebDriverWait wait = new WebDriverWait(driver, 15);
                        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#submitButton")));
                        driver.findElement(By.cssSelector("#submitButton")).click();
           
            }

            @AfterClass
            public void afterClass() throws Exception {
                        driver.quit();
            }

}

No comments:

Post a Comment