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.
// 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
Start "Chrome"
: This command launches the Chrome browser.TypeText "http://www.example.com", Return
: This types the URL into the browser's address bar and presses Enter.WaitFor 10, "ExamplePageImage"
: Waits up to 10 seconds for a specific image (which you've captured and named "ExamplePageImage") to appear on the screen.If ImageFound("ExamplePageImage")
: Checks if the expected image is found on the page, logging success or failure accordingly.// Declare a variable
put "Hello, World!" into greeting
// Use the variable
Log greeting
put "Hello, World!" into greeting
: This creates a variable named greeting
and assigns it the value "Hello, World!".Log greeting
: This logs the value of greeting
to the output.// 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
put 10 into number
if number > 5
Log "Number is greater than 5"
else
Log "Number is 5 or less"
end if
if number > 5
: This checks whether the value stored in number
is greater than 5.Log
: Depending on the condition, either "Number is greater than 5" or "Number is 5 or less" is logged to the console.else
: This block runs if the if
condition is not met.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.
// 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.
// Define a function
function addNumbers x, y
return x + y
end function
// Use the function
put addNumbers(5, 7) into result
Log result
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.// Try block with potential error
try
put 5 / 0 into result
catch
LogError "An error occurred: " & the exception
end try
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.// Define the file path
put "/path/to/file.txt" into filePath
// Read the file contents
put file filePath into fileContents
Log fileContents
// Define the file path
put "/path/to/file.txt" into filePath
// Write to the file
write "Hello, SenseTalk!" to filePath
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.