fix: 增加测试能力
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.lanyuanxiaoyao.messenger
|
||||
|
||||
import com.lanyuanxiaoyao.messenger.runner.OdsScreenshotRunner
|
||||
import jakarta.annotation.Resource
|
||||
import java.io.File
|
||||
import org.slf4j.LoggerFactory
|
||||
@@ -12,6 +13,11 @@ import org.springframework.boot.context.properties.bind.ConstructorBinding
|
||||
import org.springframework.boot.runApplication
|
||||
import org.springframework.scheduling.annotation.EnableScheduling
|
||||
|
||||
@ConfigurationProperties(prefix = "messenger")
|
||||
data class TestProperties @ConstructorBinding constructor(
|
||||
val test: Boolean,
|
||||
)
|
||||
|
||||
@ConfigurationProperties(prefix = "messenger.driver")
|
||||
data class DriverProperties @ConstructorBinding constructor(
|
||||
val driverPath: String,
|
||||
@@ -21,18 +27,24 @@ data class DriverProperties @ConstructorBinding constructor(
|
||||
|
||||
@ConfigurationProperties(prefix = "messenger.mail")
|
||||
data class MailProperties @ConstructorBinding constructor(
|
||||
val targets: List<String>
|
||||
val targets: List<String>,
|
||||
)
|
||||
|
||||
@EnableScheduling
|
||||
@EnableConfigurationProperties(DriverProperties::class, MailProperties::class)
|
||||
@EnableConfigurationProperties(TestProperties::class, DriverProperties::class, MailProperties::class)
|
||||
@SpringBootApplication
|
||||
class MessengerApplication : ApplicationRunner {
|
||||
private val log = LoggerFactory.getLogger(MessengerApplication::class.java)
|
||||
|
||||
@Resource
|
||||
private lateinit var testProperties: TestProperties
|
||||
|
||||
@Resource
|
||||
private lateinit var driverProperties: DriverProperties
|
||||
|
||||
@Resource
|
||||
private lateinit var runner: OdsScreenshotRunner
|
||||
|
||||
override fun run(args: ApplicationArguments?) {
|
||||
log.info("Target path: {}", driverProperties.targetPath)
|
||||
if (driverProperties.targetPath.isBlank()) {
|
||||
@@ -41,6 +53,12 @@ class MessengerApplication : ApplicationRunner {
|
||||
if (!File(driverProperties.targetPath).exists()) {
|
||||
throw RuntimeException("Target path doesn't exist")
|
||||
}
|
||||
|
||||
if (testProperties.test) {
|
||||
log.info("Test start")
|
||||
runner.run()
|
||||
log.info("Test end")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,22 +5,31 @@ import org.openqa.selenium.chrome.ChromeDriver
|
||||
import org.openqa.selenium.chrome.ChromeDriverService
|
||||
import org.openqa.selenium.chrome.ChromeOptions
|
||||
|
||||
fun chrome(driverProperties: DriverProperties, handler: (ChromeDriver) -> Unit) {
|
||||
fun chrome(
|
||||
testProperties: TestProperties,
|
||||
driverProperties: DriverProperties,
|
||||
handler: (ChromeDriver) -> Unit,
|
||||
) {
|
||||
val driver = ChromeDriver(
|
||||
ChromeDriverService.Builder()
|
||||
ChromeDriverService
|
||||
.Builder()
|
||||
.usingDriverExecutable(File(driverProperties.driverPath))
|
||||
.build(),
|
||||
ChromeOptions().apply {
|
||||
setBinary(driverProperties.binaryPath)
|
||||
addArguments(
|
||||
val arguments = mutableListOf(
|
||||
"--disable-gpu",
|
||||
// "--disable-javascript",
|
||||
"--disable-plugins",
|
||||
"-–disable-images",
|
||||
"--no-sandbox",
|
||||
"--headless",
|
||||
"--no-sandbox",
|
||||
"blink-settings=imagesEnabled=false"
|
||||
)
|
||||
if (testProperties.test) {
|
||||
arguments.remove("--headless")
|
||||
}
|
||||
addArguments(arguments)
|
||||
}
|
||||
)
|
||||
try {
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.messenger.runner
|
||||
|
||||
import com.lanyuanxiaoyao.messenger.DriverProperties
|
||||
import com.lanyuanxiaoyao.messenger.MailProperties
|
||||
import com.lanyuanxiaoyao.messenger.TestProperties
|
||||
import com.lanyuanxiaoyao.messenger.chrome
|
||||
import com.lanyuanxiaoyao.messenger.sender.LocalFileSender
|
||||
import com.lanyuanxiaoyao.messenger.sender.MailSender
|
||||
@@ -22,6 +23,7 @@ import org.springframework.stereotype.Service
|
||||
|
||||
@Service
|
||||
class OdsScreenshotRunner(
|
||||
private val testProperties: TestProperties,
|
||||
private val driverProperties: DriverProperties,
|
||||
private val javaMailSender: JavaMailSender,
|
||||
private val mailProperties: MailProperties,
|
||||
@@ -36,7 +38,7 @@ class OdsScreenshotRunner(
|
||||
|
||||
@Scheduled(cron = "30 0 0 * * ?")
|
||||
override fun run() {
|
||||
chrome(driverProperties) { driver ->
|
||||
chrome(testProperties, driverProperties) { driver ->
|
||||
driver.manage().window().size = Dimension(2560, 1440)
|
||||
driver.get("http://132.126.207.124:8686/udal-manager/toLogin")
|
||||
WebDriverWait(driver, 10.seconds.toJavaDuration()).until { driver.findElement(By.id("loginForm")) }
|
||||
@@ -81,8 +83,10 @@ class OdsScreenshotRunner(
|
||||
|
||||
val targetFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss")
|
||||
val data = driver.getScreenshotAs(OutputType.BYTES)
|
||||
if (!testProperties.test) {
|
||||
LocalFileSender(driverProperties.targetPath).send("${targetFormatter.format(now)}.png", data)
|
||||
MailSender(mailProperties, javaMailSender).send("ODS 监控截图 ${targetFormatter.format(now)}", data)
|
||||
}
|
||||
MailSender(testProperties, mailProperties, javaMailSender).send("ODS 监控截图 ${targetFormatter.format(now)}", data)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.lanyuanxiaoyao.messenger.sender
|
||||
|
||||
import com.lanyuanxiaoyao.messenger.MailProperties
|
||||
import com.lanyuanxiaoyao.messenger.TestProperties
|
||||
import jakarta.activation.DataHandler
|
||||
import jakarta.mail.Message
|
||||
import jakarta.mail.Part
|
||||
@@ -13,7 +14,11 @@ import java.net.SocketException
|
||||
import org.springframework.mail.javamail.JavaMailSender
|
||||
import org.springframework.retry.support.RetryTemplate
|
||||
|
||||
class MailSender(private val mailProperties: MailProperties, private val javaMailSender: JavaMailSender) : Sender {
|
||||
class MailSender(
|
||||
private val testProperties: TestProperties,
|
||||
private val mailProperties: MailProperties,
|
||||
private val javaMailSender: JavaMailSender,
|
||||
) : Sender {
|
||||
override fun send(title: String, content: ByteArray) {
|
||||
RetryTemplate.builder()
|
||||
.withTimeout(600000)
|
||||
@@ -24,7 +29,11 @@ class MailSender(private val mailProperties: MailProperties, private val javaMai
|
||||
javaMailSender.send(
|
||||
javaMailSender.createMimeMessage().also { message ->
|
||||
message.subject = title
|
||||
if (testProperties.test) {
|
||||
message.addRecipient(Message.RecipientType.TO, InternetAddress("lanyuanxiaoyao@qq.com"))
|
||||
} else {
|
||||
message.addRecipients(Message.RecipientType.TO, mailProperties.targets.map { InternetAddress(it) }.toTypedArray())
|
||||
}
|
||||
message.setContent(MimeMultipart().also { multipart ->
|
||||
multipart.addBodyPart(MimeBodyPart().also { mimeBodyPart ->
|
||||
mimeBodyPart.setContent(
|
||||
|
||||
@@ -15,6 +15,7 @@ logging:
|
||||
pattern:
|
||||
console: '%date{MM-dd HH:mm:ss} %-5level [%t] %C{35}: %msg%n%throwable'
|
||||
messenger:
|
||||
test: true
|
||||
driver:
|
||||
driver-path: /Users/lanyuanxiaoyao/Downloads/chromium/134/macOS-1345775/chromedriver
|
||||
binary-path: /Users/lanyuanxiaoyao/Downloads/chromium/134/macOS-1345775/Chromium.app/Contents/MacOS/Chromium
|
||||
|
||||
Reference in New Issue
Block a user