Skip to content

Commit 6ed088d

Browse files
committed
add waiting example code
1 parent 17cc651 commit 6ed088d

File tree

5 files changed

+259
-8
lines changed

5 files changed

+259
-8
lines changed

examples/dotnet/SeleniumDocs/Waits/WaitsTest.cs

+50-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using Microsoft.VisualStudio.TestTools.UnitTesting;
34
using OpenQA.Selenium;
45
using OpenQA.Selenium.Support.UI;
@@ -9,13 +10,25 @@ namespace SeleniumDocs.Waits
910
public class WaitsTest : BaseChromeTest
1011
{
1112
[TestMethod]
12-
public void Explicit()
13+
public void Fails()
1314
{
1415
driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html";
1516
driver.FindElement(By.Id("adder")).Click();
1617

17-
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
18-
IWebElement added = wait.Until(d => d.FindElement(By.Id("box0")));
18+
Assert.ThrowsException<NoSuchElementException>(
19+
() => driver.FindElement(By.Id("box0"))
20+
);
21+
}
22+
23+
[TestMethod]
24+
public void Sleep()
25+
{
26+
driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html";
27+
driver.FindElement(By.Id("adder")).Click();
28+
29+
Thread.Sleep(1000);
30+
31+
IWebElement added = driver.FindElement(By.Id("box0"));
1932

2033
Assert.AreEqual("redbox", added.GetDomAttribute("class"));
2134
}
@@ -32,5 +45,39 @@ public void Implicit()
3245

3346
Assert.AreEqual("redbox", added.GetDomAttribute("class"));
3447
}
48+
49+
[TestMethod]
50+
public void Explicit()
51+
{
52+
driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html";
53+
IWebElement revealed = driver.FindElement(By.Id("revealed"));
54+
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1));
55+
56+
driver.FindElement(By.Id("reveal")).Click();
57+
wait.Until(d => revealed.Displayed);
58+
59+
revealed.SendKeys("Displayed");
60+
Assert.AreEqual("Displayed", revealed.GetDomProperty("value"));
61+
}
62+
63+
[TestMethod]
64+
public void ExplicitOptions()
65+
{
66+
driver.Url = "https://www.selenium.dev/selenium/web/dynamic.html";
67+
IWebElement revealed = driver.FindElement(By.Id("revealed"));
68+
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(1))
69+
{
70+
PollingInterval = TimeSpan.FromMilliseconds(300),
71+
};
72+
wait.IgnoreExceptionTypes(typeof(ElementNotInteractableException));
73+
74+
driver.FindElement(By.Id("reveal")).Click();
75+
wait.Until(d => {
76+
revealed.SendKeys("Displayed");
77+
return true;
78+
});
79+
80+
Assert.AreEqual("input", revealed.TagName);
81+
}
3582
}
3683
}

examples/java/pom.xml

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,28 @@
2222
<dependency>
2323
<groupId>org.seleniumhq.selenium</groupId>
2424
<artifactId>selenium-java</artifactId>
25-
<version>4.8.0</version>
25+
<version>4.8.3</version>
2626
</dependency>
2727
<dependency>
2828
<groupId>org.seleniumhq.selenium</groupId>
2929
<artifactId>selenium-grid</artifactId>
30-
<version>4.8.0</version>
30+
<version>4.8.3</version>
3131
</dependency>
3232
<dependency>
3333
<groupId>org.slf4j</groupId>
3434
<artifactId>slf4j-api</artifactId>
35-
<version>1.7.36</version>
35+
<version>2.0.5</version>
3636
</dependency>
3737
<dependency>
3838
<groupId>ch.qos.logback</groupId>
3939
<artifactId>logback-classic</artifactId>
40-
<version>1.2.11</version>
40+
<version>1.4.6</version>
4141
</dependency>
4242

4343
<dependency>
4444
<groupId>org.junit.jupiter</groupId>
4545
<artifactId>junit-jupiter-engine</artifactId>
46-
<version>5.9.0</version>
46+
<version>5.9.2</version>
4747
<scope>test</scope>
4848
</dependency>
4949
<dependency>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package dev.selenium.waits;
2+
3+
import dev.selenium.BaseChromeTest;
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
import org.openqa.selenium.By;
7+
import org.openqa.selenium.ElementNotInteractableException;
8+
import org.openqa.selenium.Keys;
9+
import org.openqa.selenium.NoSuchElementException;
10+
import org.openqa.selenium.WebDriver;
11+
import org.openqa.selenium.WebElement;
12+
import org.openqa.selenium.interactions.Actions;
13+
import org.openqa.selenium.remote.RemoteWebDriver;
14+
import org.openqa.selenium.support.ui.FluentWait;
15+
import org.openqa.selenium.support.ui.Wait;
16+
import org.openqa.selenium.support.ui.WebDriverWait;
17+
18+
import java.time.Duration;
19+
20+
public class WaitsTest extends BaseChromeTest {
21+
@Test
22+
public void fails() {
23+
driver.get("https://www.selenium.dev/selenium/web/dynamic.html");
24+
driver.findElement(By.id("adder")).click();
25+
26+
Assertions.assertThrows(NoSuchElementException.class, () -> {
27+
driver.findElement(By.id("box0"));
28+
});
29+
}
30+
31+
@Test
32+
public void sleep() throws InterruptedException {
33+
driver.get("https://www.selenium.dev/selenium/web/dynamic.html");
34+
driver.findElement(By.id("adder")).click();
35+
36+
Thread.sleep(1000);
37+
38+
WebElement added = driver.findElement(By.id("box0"));
39+
40+
Assertions.assertEquals("redbox", added.getDomAttribute("class"));
41+
}
42+
43+
@Test
44+
public void implicit() {
45+
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(1));
46+
driver.get("https://www.selenium.dev/selenium/web/dynamic.html");
47+
driver.findElement(By.id("adder")).click();
48+
49+
WebElement added = driver.findElement(By.id("box0"));
50+
51+
Assertions.assertEquals("redbox", added.getDomAttribute("class"));
52+
}
53+
54+
@Test
55+
public void explicit() {
56+
driver.get("https://www.selenium.dev/selenium/web/dynamic.html");
57+
WebElement revealed = driver.findElement(By.id("revealed"));
58+
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(1));
59+
60+
driver.findElement(By.id("reveal")).click();
61+
wait.until(d -> revealed.isDisplayed());
62+
63+
revealed.sendKeys("Displayed");
64+
Assertions.assertEquals("Displayed", revealed.getDomProperty("value"));
65+
}
66+
67+
@Test
68+
public void explicitWithOptions() {
69+
driver.get("https://www.selenium.dev/selenium/web/dynamic.html");
70+
WebElement revealed = driver.findElement(By.id("revealed"));
71+
Wait<WebDriver> wait = new FluentWait<>(driver)
72+
.withTimeout(Duration.ofSeconds(1))
73+
.pollingEvery(Duration.ofMillis(300))
74+
.ignoring(ElementNotInteractableException.class);
75+
76+
driver.findElement(By.id("reveal")).click();
77+
wait.until(d -> {
78+
revealed.sendKeys("Displayed");
79+
return true;
80+
});
81+
82+
Assertions.assertEquals("Displayed", revealed.getDomProperty("value"));
83+
}
84+
}
+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import pytest
2+
import time
3+
from selenium.common import NoSuchElementException, ElementNotInteractableException
4+
from selenium.webdriver.common.by import By
5+
from selenium.webdriver.support.wait import WebDriverWait
6+
7+
8+
def test_fails(driver):
9+
driver.get('https://www.selenium.dev/selenium/web/dynamic.html')
10+
driver.find_element(By.ID, "adder").click()
11+
12+
with pytest.raises(NoSuchElementException):
13+
driver.find_element(By.ID, 'box0')
14+
15+
16+
def test_sleep(driver):
17+
driver.get('https://www.selenium.dev/selenium/web/dynamic.html')
18+
driver.find_element(By.ID, "adder").click()
19+
20+
time.sleep(1)
21+
added = driver.find_element(By.ID, "box0")
22+
23+
assert added.get_dom_attribute('class') == "redbox"
24+
25+
26+
def test_implicit(driver):
27+
driver.implicitly_wait(1)
28+
driver.get('https://www.selenium.dev/selenium/web/dynamic.html')
29+
driver.find_element(By.ID, "adder").click()
30+
31+
added = driver.find_element(By.ID, "box0")
32+
33+
assert added.get_dom_attribute('class') == "redbox"
34+
35+
36+
def test_explicit(driver):
37+
driver.get('https://www.selenium.dev/selenium/web/dynamic.html')
38+
revealed = driver.find_element(By.ID, "revealed")
39+
wait = WebDriverWait(driver, timeout=2)
40+
41+
driver.find_element(By.ID, "reveal").click()
42+
wait.until(lambda d : revealed.is_displayed())
43+
44+
revealed.send_keys("Displayed")
45+
assert revealed.get_property("value") == "Displayed"
46+
47+
48+
def test_explicit_options(driver):
49+
driver.get('https://www.selenium.dev/selenium/web/dynamic.html')
50+
revealed = driver.find_element(By.ID, "revealed")
51+
errors = [NoSuchElementException, ElementNotInteractableException]
52+
wait = WebDriverWait(driver, timeout=2, poll_frequency=.2, ignored_exceptions=errors)
53+
54+
driver.find_element(By.ID, "reveal").click()
55+
wait.until(lambda d : revealed.send_keys("Displayed") or True)
56+
57+
assert revealed.get_property("value") == "Displayed"
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# frozen_string_literal: true
2+
3+
require 'spec_helper'
4+
5+
RSpec.describe 'Waits' do
6+
let(:driver) { start_session }
7+
8+
it 'fails' do
9+
driver.get 'https://www.selenium.dev/selenium/web/dynamic.html'
10+
driver.find_element(id: 'adder').click
11+
12+
expect {
13+
driver.find_element(id: 'box0')
14+
}.to raise_error(Selenium::WebDriver::Error::NoSuchElementError)
15+
end
16+
17+
it 'sleeps' do
18+
driver.get 'https://www.selenium.dev/selenium/web/dynamic.html'
19+
driver.find_element(id: 'adder').click
20+
21+
sleep 1
22+
added = driver.find_element(id: 'box0')
23+
24+
expect(added.dom_attribute(:class)).to eq('redbox')
25+
end
26+
27+
it 'implicit' do
28+
driver.manage.timeouts.implicit_wait = 1
29+
driver.get 'https://www.selenium.dev/selenium/web/dynamic.html'
30+
driver.find_element(id: 'adder').click
31+
32+
added = driver.find_element(id: 'box0')
33+
34+
expect(added.dom_attribute(:class)).to eq('redbox')
35+
end
36+
37+
it 'explicit' do
38+
driver.get 'https://www.selenium.dev/selenium/web/dynamic.html'
39+
revealed = driver.find_element(id: 'revealed')
40+
wait = Selenium::WebDriver::Wait.new
41+
42+
driver.find_element(id: 'reveal').click
43+
wait.until { revealed.displayed? }
44+
45+
revealed.send_keys('Displayed')
46+
expect(revealed.property(:value)).to eq('Displayed')
47+
end
48+
49+
it 'options with explicit' do
50+
driver.get 'https://www.selenium.dev/selenium/web/dynamic.html'
51+
revealed = driver.find_element(id: 'revealed')
52+
errors = [Selenium::WebDriver::Error::NoSuchElementError,
53+
Selenium::WebDriver::Error::ElementNotInteractableError]
54+
wait = Selenium::WebDriver::Wait.new(timeout: 1,
55+
interval: 0.3,
56+
ignore: errors)
57+
58+
driver.find_element(id: 'reveal').click
59+
wait.until { revealed.send_keys('Displayed') || true }
60+
61+
expect(revealed.property(:value)).to eq('Displayed')
62+
end
63+
end

0 commit comments

Comments
 (0)