代码
src/main/groovy/org/zbq/Main.groovy
package org.zbq
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.support.ui.WebDriverWait
import org.openqa.selenium.NoSuchElementException
import org.openqa.selenium.InvalidElementStateException
import static org.openqa.selenium.support.ui.ExpectedConditions.*
CWD = new File(getClass().protectionDomain.codeSource.location.path).parent
def getBrowser() {
def browser = null
def props = new Properties()
props.load(new FileInputStream("webdriver.properties"))
def browserName = props.getProperty("default.browser")
if("firefox".equals(browserName)) {
def firefoxDriver = props.getProperty("webdriver.gecko.driver")
System.setProperty("webdriver.gecko.driver", firefoxDriver)
// about:config
FirefoxOptions options = new FirefoxOptions()
.addPreference("browser.download.dir", CWD)
.addPreference("browser.download.folderList", 2)
.addPreference("browser.download.manager.showWhenStarting", false)
.addPreference("browser.helperApps.neverAsk.saveToDisk", "application/vnd.ms-excel,application/zip,");
browser = new FirefoxDriver(options)
} else if("chrome".equals(browserName)) {
def chromeDriver = props.getProperty("webdriver.chrome.driver")
System.setProperty("webdriver.chrome.driver", chromeDriver)
ChromeOptions options = new ChromeOptions()
def prefs = new HashMap();
prefs.put("download.default_directory", CWD)
prefs.put("download.prompt_for_download", 0);
prefs.put("profile.default_content_settings.popups", 0);
options.setExperimentalOption("prefs", prefs);
browser = new ChromeDriver(options)
}
return browser
}
def cleanWS() {
def excel = new File(CWD + File.separator + "cmdb_ci_hardware.xls")
if(excel.exists()) {
excel.delete()
}
}
def main() {
cleanWS()
browser = getBrowser()
def url = "http://www.baidu.com"
browser.get(url)
// def wait = new WebDriverWait(browser, 10)
println browser.getTitle()
// System.exit(0)
browser.manage().window().maximize()
browser.findElementById('kw').sendKeys('ost')
def searchButton = browser.findElementById('su')
searchButton.click()
browser.quit()
}
main()
webdriver.properties
default.browser=firefox
webdriver.gecko.driver=C:\\Users\\u1\\browser\\webdriver\\geckodriver.exe
webdriver.chrome.driver=C:\\Users\\u1\\browser\\webdriver\\chromedriver.exe
apply plugin: 'groovy'
apply plugin: 'application'
mainClassName = "org.zbq.Main"
repositories {
mavenCentral()
}
// In this section you declare the dependencies for your production and test code
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.4.8'
compile 'org.seleniumhq.selenium:selenium-java:3.11.0'
testCompile 'junit:junit:4.12'
}
task runScript(type: JavaExec) {
description 'Run Groovy script'
// Set main property to name of Groovy script class.
main = mainClassName
// Set classpath for running the Groovy script.
classpath = sourceSets.main.runtimeClasspath
if (project.hasProperty('custom')) {
// Pass command-line argument to script.
args project.getProperty('custom')
}
}
// Use this task to generate the stand-alone executable jar of groovy
task uberjar( type: Jar, dependsOn: [':compileJava',':compileGroovy'] ) {
manifest {
attributes 'Main-Class': mainClassName
}
// Our groovy classes
from files(sourceSets.main.output.classesDirs)
// All groovy classes
from configurations.runtime.asFileTree.files.collect { zipTree(it) }
}
defaultTasks 'runScript'
浏览器驱动下载
- chromedriver: http://chromedriver.chromium.org/downloads
- geckodriver: https://github.com/mozilla/geckodriver/releases
运行
gradle build
打jar包
gradle uberjar