Skip to main content

Implicit and Explicit Waits in selenium with examples

Explicit Wait is related with certain conditions to wait; Implicit Wait with specific time to wait for an Element.


Explicit Wait

Here, the driver waits for 10 secs till the web element is found;  If not, it simply throws a Timeout Exception.

WebElement element = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("Value")));

or|

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("Value")));


WebDriverWait can't be declared globally and throw NullPointerException; whereas the below snippets will help you to do so.


// Handles any locator
@Test
public void Test01() throws Exception {
  driver.get("www.xyz.com");
  wait().until(ExpectedConditions.presenceOfElementLocated(By.xpath("Value")));
  }

private WebDriverWait wait()
   {
    return new WebDriverWait(driver, 20);
   }


// Handles any locator [This method works for page object pattern]
private WebDriver driver;
private final Wait<WebDriver> wait;

public Classname(WebDriver driver) { //constructor
   this.driver = driver;
   wait = new WebDriverWait(driver, 20);
 }

@Test
public void Test01() throws Exception {
  driver.get("www.xyz.com");
  wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("Value")));
  }


// Handle the locators by id, name, xpath, css, etc.,
@Test
public void Test02() throws Exception {
  driver.get("www.xyz.com");
  waitForID("Value");
   }

public void waitForID(String id) 
   {         
    WebDriverWait wait = new WebDriverWait(driver, 10);      
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id(id)));      
   }

Comments

Popular posts from this blog

Rational Functional Tester: Calling RFT scripts from using xml tags

Hello, This will help to call RFT scripts in xml, if anyone using 'ant' this will help them to embed code to invoke RFT scripts. <java classname="com.rational.test.ft.rational_ft" fork="true" maxmemory="1024m">         <classpath>         <fileset dir="C:\Program Files\IBM\SDP\FunctionalTester\bin\">         <include name="rational_ft.jar" />         </fileset>         </classpath>         <jvmarg line="-Drational_ft.install.dir=&quot;C:\Program Files\IBM\SDP\jdk_\jre\bin&quot;" />         <arg line="-rt.bring_up_logviewer false -datastore &quot;D:\RFT-Dev\Project&quot; -playback RFTTestSet5.testcase4.TestScript1" />         <arg line="-args -scriptArg ${scriptValue}" /> ...

Adding Delay to Jenins Build Steps

For Windows batch Command use below command waitfor SomethingThatIsNeverHappening /t 60 The above command will wait for 60 secs For Jenkins 'Execution Shells' sleep 30s This will sleep for 30 sec and resume the execution

Rational Functional Tester: Creating and Reading Datapool cell value in RFT

Hello, Hope this will help for my friends who is trying to use data pool for dynamic values.If you want to use any run time values in between scripts then you will be looking for the functions to read/write/create datapool cells dynamically. /************ Dp row count *************************/ IDatapoolIterator  ite; ite.dpCurrent().getEquivalenceClass().getRecordCount(); /***************** to set cell value **************/ IDatapoolIterator it; ((DatapoolCell) it.dpCurrent().getCell("RunTimeSheetForContainers")).setCellValue("Y"); /*********** to load CSV files (csv to Datapool)*******************************/ public IDatapoolIterator LoadCSV(String Sname)       {             File fname=getFileName(Sname);             IDatapool dp=DatapoolUtilities. loadCSV (fname, "," , true );  ...