Read Data from Property File - Notes By ShariqSP
How to Read Data from Property Files in Android Mobile Application Testing (Using Eclipse)
Property files are key-value pairs commonly used for configuration settings, test data, or application behavior customization. In Android mobile testing, property files can be a lightweight and straightforward way to manage test data. This guide explains how to read data from property files in Eclipse for Android mobile testing.
Steps to Read Data from Property Files
-
Set Up the Environment:
- Ensure you have installed the Android Development Tools (ADT) plugin in Eclipse.
- Create or open your Android project in Eclipse.
-
Create a Property File:
- Right-click the
assets
folder in your Android project and selectNew
>File
. - Save the file with a
.properties
extension, such astestdata.properties
. - Add key-value pairs to the property file.
- Right-click the
-
Write Code to Read the Property File:
- Use Java’s
Properties
class to load and read the file. - Access the property file using an
InputStream
from the assets folder.
- Use Java’s
Sample Code in Eclipse
import java.io.InputStream;
import java.util.Properties;
public class PropertyFileReader {
public void readPropertyFileData(Context context) {
try {
// Load the property file from assets
InputStream inputStream = context.getAssets().open("testdata.properties");
// Create Properties object and load the file
Properties properties = new Properties();
properties.load(inputStream);
// Retrieve data using keys
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String expectedResult = properties.getProperty("expectedResult");
// Print the data
System.out.println("Username: " + username);
System.out.println("Password: " + password);
System.out.println("Expected Result: " + expectedResult);
// Close the InputStream
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Example Property File
# Test data for login functionality
username=testuser1
password=password123
expectedResult=Success
Real-Time Scenarios
-
Configuration Management:
- Store environment-specific configurations like API URLs, timeouts, or feature flags in property files.
- Switch between different configurations by modifying the property file without changing the code.
-
Login Testing:
- Store test credentials and expected results for login functionality in the property file.
- Use automation scripts to read the data and simulate login attempts.
-
Localization Testing:
- Store language-specific strings in property files.
- Verify that the application displays text correctly based on the selected locale.
Advantages of Using Property Files
- Simple and easy to create, with minimal learning curve.
- Lightweight format, suitable for small to medium-sized configurations or test data.
- Readable and editable without requiring additional tools.
- Separates configuration from the source code, promoting better maintainability.