PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 1ms

/src/test/java/com/onresolve/jira/groovy/selenium/ScriptRunnerSeleniumTests.groovy

https://bitbucket.org/sorin/jira-plugin-intellij
Groovy | 153 lines | 103 code | 34 blank | 16 comment | 4 complexity | a62c4e2deb5994da8853c357b420adcb MD5 | raw file
  1. package com.onresolve.jira.groovy.selenium;
  2. import org.openqa.selenium.remote.DesiredCapabilities
  3. import org.openqa.selenium.remote.RemoteWebDriver
  4. import com.atlassian.jira.util.json.JSONObject
  5. import java.util.concurrent.TimeUnit
  6. import org.apache.log4j.Category
  7. import org.apache.log4j.Level
  8. import org.apache.log4j.Logger
  9. import org.junit.*
  10. import static org.junit.Assert.assertEquals
  11. import org.openqa.selenium.*
  12. import static org.junit.Assert.assertTrue
  13. import org.openqa.selenium.htmlunit.HtmlUnitDriver
  14. import com.onresolve.jira.groovy.PackageScanner
  15. import static org.junit.Assert.assertNull
  16. public class ScriptRunnerSeleniumTests
  17. {
  18. static WebDriver driver
  19. private static String baseUrl = "http://localhost:8080/jira";
  20. static String credential = "&os_username=admin&os_password=admin"
  21. static Category log = Category.getInstance(ScriptRunnerSeleniumTests.class)
  22. /*
  23. Note that each test is dependent on the ones before it
  24. */
  25. ScriptRunnerSeleniumTests() {
  26. Logger.getLogger(ScriptRunnerSeleniumTests).setLevel(Level.DEBUG);
  27. }
  28. @Before
  29. public void before() {
  30. // startBrowser()
  31. }
  32. @After
  33. public void after() {
  34. // driver.close()
  35. }
  36. @BeforeClass
  37. public static void setUp() throws Exception {
  38. startBrowser()
  39. }
  40. @AfterClass
  41. public static void tearDown() {
  42. // deleteAllGridCFs()
  43. driver.close()
  44. }
  45. private static def startBrowser() {
  46. System.setProperty("webdriver.chrome.driver", "C:\\Users\\jechlin\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe")
  47. driver = new RemoteWebDriver(new URL("http://localhost:9515"), DesiredCapabilities.chrome());
  48. // driver = new HtmlUnitDriver(enableJavascript: true)
  49. // Test with firefox
  50. // driver = new FirefoxDriver()
  51. // (driver as JavascriptExecutor).executeScript("window.resizeTo(1200,800)");
  52. driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS)
  53. }
  54. @Test
  55. public void testScriptRunnerAdminPanel() {
  56. driver.get(baseUrl + "/secure/admin/groovy/GroovyRunner.jspa?$credential")
  57. def scriptLanguages = driver.findElements(By.cssSelector(("input[type='radio'][name='scriptLanguage']")))*.getAttribute("value")
  58. assertTrue(scriptLanguages.contains("ECMAScript"))
  59. assertTrue(scriptLanguages.contains("Groovy"))
  60. driver.findElement(By.id("scriptLanguage_Groovy")).click()
  61. runScript("import com.atlassian.jira.propertyset.JiraPropertySetFactory")
  62. assertEquals ("no return value", driver.findElement(By.id("scriptResult")).text)
  63. runScript("import x.y.z")
  64. assertTrue (driver.findElement(By.id("scriptException")).text.contains("MultipleCompilationErrorsException"))
  65. runScript("import com.onresolve.jira.groovy.GroovyService")
  66. assertEquals ("no return value", driver.findElement(By.id("scriptResult")).text)
  67. }
  68. @Test
  69. public void testAddGroovyService() {
  70. // add service
  71. driver.get(baseUrl + "/secure/admin/ViewServices!default.jspa?$credential")
  72. driver.findElement(By.id("serviceName")).sendKeys("TestGroovyService")
  73. driver.findElement(By.id("serviceClass")).sendKeys("com.onresolve.jira.groovy.GroovyService")
  74. driver.findElement(By.id("addservice_submit")).click()
  75. // edit service
  76. // bit stupid, need an absolute path, can't be bothered to think of anything more clever right now
  77. ["C:\\work\\groovyrunner\\src\\test\\resources\\examples\\testService.groovy", "C:\\work\\projects\\groovyrunner\\src\\test\\resources\\examples\\testService.groovy"].each {
  78. if (new File(it).exists()) {
  79. driver.findElement(By.name("input-file")).sendKeys(it)
  80. }
  81. }
  82. def serviceId = driver.currentUrl.replaceAll(".*id=", "")
  83. log.debug("add service with id: $serviceId")
  84. driver.findElement(By.id("update_submit")).click()
  85. Thread.sleep(2000)
  86. // todo: check there were no errors
  87. // todo: remove the service
  88. }
  89. @Test
  90. public void testInJiraUnitTests() {
  91. driver.get(baseUrl + "/secure/admin/groovy/CannedScriptRunner.jspa?$credential")
  92. driver.findElement(By.linkText("Run unit tests")).click()
  93. def classesForPackage = PackageScanner.getClassesForPackage("com.onresolve.jira.groovy.test")
  94. // classesForPackage = ["com.onresolve.jira.groovy.test.TestCopyCustomField"]
  95. boolean hasFailedTests = false
  96. classesForPackage.each {String fileName ->
  97. def clsName = fileName.replaceAll(/\//, ".").replaceAll(/\.groovy$/, "")
  98. if (clsName.contains(".Test")) {
  99. driver.findElement(By.name("cannedScriptArgs_FIELD_CLASS_NAMES")).sendKeys(clsName)
  100. driver.findElement(By.name("RunCanned")).click()
  101. if (driver.pageSource.contains("testsFailed")) {
  102. log.error("Test $clsName failed.")
  103. hasFailedTests = true
  104. }
  105. else {
  106. log.debug("Test $clsName passed.")
  107. }
  108. }
  109. }
  110. assert !hasFailedTests
  111. }
  112. private runScript(String thescript) {
  113. setScript(thescript)
  114. driver.findElement(By.name("Run now")).click()
  115. }
  116. private setScript(String theScript) {
  117. (driver as JavascriptExecutor).executeScript("codeMirrorObjs[0].setValue(\"$theScript\")")
  118. }
  119. }