Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium implicitlyWait Not Working?

I am learning Java Maven Selenium. I want something like this in Selenium using implicitlyWait.

  1. Open website (for example https://www.facebook.com)
  2. Click on email field of login
  3. Wait 20 seconds
  4. Enter my email

Here is my simple code:

package com.org.learningMaven;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class HelloWorldTest {   
    @Test
    public void login() {
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.facebook.com/");
        driver.findElement(By.id("email")).click();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.id("email")).sendKeys("[email protected]");
    }
    private void sendKeys(Keys enter) {
        // TODO Auto-generated method stub

    }
}

This code is not working. It will simply open Facebook, click on email field & enter my email id instead of waiting 10 seconds before entering my email.

like image 971
carol Avatar asked Dec 22 '15 18:12

carol


People also ask

What is driver manage () timeouts () implicitlyWait?

1. implicitlyWait() This timeout is used to specify the amount of time the driver should wait while searching for an element if it is not immediately present.

Why implicit wait is not recommended?

Hey Aaron, the main disadvantage of implicit wait is that it slows down test performance. The implicit wait will tell to the web driver to wait for certain amount of time before it throws a "No Such Element Exception".

How do you use implicitlyWait duration?

Use implicitlyWait(Duration) Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException .

How do I force Selenium to wait?

Implicit Wait directs the Selenium WebDriver to wait for a certain measure of time before throwing an exception. Once this time is set, WebDriver will wait for the element before the exception occurs. Once the command is in place, Implicit Wait stays in place for the entire duration for which the browser is open.


1 Answers

Implicit Wait and Explicit Waits doesn't work that way, they will maximum wait for element for the time duration specified, If they find the element before that next step would be executed.

If you want your test to wait for exact time duration, you may want to use.

Thread.sleep(Time duration in milliseconds);

You may want to refer Diff b/w Implict Wait and Explicit Wait

Explicit Waits : An explicit waits is code you define to wait for a certain condition to occur before proceeding further in the code.

Implicit Waits : An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available.

Thread.sleep : In sleep code It will always wait for mentioned seconds, even in case the page is ready to interact after 1 sec. So this can slow the tests.

like image 186
Paras Avatar answered Sep 20 '22 14:09

Paras