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