Skip to content

Commit

Permalink
Adding selenium grid and modify selenium code
Browse files Browse the repository at this point in the history
  • Loading branch information
teahan105 committed Mar 14, 2024
1 parent 155cb51 commit 65008b8
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 70 deletions.
14 changes: 7 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ services:
- REMOTE_WEBDRIVER_PASSWORD=vmo@123
volumes:
- backend_data:/app/data # Mount a volume for application data
node-docker:
image: selenium/node-docker:latest
volumes:
- ./volume/assets:/opt/selenium/assets
- ./volume/config.toml:/opt/bin/config.toml
- /var/run/docker.sock:/var/run/docker.sock
chrome:
image: selenium/node-chrome:4.18.1-20240224
shm_size: 2gb
depends_on:
- selenium-hub
environment:
- SE_EVENT_BUS_HOST=selenium-hub
- SE_EVENT_BUS_PUBLISH_PORT=4442
- SE_EVENT_BUS_SUBSCRIBE_PORT=4443

selenium-hub:
image: selenium/hub:latest
image: selenium/hub:4.18.1-20240224
container_name: selenium-hub
ports:
- "4442:4442"
- "4443:4443"
- "4444:4444"
volumes:
- ./volume/config.toml:/opt/selenium/config.toml
volumes:
mysql_data: # Define the volume for MySQL data
backend_data: # Define the volume for application data
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
package com.hanstack.linkedintool;

import com.hanstack.linkedintool.enums.ProfileEnum;
import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.Platform;
import org.openqa.selenium.UsernameAndPassword;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.Browser;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.HttpCommandExecutor;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.http.ClientConfig;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.util.Base64;

@SpringBootApplication
@Configuration
@Slf4j
public class LinkedinToolApplication {

Expand All @@ -54,35 +45,34 @@ public static void main(String[] args) {
}

@Bean
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public WebDriver getDriverInstance() {
WebDriver driver = null;
try {
var chromeOptions = new ChromeOptions()
.addEncodedExtensions(Base64.getEncoder().encodeToString(drx.getContentAsByteArray()));

if (StringUtils.equals(env, ProfileEnum.DEV.getName())) {
driver = new ChromeDriver(chromeOptions);
} else {
ClientConfig clientConfig = ClientConfig.defaultConfig()
// if (StringUtils.equals(env, ProfileEnum.DEV.getName())) {
// driver = new ChromeDriver(chromeOptions);
// } else {
var clientConfig = ClientConfig.defaultConfig()
.baseUrl(new URL(rwDriverUrl))
.authenticateAs(new UsernameAndPassword(rwDriverUsername, rwDriverPassword));
HttpCommandExecutor executor = new HttpCommandExecutor(clientConfig);

chromeOptions.setCapability(CapabilityType.BROWSER_NAME, Browser.CHROME.browserName());
chromeOptions.setCapability(CapabilityType.PLATFORM_NAME, Platform.LINUX.name());
var executor = new HttpCommandExecutor(clientConfig);
// chromeOptions.setCapability(CapabilityType.BROWSER_NAME, Browser.CHROME.browserName());
// chromeOptions.setCapability(CapabilityType.PLATFORM_NAME, Platform.LINUX.name());
driver = new RemoteWebDriver(executor, chromeOptions);
}
// }
} catch (IOException ex) {
log.error("There is an error when init the webdriver instance", ex.getCause());
}

return driver;
}

@Bean
public Wait<WebDriver> getWaitDriverInstance() {
WebDriver driver = getDriverInstance();
return new WebDriverWait(driver, Duration.ofSeconds(2));
}
// @Bean
// public Wait<WebDriver> getWaitDriverInstance() {
// return new WebDriverWait(getDriverInstance(), Duration.ofSeconds(2));
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ public String process(@ModelAttribute LinkedinDTO linkedinDTO, HttpSession httpS
model.addAttribute("lstFilterBarGrouping", ToolbarEnum.values());
model.addAttribute(httpSession.getAttribute("linkedinDTO"));
} catch (Exception ex) {
log.error("ERROR: ", ex.getCause());
log.error("ERROR: {}", ex.getMessage());
} finally {
log.info("Close only the current browser window");
seleniumService.close();
seleniumService.quit();
}
return "index";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.hanstack.linkedintool.service.impl;

import com.hanstack.linkedintool.dto.FilterDTO;
import com.hanstack.linkedintool.enums.ProfileEnum;
import com.hanstack.linkedintool.service.SeleniumService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.time.Duration;

@Service
@Slf4j
Expand All @@ -26,12 +26,11 @@ public class SeleniumServiceImpl implements SeleniumService {

public SeleniumServiceImpl(
@Value("${spring.profiles.active:}") String env,
WebDriver driver,
Wait<WebDriver> wait
WebDriver driver
) {
this.env = env;
this.driver = driver;
this.wait = wait;
this.wait = new WebDriverWait(driver, Duration.ofSeconds(2));
}

@Override
Expand All @@ -47,9 +46,9 @@ public void deleteAndImportCookies(MultipartFile cookieFile) throws InterruptedE
Thread.sleep(1000);
driver.switchTo().newWindow(WindowType.TAB);
driver.get("chrome-extension://okpidcojinmlaakglciglbpcpajaibco/popup.html?url=aHR0cHM6Ly93d3cubGlua2VkaW4uY29tLw%3D%3D");
if (StringUtils.equals(env, ProfileEnum.PROD.getName())) {
// if (StringUtils.equals(env, ProfileEnum.PROD.getName())) {
((RemoteWebDriver) driver).setFileDetector(new LocalFileDetector());
}
// }
WebElement fileInput = driver.findElement(By.cssSelector("input[type=file]"));
File tempFile = File.createTempFile("temp", null);
cookieFile.transferTo(tempFile);
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ spring:
username: root
password: Admin@10525597
jpa:
properties:
hibernate:
dialect: org.hibernate.dialect.MySQLDialect
hibernate:
ddl-auto: update
thymeleaf:
Expand Down
26 changes: 0 additions & 26 deletions volume/config.toml
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
[docker]
# Configs have a mapping between the Docker image to use and the capabilities that need to be matched to
# start a container with the given image.
configs = [
# "selenium/standalone-firefox:4.18.1-20240224", '{"browserName": "firefox", "platformName": "linux"}',
"selenium/standalone-chrome:latest", '{"browserName": "chrome", "platformName": "linux"}',
# "selenium/standalone-edge:4.18.1-20240224", '{"browserName": "MicrosoftEdge", "platformName": "linux"}'
]

# URL for connecting to the docker daemon
# host.docker.internal works for macOS and Windows.
# Linux could use --net=host in the `docker run` instruction or 172.17.0.1 in the URI below.
# To have Docker listening through tcp on macOS, install socat and run the following command
# socat -4 TCP-LISTEN:2375,fork UNIX-CONNECT:/var/run/docker.sock
#linux
#url = "http://127.0.0.1:2375"
#macos
url = "http://host.docker.internal:2375"
# Docker image used for video recording
video-image = "selenium/video:latest"

# Uncomment the following section if you are running the node on a separate VM
# Fill out the placeholders with appropriate values
#[server]
#host = <ip-from-node-machine>
#port = <port-from-node-machine>
[route]
username = "admin"
password = "vmo@123"

0 comments on commit 65008b8

Please sign in to comment.