0
0
Selenium Javatesting~8 mins

File upload (sendKeys to input) in Selenium Java - Framework Patterns

Choose your learning style9 modes available
Framework Mode - File upload (sendKeys to input)
Folder Structure
  project-root/
  ├── src/
  │   └── test/
  │       └── java/
  │           ├── pages/
  │           │   └── FileUploadPage.java
  │           ├── tests/
  │           │   └── FileUploadTest.java
  │           └── utils/
  │               └── DriverManager.java
  ├── resources/
  │   ├── testdata/
  │   │   └── uploadFiles/
  │   │       └── sampleFile.txt
  │   └── config.properties
  ├── testng.xml
  └── pom.xml
  
Test Framework Layers
  • Driver Layer: Manages WebDriver setup and teardown (e.g., DriverManager.java).
  • Page Objects: Encapsulate page elements and actions. FileUploadPage.java has the file input element and upload method using sendKeys().
  • Tests: Test classes like FileUploadTest.java contain test methods that use page objects to perform file upload and verify results.
  • Utilities: Helper classes for common tasks, e.g., reading config or handling waits.
  • Test Data: Files to upload stored under resources/testdata/uploadFiles/.
  • Configuration: Properties files and testng.xml for environment and browser settings.
Configuration Patterns
  • Use config.properties to store environment URLs, browser type, and file paths.
  • Load config in DriverManager to initialize WebDriver accordingly.
  • Store file paths relative to project root or resources folder to keep tests portable.
  • Use TestNG parameters or profiles to switch environments or browsers without code changes.
Test Reporting and CI/CD Integration
  • Use TestNG reports for test execution results (pass/fail) with screenshots on failure.
  • Integrate with CI tools like Jenkins or GitHub Actions to run tests on code commits.
  • Generate HTML reports with detailed logs and attach uploaded file info for traceability.
  • Use build tools like Maven to manage dependencies and run tests automatically.
Best Practices
  • Use Page Object Model to separate page details from test logic.
  • Use sendKeys() on the file input element to upload files instead of OS dialogs.
  • Keep test files in a dedicated testdata folder for easy maintenance.
  • Use explicit waits to ensure the page is ready before interacting with elements.
  • Keep configuration externalized to support multiple environments and browsers.
Self Check

Where in this folder structure would you add a new page object for a different page that also has a file upload feature?

Key Result
Use Page Object Model with sendKeys on file input elements, externalize config, and organize tests and data clearly.