groovy 通过 selenium 操作浏览器

代码

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<String, Object>();
        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'

浏览器驱动下载

  1. chromedriver: http://chromedriver.chromium.org/downloads
  2. geckodriver: https://github.com/mozilla/geckodriver/releases

运行

gradle build

打jar包

gradle uberjar

转载于:https://my.oschina.net/u/3845788/blog/1835491

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值