メインコンテンツまでスキップ
バージョン:23.4

Gathering and Using Data

Reading Text on the SUT

The ReadText() and ReadTable() functions use the Eggplant Functional OCR engine to read and return the text in a given area on the SUT. The area can be within a rectangle (determined by two diagonal points), or near a single point.

For simplicity, the following information refers to the ReadText() function. This information applies to the ReadTable() function as well.

Reading text within a rectangle

When you call the ReadText() function with a rectangle parameter, the rectangle absolutely limits the returned text value. That is, if a line of text extends beyond the edges of the text rectangle, the overflow text is not returned.

Like other rectangles in Eggplant Functional, the text rectangle is determined by two diagonal points. Each point can be given as an image name (which represents the location where that image is found) or any other coordinate value.

Example:

Set address to ReadText (("AddressField", "EndAddressField"))
// Sets the address variable to the text in the given rectangle. The diagonal points of
// the rectangle are the location of the AddressField image, the location of the
// EndAddressField image.

Reading text near a point

When you call the ReadText() function with a single point parameter, the OCR engine attempts to find a line of text that includes your point, or a line of text that begins near that point.

The point can be given as an image name (which represents the location where that image is found) or any other coordinate value.

Using the SUT clipboard

If the text you want to read can be selected and copied, copy the text to the SUT clipboard, then return the clipboard contents through the RemoteClipboard() function.

Example:

Click "SomeTextField"
TypeText CommandKey, "a" // Selects All in the text field.
TypeText CommandKey, "c" // Copies text to the clipboard.
put RemoteClipboard(5) // Waits up to 5 seconds to return the clipboard content generated by previous remote command; shows that content.
ノート

Note: On Mac OS X systems, VNC servers running outside of any user account (system servers) cannot transfer clipboard contents. If you want to have access to the SUT clipboard, make sure that you are connected to a VNC server within the active user account.

Data-Driven Testing

One of the great benefits of test automation is the ability to run tests repeatedly using different data values. One common approach is to store the data in a text file, then read values from that file during the course of a script. The chunk expressions and direct file access in SenseTalk make this a straightforward task with a formatted text file.

For example, to test a calculator application, you could pass in a text file with number values separated by commas.

| Example: Calculator Test Data File | | | --- | | 1, 2, 2 | | | 3, 4, 12 | | | 5, 6, 30 | |

The following script drives the calculator to multiply the first two values on each line of the file. Then, it returns the products and validates them by comparing them to the third values.

Example:

repeat with theData = each line of file "CalculatorData.txt"
TypeText item 1 of theData // Enters 1st value from the current line of the data file
Click "Multiply Button"
TypeText item 2 of theData // Enters 2nd value from the current line of the data file
Click "Equals Button"
Click “OutputField”
TypeText CommandKey, "a" // Selects All in the output field
TypeText CommandKey, "c" // Copies text to the SUT clipboard
put remoteClipboard() into Answer // Puts clipboard contents into a variable
if (Answer is not equal to item 3 of theData) then // Compares results with 3rd value in the data file
LogError "Got: " & theAnswer & ", " & item 1 of theData & " x " & item 2 of theData & "should be " & item 3 of theData // Logs discrepancy as an error:
end if
end repeat

Creating a Data File

The data you use in your scripts might come from a spreadsheet as well as a text file. Spreadsheet programs usually have an option to export a file in CSV (comma-separated values) format, or as tab-delimited text.

You can also pass the data directly in a script, as shown in the following example. The script in this example creates a test data file with three numbers on each line. The third number is the product of the first two. *

Example:

set file "/tmp/CalculatorData.txt" to {{
1,2,2
3,4,12
5,6,30
}}
ノート

Note: SenseTalk makes it easy to read or write the entire contents of a file by using the word file, followed by the filename. See the filename in the example above. To be sure you're working with the right file, it is a good idea to use the file's full pathname. For more information, see the page File and Folder Interaction.

Reading and Validating a Data File

The following example shows a script that reads a returned data file.

Example:

repeat with theData = each line of file "/tmp/CalculatorData.txt" // Assigns the contents of each line to theData.txt in turn.
put repeatIndex() & ": " & theData // Counts the number of times the loop is repeated; writes this value before each line.
end repeat

If you run the script at this point, Data.txt returns like this:

1: 1,2,22: 3,4,123: 5,6,30

Now that the script can read the data file, the next step is to verify that the values on each line contain useful information a shown in the following example:

Example:

put zero into goodCount
put zero into badCount
repeat with theData = each line of file "/tmp/CalculatorData.txt" // For each line of the /tmp/ file...
put item 1 of theData into num1 // Puts the three numbers from each line into three different variables.
put item 2 of theData into num2
put item 3 of theData into product
if product = num1 * num2 then // Compares the 3rd number to the product of the first two...
add 1 to GoodCount // then increments GoodCount if they are equal;
else
put "Bad Data at line " & repeatIndex() & ": " & theData // If they are not equal, displays a "Bad data" message,
add 1 to BadCount // and increments BadCount
end if
end repeat
put "Good data lines: " & GoodCount // Returns the GoodCount value
put "Bad data lines: " & BadCount // Returns the BadCount value
ノート

Note: The terms line and item in this script are SenseTalk "chunk expressions" that refer to portions of text. Items are normally delimited by commas, however, you can specify any other characters as delimiters by setting the itemDelimiter local property. For more information about chunk expressions and the use of different delimiters, see Chunk Expressions.

Timing Script Events

This section defines some of the functions useful for timing script events, and provides an example of how you can use them in script logging. For more information, see the Date and Time Values in SenseTalk.

  • The Date: Returns the current date
  • The Time: Returns the current time of day
  • The Seconds: Returns the whole number of seconds since January 1, 2001

The following script shows a time-logging example:

Example:

log "Starting timed task at" && the time && "on" && the date // Logs the script start time and date.
put the time into startTime // (* put any code here that you want to time *)
put the time into stopTime // (* put any code here that you want to time *)
log "That took" && stopTime - startTime &&"seconds to complete." // Logs the total time of the script execution.

If you run the above code, look for the following output:

2002-07-16 14:33:36 -0600log Starting timed task at 02:33 PM on 07/16/02

2002-07-16 14:33:39 -0600log That took 2.500326 seconds to complete.