SenseTalk Tutorial Series

Chapter 1: Getting Started with SenseTalk

Setting Up Your Environment

Before writing your first script, ensure that you have Eggplant Functional installed. This tutorial assumes that you have a basic understanding of how to navigate the Eggplant Functional interface.

Your First SenseTalk Script

// Open the browser
Start "Chrome"

// Navigate to a URL
TypeText "http://www.example.com", Return

// Wait for the page to load
WaitFor 10, "ExamplePageImage"

// Validate that the correct page has loaded
If ImageFound("ExamplePageImage")
    Log "Page loaded successfully!"
Else
    LogError "Failed to load the page."
End If

Explanation:

Chapter 2: Variables and Data Handling in SenseTalk

Declaring and Using Variables

// Declare a variable
put "Hello, World!" into greeting

// Use the variable
Log greeting

Explanation:

Working with Lists and Property Lists

// Create a list
put ("apple", "banana", "cherry") into fruitList

// Access an item from the list
put fruitList's first item into firstFruit
Log firstFruit

// Create a property list
put (name:"John", age:30, occupation:"Tester") into person

// Access a property
Log person's name

Explanation:

Chapter 3: Control Structures in SenseTalk

Conditionals

put 10 into number

if number > 5
    Log "Number is greater than 5"
else
    Log "Number is 5 or less"
end if

Explanation:

Switch Statement

put "banana" into fruit

switch fruit
case “apple”
Log “It’s an apple.”
case “banana”
Log “It’s a banana.”
case “cherry”
Log “It’s a cherry.”
default
Log “It’s an unknown fruit.”
end switch

        

Explanation:

  • switch statement: Evaluates the value of fruit and runs the corresponding case block.
  • default case: Runs if none of the specified cases match the value of fruit.

Loops

Repeat with Each Loop:

put ("apple", "banana", "cherry") into fruitList

repeat with each fruit in fruitList
Log “Fruit: “ & fruit
end repeat

        

Explanation:

  • repeat with each: This loop iterates over each item in fruitList and logs it.

Repeat with a Condition:

put 5 into counter

repeat while counter > 0
Log “Counter is “ & counter
subtract 1 from counter
end repeat

        

Explanation:

  • repeat while: The loop continues as long as the condition (counter > 0) is true.
  • subtract 1 from counter: Decrements the counter by 1 in each iteration.

Repeat a Fixed Number of Times:

repeat 3 times
Log "This message will be logged three times."

end repeat

        

Explanation:

  • repeat X times: Repeats the enclosed block of code a fixed number of times.

Chapter 4: Advanced Topics

Image Recognition and Interaction

Clicking on an Image:

// Clicks on the image of a button

Click “SubmitButton”

        

Explanation:

  • Click "SubmitButton": This command searches for an image named "SubmitButton" and clicks on it. The image should be captured and stored in Eggplant Functional's suite.

Waiting for an Image:

// Waits for up to 15 seconds for the image to appear

WaitFor 15, “LoadingIcon”

        

Explanation:

  • WaitFor 15, "LoadingIcon": Pauses the script for up to 15 seconds, waiting for the image "LoadingIcon" to appear on the screen.

Verifying the Presence of an Image:

// Checks if an image is present on the screen

if ImageFound(“SuccessMessage”)
Log “Operation completed successfully.”
else
LogError “Operation failed.”
end if

        

Explanation:

  • ImageFound: This function returns true if the specified image is found on the screen, enabling conditional logic based on the presence of UI elements.

Working with External Data

Reading Data from a CSV File:

// Define the file path

set filePath to ”/path/to/data.csv”

// Read the CSV file
repeat with each record in file filePath
Log “Record: “ & record
end repeat

        

Using Data in a Test Script:

// Define the file path and read the data

set filePath to ”/path/to/users.csv”

repeat with each user in file filePath
put user’s username into username
put user’s password into password

            // Use the data to perform a login operation
TypeText username, Tab, password, Return
WaitFor 5, "Dashboard"

if ImageFound("Dashboard")
    Log "Login successful for " & username
else
    LogError "Login failed for " & username
end if

end repeat

        

Explanation:

  • CSV Data: The script reads usernames and passwords from a CSV file and uses them to perform login operations, verifying the outcome for each user.
Chapter 5: Functions in SenseTalk

Chapter 5: Functions in SenseTalk

Defining and Using Functions

// Define a function
function addNumbers x, y
    return x + y
end function

// Use the function
put addNumbers(5, 7) into result
Log result

Explanation:

  • function addNumbers x, y: This defines a new function called `addNumbers` that takes two arguments, `x` and `y`.
  • return x + y: The function returns the sum of `x` and `y`.
  • put addNumbers(5, 7) into result: Calls the function with arguments 5 and 7, and stores the result in the variable `result`.
  • Log result: Outputs the result of the function to the log.
Chapter 6: Handling Errors with Try/Catch

Chapter 6: Handling Errors with Try/Catch

Using Try/Catch for Error Handling

// Try block with potential error
try
    put 5 / 0 into result
catch
    LogError "An error occurred: " & the exception
end try

Explanation:

  • try: Starts a block of code that may throw an error.
  • put 5 / 0 into result: This line will cause a division by zero error.
  • catch: Catches the error and allows you to handle it.
  • LogError "An error occurred: " & the exception: Logs the error message.
Chapter 7: Working with Files

Chapter 7: Working with Files

Reading from a File

// Define the file path
put "/path/to/file.txt" into filePath

// Read the file contents
put file filePath into fileContents
Log fileContents

Writing to a File

// Define the file path
put "/path/to/file.txt" into filePath

// Write to the file
write "Hello, SenseTalk!" to filePath

Explanation:

  • put "/path/to/file.txt" into filePath: Specifies the path to the file.
  • put file filePath into fileContents: Reads the contents of the file and stores it in `fileContents`.
  • write "Hello, SenseTalk!" to filePath: Writes the string "Hello, SenseTalk!" to the specified file.