Skip to content

Instantly share code, notes, and snippets.

@acetousk
Created May 26, 2020 03:53
Show Gist options
  • Save acetousk/371e0d44372b0b9adbd5f8c367e82984 to your computer and use it in GitHub Desktop.
Save acetousk/371e0d44372b0b9adbd5f8c367e82984 to your computer and use it in GitHub Desktop.
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.*
import org.openqa.selenium.interactions.Actions
import org.openqa.selenium.support.ui.*
import spock.lang.*
import java.time.Duration
class KatalonGroovyGenerator extends Specification{
@Shared WebDriver driver = new FirefoxDriver()
@Shared Actions actions = new Actions(driver)
@Shared Select select
@Shared double defaultWaitTimeInSeconds = 1
//runs only before the first test
def setupSpec(){
System.println("Start Framework Browser Tests")
System.println("\tImporting Gecko Driver (if this fails you may need to change the path in the code)")
//for different installations the path needs to change
//System.setProperty("webdriver.gecko.driver", "/home/acetousk/dev/java/moqui-selenium-test/geckodriver")
}
//runs only after the last test
def cleanupSpec(){
System.println("Browser Tests Done!")
driver.quit()
}
//run before each test
def setup(){}
//run after each test
def cleanup(){}
def "first test"(){
when:
open("https://moqui.org")
clickAndWait(By.)
then:
}
//waits
WebElement wait(double time = defaultWaitTimeInSeconds ){
try{
return new WebDriverWait(driver, Duration.ofSeconds(time).getSeconds())
}catch(Exception e){
e.printStackTrace()
}
}
WebElement waitUntil(ExpectedConditions conditions, double time = defaultWaitTimeInSeconds){
try{
return new WebDriverWait(driver, Duration.ofSeconds(time).getSeconds())
.until(conditions)
}catch(Exception e){
e.printStackTrace()
}
}
//action and wait
//click
def clickAndWait(By element){
waitUntil(ExpectedConditions.elementToBeClickable(element)).click()
}
def click(By element){
clickAndWait(element)
}
//double click
def doubleClickAndWait(By element){
actions.doubleClick(waitUntil(ExpectedConditions.elementToBeClickable(element))).perform()
}
def doubleClick(By element){
doubleClickAndWait(element)
}
//select
//TODO: check to see if this is actually works
def selectAndWait(By element, String visibleText){
select = new Select(waitUntil(ExpectedConditions.elementToBeSelected(element)))
select.selectByVisibleText(visibleText)
}
def select(By element, String visibleText){
selectAndWait(element, visibleText)
}
//sendKeys
def sendKeysAndWait(By element, String keys){
waitUntil(ExpectedConditions.elementToBeClickable(element)).sendKeys(keys)
}
def sendKeys(By element, String keys){
sendKeysAndWait(element,keys)
}
//submit
def submitAndWait(By element){
waitUntil(ExpectedConditions.elementToBeClickable(element)).submit()
}
def submit(By element){
submitAndWait(element)
}
//veryify / assert
//text
//TODO: does this work
boolean verifyText(By element, String text){
if(waitUntil(ExpectedConditions.invisibilityOfElementWithText(element,text)).getText().contains(text)){
return true
}else{
//TODO: throw error
return false
}
}
def assertText(By element, String text){
if(!verifyText(element,text)){
//TODO: throw error
}
}
//title
boolean verifyTitle(String text){
if(waitUntil(ExpectedConditions.titleContains(text))){
return true
}else{
//TODO: throw error
return false
}
}
def assertTitle(String text){
if(!verifyTitle(text)){
//TODO: throw error
}
}
//value
boolean verifyValue(By element, String attribute, String text){
if(waitUntil(ExpectedConditions.attributeContains(element,attribute,text))){
return true
}else{
//TODO: throw error
return false
}
}
def assertValue(By element, String attribute, String text){
if(!verifyValue(element,attribute,text)){
//TODO: throw error
}
}
//wait for
//alert
def waitForAlertPresent(By element){
waitUntil(ExpectedConditions.alertIsPresent())
}
def waitForAlertNotPresent(By element){
waitUntil(!ExpectedConditions.alertIsPresent())
}
//element
def waitForElementPresent(By element){
waitUntil(ExpectedConditions.presenceOfElementLocated(element))
}
def waitForElementNotPresent(By element){
waitUntil(ExpectedConditions.stalenessOf(element))
}
//text
def waitForTextPresent(By element, String text){
waitUntil(ExpectedConditions.textToBePresentInElementLocated(element,text))
}
def waitForTextNotPresent(By element, String text){
waitUntil(ExpectedConditions.not(ExpectedConditions.textToBePresentInElementLocated(element,text)))
}
//value
def waitForValue(By element, String text){
waitUntil(ExpectedConditions.textToBePresentInElementValue(element,text))
}
def waitForNotValue(By element, String text){
waitUntil(ExpectedConditions.not(ExpectedConditions.textToBePresentInElementValue(element,text)))
}
//visible
def waitForVisible(By element){
waitUntil(ExpectedConditions.visibilityOf(element))
}
def waitForNotVisible(By element){
waitUntil(ExpectedConditions.invisibilityOf(element))
}
//random (self described)
def open(String target, String name="example name"){
try{
driver.get(target, name)
return name
}catch(Exception e){
e.printStackTrace()
}
}
def pause(double time){
wait(time)
}
def refresh(){
driver.navigate().refresh()
}
def selectWindow(){
int length = driver.getWindowHandles().size()
driver.switchTo(driver.getWindowHandles()[length-1])
}
def selectFrame(By element){
waitUntil(ExpectedConditions.visibilityOfElementLocated(element))
driver.switchTo().frame(element) //wrong syntax?
}
def goBack(){
driver.navigate().back()
}
def assertConfirmation(){
driver.switchTo().alert().accept()
}
}
/**
open | http://localhost:8080/Login |
click | id=TestLoginLink_button |
click | //div[@id='content']/div/div/div/div/a[5]/i |
click | link=Data Import |
click | name=dummyFks |
click | name=useTryInsert |
click | id=ImportData_timeout |
type | id=ImportData_timeout | 30
click | //form[@id='ImportData']/fieldset/div/div |
click | //form[@id='ImportData']/fieldset |
click | id=ImportData_types |
click | id=ImportData_components |
click | link=Resource Location |
click | link=XML Text |
click | link=JSON Text |
click | link=CSV Text |
click | id=ImportData_checkOnly |
click | xpath=(//button[@type='button'])[4] |
click | xpath=(//a[contains(text(),'Entity')])[2] |
click | link=Speed Test |
click | id=SelectBaseCalls_submitButton |
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment