Should we use Implicit Wait or Explicit Wait for Selenium? Implicit Wait is a Selenium Wait Method that waits for all elements on a web page. Explicit Wait is a Wait Method that waits for 1 element at a time. It’s best to use Explicit Wait and handle slow loading elements on an element-by element basis.
The following video shows why Selenium Wait Methods are important then demo Implicit Wait and Explicit Wait. Afterwards, it explains why Explicit Wait is preferred over Implicit Wait.
Free Selenium Book (Implicit Wait and Explicit Wait via pages 117 - 122):
► Free Selenium PDF Book: [ Ссылка ]
► Free Java PDF Book: [ Ссылка ]
► All Paperback & eBooks: [ Ссылка ]
► View All Videos On LinkedIn [ Ссылка ]
► View All Videos On YouTube (Caption or Non-Caption) [ Ссылка ]
► View All Videos On Facebook (Caption or Non-Caption) [ Ссылка ]
► Increase Video Quality: Settings - Quality - Select HD
► CC Font Size: Settings - Subtitles/CC - Options - Font Size
► Turn CC On or Off: Click CC
► Transcript
Implicit Wait
Next is Implicit Wait.
Let’s walkthrough this Test Script for an AJAX web page. AJAX stands for Asynchronous JavaScript and XML. First, we load the web page which is Welcome To The Internet. This web page is Powered by Elemental Selenium where you can use Selenium like a Pro by Dave Haeffner.
Next, we click the Dynamic Loading hyperlink. After clicking Dynamic Loading, the next step is to click Example 2. The last step is to click the Start button. Do you see how we are waiting for the dynamic element to load?
Finally, in our Test Script, we are going to find Hello World, get the text of Hello World, then print Hello World. Let’s run our test.
Uh oh – NoSuchElementException. Unable to locate element. Which element? The string for Hello World. Our Test Script executed so fast that it did not wait for the next step which is find Hello World. That’s why Selenium provides Wait Methods so our execution can pause between these types of statements. If not, our Test Script will fail every time.
Let’s use Implicit Wait. driver.manage().timeouts().implicitlyWait(). The description states Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. This means Selenium will wait for all instances of driver. Enter 5 and TimeUnit is Seconds. Selenium will wait up to 5 seconds. Let’s Run. Now, we see Hello World! and Pass for our Test Script.
Implicit Wait would have waited for Lines 44, 45, 46, and 48 because they have a driver instance. If the element was not found for those lines then an exception would have been thrown. That’s it for Implicit Wait.
Explicit Wait
Next is Explicit Wait.
This is the same Test Script from Implicit Wait. Where we load the page, click the Dynamic Loading hyperlink, click Example 2, then click the Start button. Followed by finding Hello World, getting the text of Hello World, and printing Hello World. I’m going to run this Test Script again. It’s going to fail because of this dynamic Hello World element. Fail
Explicit Wait is used on 1 WebElement at a time. For that 1 element which is Hello World, execution will pause until time has expired or an expected condition is met using the WebDriverWait class.
Let’s start by writing WebDriverWait and wait as the object reference = new WebDriverWait() pass in driver and TimeOutInSeconds as 5 for the parameters. 5 represents the maximum number of seconds Selenium will wait for an element before throwing an exception.
Here’s the power of Explicit Wait. After clicking the Start button, we write wait dot until ExpectedConditions with an s dot. So far, we are going to wait until an expected condition is met. There’s many Expected Conditions. Our goal is to check if Hello World is present. We can select visibilityOfElementLocated which is an expectation for checking that an element is present. Locator will be By.xpath().
Let’s run. Hello World is printed to the Console. All of the ExpectedConditions are examples that assist us with writing our own customized Explicit Wait statements.
Let’s breakdown Explicit Wait.
With the first line, we have the WebDriverWait class which has a Constructor of WebDriverWait and 2 parameters. driver and 5. Let’s view JavaDocs for the WebDriverWait class. It has 3 Constructors. 1, 2, and 3. We used the 2nd Constructor which ignore instances of NotFoundException.
NotFoundException is an exception that is thrown when an element is not found. We see both parameters: driver and timeOutInSeconds.
#SeleniumTraining #SeleniumAutomation #BeginnerSeleniumTutorials #SeleniumWebDriver
Ещё видео!