-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Upgrade to selenium version 4.27.0
2. Add Firefox driver and Docker Firefox driver.
- Loading branch information
1 parent
236241b
commit 8f21e22
Showing
8 changed files
with
243 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/main/java/org/zkoss/test/webdriver/DockerFirefoxDriverTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* DockerFirefoxDriverTestCase.java | ||
Purpose: | ||
Description: | ||
History: | ||
10:45 AM 2024/11/29, Created by jumperchen | ||
Copyright (C) 2024 Potix Corporation. All Rights Reserved. | ||
*/ | ||
package org.zkoss.test.webdriver; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.RandomAccessFile; | ||
import java.nio.channels.FileChannel; | ||
import java.nio.channels.FileLock; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardCopyOption; | ||
import java.text.SimpleDateFormat; | ||
|
||
import com.palantir.docker.compose.DockerComposeExtension; | ||
import com.palantir.docker.compose.configuration.ShutdownStrategy; | ||
import com.palantir.docker.compose.connection.waiting.HealthChecks; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.junit.jupiter.api.parallel.ResourceLock; | ||
|
||
/** | ||
* A test case with a Firefox WebDriver running in a Docker container. | ||
* @author jumperchen | ||
*/ | ||
@ResourceLock("dockerResource") | ||
public abstract class DockerFirefoxDriverTestCase extends FirefoxWebDriverTestCase { | ||
// enable to use docker env. | ||
protected final boolean isUseDocker() { | ||
return true; | ||
} | ||
|
||
|
||
protected String getRemoteWebDriverUrl() { | ||
final int externalPort = firefoxDocker.containers().container("hub").port(4444).getExternalPort(); | ||
return "http://localhost:" + externalPort + "/wd/hub"; | ||
} | ||
|
||
protected FileLock globalLock; | ||
|
||
private static final String tempDir = new SimpleDateFormat("yyyyMMdd").format(new java.util.Date()); | ||
|
||
// create a temp file for docker compose.yml | ||
protected String exportResource(String file) { | ||
InputStream resourceAsStream = ClassLoader.getSystemResourceAsStream( | ||
"docker/docker-compose.yml"); | ||
try { | ||
Path path = Paths.get(System.getProperty("java.io.tmpdir"), tempDir, "zkWebdriver").resolve(file); | ||
if (!Files.isDirectory(path.getParent())) { | ||
Files.createDirectories(path.getParent()); | ||
} | ||
if (path.toFile().exists()) { | ||
path.toFile().delete(); | ||
} | ||
|
||
// always copy the docker-compose.yml to the temp file | ||
Files.copy(resourceAsStream, path, StandardCopyOption.REPLACE_EXISTING); | ||
|
||
// try to acquire a global lock for each DockerWebDriver | ||
RandomAccessFile files = new RandomAccessFile(path.toFile(), "rw"); | ||
FileChannel channel = files.getChannel(); | ||
while (true) { | ||
try { | ||
globalLock = channel.tryLock(); | ||
if (globalLock.isValid()) { | ||
break; | ||
} | ||
} catch (Throwable e) { | ||
// File is already locked in this thread or virtual machine | ||
} | ||
try { | ||
Thread.sleep(200); | ||
} catch (InterruptedException ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
return path.toAbsolutePath().toString(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@AfterAll | ||
public void unlockGlobalLock() { | ||
if (globalLock != null) { | ||
try { | ||
globalLock.release(); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
@RegisterExtension | ||
public final DockerComposeExtension firefoxDocker = DockerComposeExtension.builder() | ||
.file(exportResource("docker/docker-compose.yml")) | ||
.useDockerComposeV2(Boolean.parseBoolean(System.getProperty("useDockerComposeV2", "true"))) | ||
.waitingForService("hub", HealthChecks.toRespondOverHttp(4444, | ||
(port) -> port.inFormat("http://$HOST:$EXTERNAL_PORT/ui/index.html"))) | ||
.waitingForService("firefox", HealthChecks.toHaveAllPortsOpen()) | ||
.shutdownStrategy(ShutdownStrategy.KILL_DOWN) | ||
.build(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/org/zkoss/test/webdriver/FirefoxHeadlessDriver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* FirefoxHeadlessDriver.java | ||
Purpose: | ||
Description: | ||
History: | ||
10:36 AM 2024/11/29, Created by jumperchen | ||
Copyright (C) 2024 Potix Corporation. All Rights Reserved. | ||
*/ | ||
package org.zkoss.test.webdriver; | ||
|
||
import io.github.bonigarcia.wdm.WebDriverManager; | ||
import org.openqa.selenium.firefox.FirefoxDriver; | ||
import org.openqa.selenium.firefox.FirefoxDriverService; | ||
import org.openqa.selenium.firefox.FirefoxOptions; | ||
|
||
/** | ||
* A local running Firefox GeckoDriver. By default it runs in headless mode. | ||
* @author jumperchen | ||
*/ | ||
public class FirefoxHeadlessDriver extends FirefoxDriver { | ||
static { | ||
WebDriverManager.firefoxdriver().setup(); | ||
System.setProperty("webdriver.gecko.logfile", String.format("%s/geckodriver.log", System.getProperty("java.io.tmpdir"))); | ||
System.setProperty("webdriver.gecko.log.level", "debug"); | ||
} | ||
|
||
public FirefoxHeadlessDriver() { | ||
this(true); | ||
} | ||
|
||
public FirefoxHeadlessDriver(boolean headless) { | ||
this(new FirefoxOptions(), headless); | ||
} | ||
|
||
public FirefoxHeadlessDriver(FirefoxOptions options) { | ||
this(options, true); | ||
} | ||
|
||
public FirefoxHeadlessDriver(FirefoxOptions options, boolean headless) { | ||
super(headlessSettings(options, headless)); | ||
} | ||
|
||
public FirefoxHeadlessDriver(FirefoxDriverService service) { | ||
this(service, new FirefoxOptions()); | ||
} | ||
|
||
public FirefoxHeadlessDriver(FirefoxDriverService service, FirefoxOptions options) { | ||
this(service, options, true); | ||
} | ||
|
||
public FirefoxHeadlessDriver(FirefoxDriverService service, FirefoxOptions options, boolean headless) { | ||
super(service, headlessSettings(options, headless)); | ||
} | ||
|
||
private static FirefoxOptions headlessSettings(FirefoxOptions options, boolean headless) { | ||
if (headless) { | ||
options.addArguments("--headless"); | ||
} | ||
return options; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/org/zkoss/test/webdriver/FirefoxWebDriverTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* FirefoxWebDriverTestCase.java | ||
Purpose: | ||
Description: | ||
History: | ||
2:00 PM 2024/11/29, Created by jumperchen | ||
Copyright (C) 2024 Potix Corporation. All Rights Reserved. | ||
*/ | ||
package org.zkoss.test.webdriver; | ||
|
||
import org.openqa.selenium.WebDriver; | ||
import org.openqa.selenium.firefox.FirefoxOptions; | ||
|
||
/** | ||
* A Firefox WebDriver test case. | ||
* @author jumperchen | ||
*/ | ||
public class FirefoxWebDriverTestCase extends WebDriverTestCase{ | ||
|
||
protected FirefoxOptions getFirefixOptions() { | ||
FirefoxOptions options = new FirefoxOptions(); | ||
|
||
options.addArguments("--width=1920", "--height=1080"); | ||
options.addPreference("moz:firefoxOptions", "--remote-allow-origins=*"); | ||
return options; | ||
} | ||
|
||
protected WebDriver getWebDriver() { | ||
if (driver == null) { | ||
FirefoxOptions driverOptions = getFirefixOptions(); | ||
driver = isUsingRemoteWebDriver(driverOptions) | ||
? new DockerRemoteWebDriver(getRemoteWebDriverUrl(), driverOptions) | ||
: new FirefoxHeadlessDriver(getFirefixOptions(), isHeadless()); | ||
} | ||
return driver; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters