Skip to content

Instantly share code, notes, and snippets.

@dhinojosa
Created November 5, 2021 05:12
Show Gist options
  • Save dhinojosa/7d346a5eaf5f39fdacd25749cac6da4a to your computer and use it in GitHub Desktop.
Save dhinojosa/7d346a5eaf5f39fdacd25749cac6da4a to your computer and use it in GitHub Desktop.
Understanding the Callback
/*
* (C) Copyright 2021 Boni Garcia (https://bonigarcia.github.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.github.bonigarcia.webdriver.jupiter.ch4.timeouts;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import java.time.Duration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.ScriptTimeoutException;
import org.openqa.selenium.WebDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
class ScriptTimeoutJupiterTest {
WebDriver driver;
@BeforeEach
void setup() {
driver = WebDriverManager.chromedriver().create();
}
@AfterEach
void teardown() {
driver.quit();
}
@Test
void testScriptTimeout() {
driver.get("https://bonigarcia.dev/selenium-webdriver-java/");
JavascriptExecutor js = (JavascriptExecutor) driver;
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(3));
assertThatThrownBy(() -> {
long waitMillis = Duration.ofSeconds(5).toMillis();
String script =
"console.log(arguments);" + //Understanding the arguments
"const callback = arguments[arguments.length - 1];" //Getting the last argument
+ "window.setTimeout(callback, " + waitMillis + ");";
js.executeAsyncScript(script);
}).isInstanceOf(ScriptTimeoutException.class);
try {
Thread.sleep(60000); //Sleeping for a minute so I can inspect
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment