Implicit wait and explicit wait example in selenium

Implicit wait and explicit wait example in selenium:- in this example user is opening his gmail account and navigate to composed mail.
-------------------------------------------------------------------------------------------------------

package demo_Testng;

import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeTest;

public class wait_Demo {
WebDriver driver;
@BeforeTest
public void setup()throws InterruptedException
{
driver=new FirefoxDriver();
driver.get("https://www.gmail.com");
driver.manage().window().maximize();
//implicit wait
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println(" implicit wait has been worked successfully");

}// end setup method
@Test
public void test() throws InterruptedException
{

WebElement username1 = driver.findElement(By.id("Email"));
    username1.sendKeys("Enter your username ");
    driver.findElement(By.id("next")).click();
    WebElement Password = driver.findElement(By.id("Passwd"));
    Password.sendKeys("Enter your password");
    driver.findElement(By.id("signIn")).click();
    // explicit wait - to wait for the compose button to be click-able
   WebDriverWait wait = new WebDriverWait(driver,30);
             wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
                  // click on the compose button as soon as the "compose" button is visible
           driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
           driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
}
@AfterTest
public void Closed()
{
driver.quit();
}

}

No comments:

Post a Comment