From b7f6f5fe90f4b12552ad1a3920c4ab0f7b1b4c0f Mon Sep 17 00:00:00 2001 From: Augusto Date: Sat, 3 Aug 2024 17:20:55 +0200 Subject: [PATCH] Refactoring to BaseBrowser class, moving and reducing the duplicated code: - Two new methods, that instantiate a remotedriver and sets the common capabilities - Improved and removed code not necessary --- .../e2e/no_elastest/common/BrowserUser.java | 46 +++++++++++++++- .../e2e/no_elastest/common/ChromeUser.java | 50 +++-------------- .../e2e/no_elastest/common/EdgeUser.java | 53 +++---------------- .../e2e/no_elastest/common/FirefoxUser.java | 52 +++--------------- .../no_elastest/functional/test/UserTest.java | 1 + 5 files changed, 66 insertions(+), 136 deletions(-) diff --git a/src/test/java/com/fullteaching/e2e/no_elastest/common/BrowserUser.java b/src/test/java/com/fullteaching/e2e/no_elastest/common/BrowserUser.java index 40ac74c..f812907 100644 --- a/src/test/java/com/fullteaching/e2e/no_elastest/common/BrowserUser.java +++ b/src/test/java/com/fullteaching/e2e/no_elastest/common/BrowserUser.java @@ -18,14 +18,23 @@ package com.fullteaching.e2e.no_elastest.common; import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.MutableCapabilities; import org.openqa.selenium.WebDriver; +import org.openqa.selenium.remote.LocalFileDetector; +import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; import java.time.Duration; - +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.Map; public class BrowserUser { @@ -36,12 +45,45 @@ public class BrowserUser { protected boolean isOnSession; protected WebDriverWait waiter; - public BrowserUser(String clientData, int timeOfWaitInSeconds) { + public BrowserUser(String clientData, int timeOfWaitInSeconds,String testName) { + log.debug("Creating BrowserUser for the test: {}", testName); this.clientData = clientData; this.timeOfWaitInSeconds = timeOfWaitInSeconds; this.isOnSession = false; } + public void configureRemoteWebDriver(String testName,MutableCapabilities options) throws URISyntaxException, MalformedURLException { + log.debug("SELENOID_PRESENT, using the remote WebDriver (Selenoid)"); + Map selenoidOptions = new HashMap<>(); + DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy-MM-dd-HH:mm"); + LocalDateTime now = LocalDateTime.now(); + String baseName = System.getProperty("tjob_name") + "-" + dtf.format(now) + "-" + testName + "-" + clientData ; + log.debug("Video and log files stored into .mp4 and .log named: {}" , baseName); + log.debug("Adding all the extra capabilities needed: {testName,enableVideo,enableVNC,name,enableLog,videoName,screenResolution}"); + //CAPABILITIES FOR SELENOID + selenoidOptions.put("testName", testName + "-" + clientData + "-" + dtf.format(now)); + selenoidOptions.put("enableVideo", true); + selenoidOptions.put("enableVNC", true); + selenoidOptions.put("name", testName + "-" + clientData); + selenoidOptions.put("enableLog", true); + selenoidOptions.put("logName ", String.format("%s%s",baseName.replaceAll("\\s","") , ".log")); + selenoidOptions.put("videoName", String.format("%s%s",baseName.replaceAll("\\s","") ,".mp4")); + selenoidOptions.put("screenResolution", "1920x1080x24"); + options.setCapability("selenoid:options", selenoidOptions); + //END CAPABILITIES FOR SELENOID RETORCH + log.debug("Configuring the remote WebDriver "); + RemoteWebDriver remote = new RemoteWebDriver(new URI("http://selenoid:4444/wd/hub").toURL(), options); + log.debug("Configuring the Local File Detector"); + remote.setFileDetector(new LocalFileDetector()); + this.driver = remote; + log.debug("End configuration of the RemoteWebDriver"); + } + public void waitAndLastConfDriver(){ + log.debug("Configure the driver connection timeouts at ({})", this.timeOfWaitInSeconds); + new WebDriverWait(driver, Duration.ofSeconds(this.timeOfWaitInSeconds)); + log.info("Driver Successfully configured"); + this.configureDriver(); + } public WebDriver getDriver() { return this.driver; } diff --git a/src/test/java/com/fullteaching/e2e/no_elastest/common/ChromeUser.java b/src/test/java/com/fullteaching/e2e/no_elastest/common/ChromeUser.java index f5e53b6..0eb902c 100644 --- a/src/test/java/com/fullteaching/e2e/no_elastest/common/ChromeUser.java +++ b/src/test/java/com/fullteaching/e2e/no_elastest/common/ChromeUser.java @@ -20,28 +20,20 @@ import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.logging.LoggingPreferences; -import org.openqa.selenium.remote.LocalFileDetector; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.support.ui.WebDriverWait; import java.net.MalformedURLException; -import java.net.URI; import java.net.URISyntaxException; -import java.time.Duration; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.Map; import static java.util.logging.Level.ALL; import static org.openqa.selenium.logging.LogType.BROWSER; public class ChromeUser extends BrowserUser { + ChromeOptions options = new ChromeOptions(); public ChromeUser(String userIdentifier, int timeOfWaitInSeconds, String testName) throws URISyntaxException, MalformedURLException { - super(userIdentifier, timeOfWaitInSeconds); - log.info("Starting the configuration of the web browser"); + super(userIdentifier, timeOfWaitInSeconds, testName); + log.info("Starting the configuration of the Chrome web browser"); LoggingPreferences logPrefs = new LoggingPreferences(); logPrefs.enable(BROWSER, ALL); @@ -53,42 +45,14 @@ public ChromeUser(String userIdentifier, int timeOfWaitInSeconds, String testNam options.addArguments("--start-maximized"); options.setAcceptInsecureCerts(true); //This capability is to store the logs of the test case - log.debug("Added Capabilities of acceptInsecureCerts and ignore alarms"); + log.debug("Added Capabilities of acceptInsecureCerts and --start-maximized"); if (System.getenv("SELENOID_PRESENT") == null) { - log.info("Using the Local WebDriver ()"); + log.info("Using the Local Chrome WebDriver ()"); this.driver = new ChromeDriver(options); } else { - Map selenoidOptions = new HashMap<>(); - log.info("Using the remote WebDriver (Selenoid)"); - DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy-MM-dd-HH:mm"); - LocalDateTime now = LocalDateTime.now(); - String baseName = System.getProperty("tjob_name") + "-" + dtf.format(now) + "-" + testName + "-" + userIdentifier ; - log.debug("The data of this test are stored into .mp4 and .log files named: {}" , baseName); - log.debug("Adding all the extra capabilities needed: {testName,enableVideo,enableVNC,name,enableLog,videoName,screenResolution}"); - //CAPABILITIES FOR SELENOID - - selenoidOptions.put("testName", testName + "-" + userIdentifier + "-" + dtf.format(now)); - selenoidOptions.put("enableVideo", true); - selenoidOptions.put("enableVNC", true); - selenoidOptions.put("name", testName + "-" + userIdentifier); - selenoidOptions.put("enableLog", true); - selenoidOptions.put("logName ", String.format("%s%s",baseName.replaceAll("\\s","") , ".log")); - selenoidOptions.put("videoName", String.format("%s%s",baseName.replaceAll("\\s","") ,".mp4")); - selenoidOptions.put("screenResolution", "1920x1080x24"); - options.setCapability("selenoid:options", selenoidOptions); - //END CAPABILITIES FOR SELENOID RETORCH - log.debug("Configuring the remote WebDriver "); - RemoteWebDriver remote = new RemoteWebDriver(new URI("http://selenoid:4444/wd/hub").toURL(), options); - log.debug("Configuring the Local File Detector"); - remote.setFileDetector(new LocalFileDetector()); - this.driver = remote; + configureRemoteWebDriver(testName, options); } - log.debug("Configure the driver connection timeouts at ({})", this.timeOfWaitInSeconds); - new WebDriverWait(driver, Duration.ofSeconds(this.timeOfWaitInSeconds)); - - log.info("Driver Successfully configured"); - this.configureDriver(); + waitAndLastConfDriver(); } - } \ No newline at end of file diff --git a/src/test/java/com/fullteaching/e2e/no_elastest/common/EdgeUser.java b/src/test/java/com/fullteaching/e2e/no_elastest/common/EdgeUser.java index b37c7d0..7db579d 100644 --- a/src/test/java/com/fullteaching/e2e/no_elastest/common/EdgeUser.java +++ b/src/test/java/com/fullteaching/e2e/no_elastest/common/EdgeUser.java @@ -19,67 +19,28 @@ import org.openqa.selenium.edge.EdgeDriver; import org.openqa.selenium.edge.EdgeOptions; -import org.openqa.selenium.remote.LocalFileDetector; -import org.openqa.selenium.remote.RemoteWebDriver; -import org.openqa.selenium.support.ui.WebDriverWait; import java.net.MalformedURLException; -import java.net.URI; import java.net.URISyntaxException; -import java.time.Duration; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.Map; public class EdgeUser extends BrowserUser { + EdgeOptions options = new EdgeOptions(); public EdgeUser(String userIdentifier, int timeOfWaitInSeconds, String testName) throws URISyntaxException, MalformedURLException { - super(userIdentifier, timeOfWaitInSeconds); - log.info(String.format("The Test names are: %s", testName)); - - - String eusApiURL = System.getenv("ET_EUS_API"); + super(userIdentifier, timeOfWaitInSeconds, testName); + log.info("Starting the configuration of Edge Web Browser, setting the capabilities"); //This capability is to store the logs of the test case - //Some tests report an error due to a non-trust server, this capability avoid it options.setAcceptInsecureCerts(true); - - if (eusApiURL == null) { + if (System.getenv("SELENOID_PRESENT") == null) { + log.info("SELENOID not present, using the local EdgeDriver") ; this.driver = new EdgeDriver(options); } else { - Map selenoidOptions = new HashMap<>(); - log.info("Using the remote WebDriver (Selenoid)"); - - DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy-MM-dd-HH:mm"); - LocalDateTime now = LocalDateTime.now(); - String baseName = System.getProperty("tjob_name") + "-" + dtf.format(now) + "-" + testName + "-" + userIdentifier ; - log.debug("The data of this test are stored into .mp4 and .log files named: {}" , baseName); - log.debug("Adding all the extra capabilities needed: {testName,enableVideo,enableVNC,name,enableLog,videoName,screenResolution}"); - //CAPABILITIES FOR SELENOID - - selenoidOptions.put("testName", testName + "-" + userIdentifier + "-" + dtf.format(now)); - selenoidOptions.put("enableVideo", true); - selenoidOptions.put("enableVNC", true); - selenoidOptions.put("name", testName + "-" + userIdentifier); - selenoidOptions.put("enableLog", true); - selenoidOptions.put("logName ", String.format("%s%s",baseName.replaceAll("\\s","") , ".log")); - selenoidOptions.put("videoName", String.format("%s%s",baseName.replaceAll("\\s","") ,".mp4")); - selenoidOptions.put("screenResolution", "1920x1080x24"); - options.setCapability("selenoid:options", selenoidOptions); - //END CAPABILITIES FOR SELENOID RETORCH - log.debug("Configuring the remote WebDriver "); - RemoteWebDriver remote = new RemoteWebDriver(new URI("http://selenoid:4444/wd/hub").toURL(), options); - log.debug("Configuring the Local File Detector"); - remote.setFileDetector(new LocalFileDetector()); - this.driver = remote; + configureRemoteWebDriver(testName, options); } - - new WebDriverWait(driver, Duration.ofSeconds(this.timeOfWaitInSeconds)); - - this.configureDriver(); + waitAndLastConfDriver(); } } \ No newline at end of file diff --git a/src/test/java/com/fullteaching/e2e/no_elastest/common/FirefoxUser.java b/src/test/java/com/fullteaching/e2e/no_elastest/common/FirefoxUser.java index 2d207dd..d7ec623 100644 --- a/src/test/java/com/fullteaching/e2e/no_elastest/common/FirefoxUser.java +++ b/src/test/java/com/fullteaching/e2e/no_elastest/common/FirefoxUser.java @@ -20,71 +20,33 @@ import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; -import org.openqa.selenium.remote.LocalFileDetector; -import org.openqa.selenium.remote.RemoteWebDriver; import java.net.MalformedURLException; -import java.net.URI; import java.net.URISyntaxException; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.HashMap; -import java.util.Map; public class FirefoxUser extends BrowserUser { + FirefoxOptions options = new FirefoxOptions(); public FirefoxUser(String userIdentifier, int timeOfWaitInSeconds, String testName) throws URISyntaxException, MalformedURLException { - super(userIdentifier, timeOfWaitInSeconds); - //TO-DO Firefox configuration has changed, review it. + super(userIdentifier, timeOfWaitInSeconds, testName); FirefoxProfile profile = new FirefoxProfile(); // This flag avoids granting the access to the camera profile.setPreference("media.navigator.permission.disabled", true); // This flag force using fake user media (synthetic video of multiple color) profile.setPreference("media.navigator.streams.fake", true); profile.setPreference("dom.file.createInChild", true); - - String eusApiURL = System.getenv("ET_EUS_API"); - - + options.setProfile(profile); options.setCapability("acceptInsecureCerts", true); - - if (eusApiURL == null) { - + if (System.getenv("SELENOID_PRESENT") == null) { + log.info("SELENOID not present, using the local Firefox WebDriver(), setting additional capability (marionette)"); options.setCapability("marionette", true); driver = new FirefoxDriver(options); - } else { - Map selenoidOptions = new HashMap<>(); - log.info("Using the remote WebDriver (Selenoid)"); - - DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yy-MM-dd-HH:mm"); - LocalDateTime now = LocalDateTime.now(); - String baseName = System.getProperty("tjob_name") + "-" + dtf.format(now) + "-" + testName + "-" + userIdentifier ; - log.debug("The data of this test are stored into .mp4 and .log files named: {}", baseName); - log.debug("Adding all the extra capabilities needed: {testName,enableVideo,enableVNC,name,enableLog,videoName,screenResolution}"); - //CAPABILITIES FOR SELENOID - - selenoidOptions.put("testName", testName + "-" + userIdentifier + "-" + dtf.format(now)); - selenoidOptions.put("enableVideo", true); - selenoidOptions.put("enableVNC", true); - selenoidOptions.put("name", testName + "-" + userIdentifier); - selenoidOptions.put("enableLog", true); - selenoidOptions.put("logName ", String.format("%s%s",baseName.replaceAll("\\s","") , ".log")); - selenoidOptions.put("videoName", String.format("%s%s",baseName.replaceAll("\\s","") ,".mp4")); - selenoidOptions.put("screenResolution", "1920x1080x24"); - options.setCapability("selenoid:options", selenoidOptions); - //END CAPABILITIES FOR SELENOID RETORCH - log.debug("Configuring the remote WebDriver "); - RemoteWebDriver remote = new RemoteWebDriver(new URI("http://selenoid:4444/wd/hub").toURL(), options); - log.debug("Configuring the Local File Detector"); - remote.setFileDetector(new LocalFileDetector()); - this.driver = remote; + configureRemoteWebDriver(testName, options); } - - this.configureDriver(); + waitAndLastConfDriver(); } - } \ No newline at end of file diff --git a/src/test/java/com/fullteaching/e2e/no_elastest/functional/test/UserTest.java b/src/test/java/com/fullteaching/e2e/no_elastest/functional/test/UserTest.java index 866b33f..cfb0af1 100644 --- a/src/test/java/com/fullteaching/e2e/no_elastest/functional/test/UserTest.java +++ b/src/test/java/com/fullteaching/e2e/no_elastest/functional/test/UserTest.java @@ -31,6 +31,7 @@ public static Stream data() throws IOException { * This test is a simple logging acknowledgement, that checks if the current logged user * was logged correctly */ + @Resource(resID = "LoginService", replaceable = {}) @AccessMode(resID = "LoginService", concurrency = 10, sharing = true, accessMode = "READONLY") @Resource(resID = "OpenVidu", replaceable = {"OpenViduMock"})