/src/test/java/com/onresolve/jira/groovy/selenium/ScriptRunnerSeleniumTests.groovy
Groovy | 153 lines | 103 code | 34 blank | 16 comment | 4 complexity | a62c4e2deb5994da8853c357b420adcb MD5 | raw file
- package com.onresolve.jira.groovy.selenium;
- import org.openqa.selenium.remote.DesiredCapabilities
- import org.openqa.selenium.remote.RemoteWebDriver
- import com.atlassian.jira.util.json.JSONObject
- import java.util.concurrent.TimeUnit
- import org.apache.log4j.Category
- import org.apache.log4j.Level
- import org.apache.log4j.Logger
- import org.junit.*
- import static org.junit.Assert.assertEquals
- import org.openqa.selenium.*
- import static org.junit.Assert.assertTrue
- import org.openqa.selenium.htmlunit.HtmlUnitDriver
- import com.onresolve.jira.groovy.PackageScanner
- import static org.junit.Assert.assertNull
- public class ScriptRunnerSeleniumTests
- {
- static WebDriver driver
- private static String baseUrl = "http://localhost:8080/jira";
- static String credential = "&os_username=admin&os_password=admin"
- static Category log = Category.getInstance(ScriptRunnerSeleniumTests.class)
- /*
- Note that each test is dependent on the ones before it
- */
- ScriptRunnerSeleniumTests() {
- Logger.getLogger(ScriptRunnerSeleniumTests).setLevel(Level.DEBUG);
- }
- @Before
- public void before() {
- // startBrowser()
- }
- @After
- public void after() {
- // driver.close()
- }
- @BeforeClass
- public static void setUp() throws Exception {
- startBrowser()
- }
- @AfterClass
- public static void tearDown() {
- // deleteAllGridCFs()
- driver.close()
- }
- private static def startBrowser() {
- System.setProperty("webdriver.chrome.driver", "C:\\Users\\jechlin\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe")
- driver = new RemoteWebDriver(new URL("http://localhost:9515"), DesiredCapabilities.chrome());
- // driver = new HtmlUnitDriver(enableJavascript: true)
- // Test with firefox
- // driver = new FirefoxDriver()
- // (driver as JavascriptExecutor).executeScript("window.resizeTo(1200,800)");
- driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS)
- }
- @Test
- public void testScriptRunnerAdminPanel() {
- driver.get(baseUrl + "/secure/admin/groovy/GroovyRunner.jspa?$credential")
- def scriptLanguages = driver.findElements(By.cssSelector(("input[type='radio'][name='scriptLanguage']")))*.getAttribute("value")
- assertTrue(scriptLanguages.contains("ECMAScript"))
- assertTrue(scriptLanguages.contains("Groovy"))
- driver.findElement(By.id("scriptLanguage_Groovy")).click()
- runScript("import com.atlassian.jira.propertyset.JiraPropertySetFactory")
- assertEquals ("no return value", driver.findElement(By.id("scriptResult")).text)
- runScript("import x.y.z")
- assertTrue (driver.findElement(By.id("scriptException")).text.contains("MultipleCompilationErrorsException"))
- runScript("import com.onresolve.jira.groovy.GroovyService")
- assertEquals ("no return value", driver.findElement(By.id("scriptResult")).text)
- }
- @Test
- public void testAddGroovyService() {
- // add service
- driver.get(baseUrl + "/secure/admin/ViewServices!default.jspa?$credential")
- driver.findElement(By.id("serviceName")).sendKeys("TestGroovyService")
- driver.findElement(By.id("serviceClass")).sendKeys("com.onresolve.jira.groovy.GroovyService")
- driver.findElement(By.id("addservice_submit")).click()
-
- // edit service
- // bit stupid, need an absolute path, can't be bothered to think of anything more clever right now
- ["C:\\work\\groovyrunner\\src\\test\\resources\\examples\\testService.groovy", "C:\\work\\projects\\groovyrunner\\src\\test\\resources\\examples\\testService.groovy"].each {
- if (new File(it).exists()) {
- driver.findElement(By.name("input-file")).sendKeys(it)
- }
- }
- def serviceId = driver.currentUrl.replaceAll(".*id=", "")
- log.debug("add service with id: $serviceId")
- driver.findElement(By.id("update_submit")).click()
- Thread.sleep(2000)
- // todo: check there were no errors
- // todo: remove the service
- }
- @Test
- public void testInJiraUnitTests() {
- driver.get(baseUrl + "/secure/admin/groovy/CannedScriptRunner.jspa?$credential")
- driver.findElement(By.linkText("Run unit tests")).click()
- def classesForPackage = PackageScanner.getClassesForPackage("com.onresolve.jira.groovy.test")
- // classesForPackage = ["com.onresolve.jira.groovy.test.TestCopyCustomField"]
- boolean hasFailedTests = false
- classesForPackage.each {String fileName ->
- def clsName = fileName.replaceAll(/\//, ".").replaceAll(/\.groovy$/, "")
- if (clsName.contains(".Test")) {
- driver.findElement(By.name("cannedScriptArgs_FIELD_CLASS_NAMES")).sendKeys(clsName)
- driver.findElement(By.name("RunCanned")).click()
- if (driver.pageSource.contains("testsFailed")) {
- log.error("Test $clsName failed.")
- hasFailedTests = true
- }
- else {
- log.debug("Test $clsName passed.")
- }
- }
- }
- assert !hasFailedTests
- }
- private runScript(String thescript) {
- setScript(thescript)
- driver.findElement(By.name("Run now")).click()
- }
- private setScript(String theScript) {
- (driver as JavascriptExecutor).executeScript("codeMirrorObjs[0].setValue(\"$theScript\")")
- }
- }