Skip to content

Commit

Permalink
Refactoring to BaseBrowser class, moving and reducing the duplicated …
Browse files Browse the repository at this point in the history
…code:

- Two new methods, that instantiate a remotedriver and sets the common capabilities
- Improved and removed code not necessary
  • Loading branch information
augustocristian committed Aug 20, 2024
1 parent 1fbb567 commit b7f6f5f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 136 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<String, Object> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand 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<String, Object> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> 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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public static Stream<Arguments> 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"})
Expand Down

0 comments on commit b7f6f5f

Please sign in to comment.