Skip to content

Instantly share code, notes, and snippets.

@h-mochizuki
Created November 7, 2019 05:07
Show Gist options
  • Save h-mochizuki/67b77e041538c0e6ea9a04052f42debc to your computer and use it in GitHub Desktop.
Save h-mochizuki/67b77e041538c0e6ea9a04052f42debc to your computer and use it in GitHub Desktop.
GebでSlackに投稿するやつ
apply from: "slack-report.gradle"
slack {
workspace = "your-slack-workspace"
channel = ~/^your-channel/
email = "example@example.com"
password = "yourpassword"
template = { "test" }
}
apply plugin: SlackReportPlugin
buildscript {
repositories { mavenCentral() }
dependencies {
classpath("org.gebish:geb-core:3.+") { exclude group: "org.codehaus.groovy" }
classpath "org.seleniumhq.selenium:selenium-chrome-driver:3.+"
classpath "io.github.bonigarcia:webdrivermanager:3.+"
}
}
import geb.Browser
import geb.driver.CachingDriverFactory
import geb.navigator.Locator
import io.github.bonigarcia.wdm.WebDriverManager
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
class SlackReportPluginConfiguration {
/** ヘッドレスモード起動 */
boolean headless = false
/** ワークスペース名 */
String workspace = null
/** チャンネル名(文字列 or 正規表現) */
def channel = null
/** メールアドレス */
String email = null
/** パスワード */
String password = null
/** 投稿内容テンプレート(文字列 or Closure) */
def template = null
// def template = """${new Date().format("yyyy/MM/dd")}
// | 自動投稿のテスト
// |""".stripMargin()
/** 投稿内容を返します */
final String getFixText() {
if (template instanceof Closure) {
template.delegate = this
template()
} else {
template
}
}
/** 入力チェックします */
final void validate() {
List params = []
workspace || params << "workspace"
channel || params << "channel"
email || params << "email"
password || params << "password"
fixText || params << "template"
if (params) throw new GradleException("Please input params: ${params}")
}
}
class SlackReportPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.extensions.create('slack', SlackReportPluginConfiguration);
project.task('report') {
doFirst { report(project) }
}
}
static def chromeOptions(Project project) {
ChromeOptions options = new ChromeOptions()
if (project?.slack?.headless) {
options.addArguments("--headless")
options.addArguments("--disable-gpu")
}
options
}
static def report(Project project) {
project.slack.validate()
CachingDriverFactory.clearCacheAndQuitDriver()
WebDriverManager.chromedriver().setup()
Browser browser = new Browser()
try {
browser.with {
driver = new ChromeDriver(chromeOptions(project))
go "https://${project.slack.workspace}.slack.com"
waitFor(message: "ログイン画面が表示できません") {
$("#email").displayed && $("#password").displayed
}
$("#email").value("${project.slack.email}")
$("#password").value("${project.slack.password}")
$("#signin_btn").click()
waitFor(message: "Slackタイムラインが表示できません") {
$(".p-channel_sidebar__static_list").$("a", "aria-label": startsWith(project.slack.channel)).displayed
}
$(".p-channel_sidebar__static_list").$("a", "aria-label": startsWith(project.slack.channel)).click()
Thread.sleep(500)
js.exec(project.slack?.fixText, "document.querySelector('.ql-editor p').textContent = arguments[0];")
$(".p-message_pane_input").$("button", textContent: contains(~/送信/)).click()
Thread.sleep(3000)
}
} catch (Exception e) {
e.printStackTrace()
} finally {
browser?.quit()
CachingDriverFactory.clearCacheAndQuitDriver()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment