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EzNTYgMTAuNjg1IDUuNjAwMSAxMS4wMzg0IDUuNjAwMUgxMy43NTg0QzE0LjExMTkgNS42MDAxIDE0LjM5ODQgNS4zMTM1NiAxNC4zOTg0IDQuOTYwMVYyLjI0MDFDMTQuMzk4NCAxLjg4NjY0IDE0LjExMTkgMS42MDAxIDEzLjc1ODQgMS42MDAxWiIgZmlsbD0iI2ZmZiIvPgo8cGF0aCBkPSJNNCAxMkwxMiA0TDQgMTJaIiBmaWxsPSIjZmZmIi8%2BCjxwYXRoIGQ9Ik00IDEyTDEyIDQiIHN0cm9rZT0iI2ZmZiIgc3Ryb2tlLXdpZHRoPSIxLjUiIHN0cm9rZS1saW5lY2FwPSJyb3VuZCIvPgo8L3N2Zz4K&logoColor=ffffff)](https://zread.ai/Master-Frank/midscene-java) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![Java Version](https://img.shields.io/badge/Java-17+-green.svg)](https://www.oracle.com/java/technologies/javase-jdk17-downloads.html) [![Maven Central](https://img.shields.io/badge/Maven%20Central-0.1.1-blue.svg)](https://search.maven.org/artifact/com.midscene/midscene-java-parent) AI-powered automation framework for Web and Android with natural language-driven UI operations - Java version ## ๐ŸŒ Language Version - [ไธญๆ–‡็‰ˆๆœฌ (Chinese Version)](README-zh.md) ## ๐ŸŒŸ Project Overview Midscene Java is a revolutionary AI-powered automation framework designed for UI automation operations on Web and Android platforms. It is the Java implementation of Midscene Python, inheriting its core philosophy: **making automation as simple as speaking**. ### ๐ŸŽฏ Core Features - **Natural Language Operations** - Describe operation intentions in everyday language, and AI will automatically understand and execute them - **Intelligent Element Locating** - Multi-strategy fusion, automatically selects the optimal positioning method, adapts to page changes - **Structured Data Extraction** - Use natural language to extract complex structured data - **Intelligent Assertion Verification** - Describe verification conditions in natural language, AI automatically judges - **Multi-Platform Support** - Unified interface supports Web and Android platforms - **Visual Debugging** - Detailed execution screenshots and decision process recording - **Code Optimization and Refactoring** - Systematically refactored for more modular and maintainable code ## ๐Ÿ—๏ธ Project Structure ``` midscene-java/ โ”œโ”€โ”€ packages/ โ”‚ โ”œโ”€โ”€ core/ # Core module, providing Agent and AI engine โ”‚ โ”œโ”€โ”€ web/ # Web automation module โ”‚ โ”‚ โ”œโ”€โ”€ playwright/ # Playwright implementation โ”‚ โ”‚ โ””โ”€โ”€ selenium/ # Selenium implementation โ”‚ โ”œโ”€โ”€ android/ # Android automation module โ”‚ โ”œโ”€โ”€ cli/ # Command line tool โ”‚ โ”œโ”€โ”€ examples/ # Example code โ”‚ โ”œโ”€โ”€ playground/ # Development testing environment โ”‚ โ””โ”€โ”€ tests/ # Test cases โ”œโ”€โ”€ apps/ # Application examples โ”œโ”€โ”€ docs/ # Project documentation and optimization plans โ””โ”€โ”€ wiki/ # Project wiki documentation ``` ## ๐Ÿš€ Quick Start ### Prerequisites - Java 17+ - Maven 3.6+ or Gradle 7.0+ - Browser (Chrome/Firefox/Edge, for Web automation) - AI model API Key (Choose one from OpenAI, Claude, Qwen, or Gemini) ### Installation Add Midscene Java dependencies to your `pom.xml` file: ```xml com.midscene midscene-core 0.1.1 com.midscene midscene-web-playwright 0.1.1 com.midscene midscene-web-selenium 0.1.1 com.midscene midscene-android 0.1.1 ``` ### Configure AI Model Create an `application.properties` or `application.yml` file to configure the AI model: ```properties # application.properties midscene.ai.provider=openai midscene.ai.model=gpt-4-vision-preview midscene.ai.api-key=your_openai_api_key_here ``` ### Example Code #### Web Automation Example ```java package com.example; import com.midscene.core.Agent; import com.midscene.web.playwright.PlaywrightPage; import com.midscene.web.playwright.PlaywrightUIContextProvider; import com.microsoft.playwright.Playwright; import com.microsoft.playwright.Browser; import com.microsoft.playwright.Page; public class SearchExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { // Create browser instance Browser browser = playwright.chromium().launch(); Page page = browser.newPage(); // Create PlaywrightPage wrapper PlaywrightPage playwrightPage = new PlaywrightPage(page); // Create Agent Agent agent = new Agent(new PlaywrightUIContextProvider(playwrightPage)); // Navigate to website page.navigate("https://www.baidu.com"); // Use natural language for search agent.aiAction("Type 'Java tutorial' in the search box"); agent.aiAction("Click the search button"); // Verify search results agent.aiAssert("The page displays search results for Java tutorials"); System.out.println("โœ… Search operation completed!"); // Close browser browser.close(); } } } ``` #### Data Extraction Example ```java package com.example; import com.midscene.core.Agent; import com.midscene.web.playwright.PlaywrightPage; import com.midscene.web.playwright.PlaywrightUIContextProvider; import com.microsoft.playwright.Playwright; import com.microsoft.playwright.Browser; import com.microsoft.playwright.Page; import java.util.HashMap; import java.util.List; import java.util.Map; public class ExtractExample { public static void main(String[] args) { try (Playwright playwright = Playwright.create()) { Browser browser = playwright.chromium().launch(); Page page = browser.newPage(); PlaywrightPage playwrightPage = new PlaywrightPage(page); Agent agent = new Agent(new PlaywrightUIContextProvider(playwrightPage)); // Visit news website page.navigate("https://news.example.com"); // Extract structured data Map schema = new HashMap<>(); schema.put("articles", List.of( Map.of( "title", "News title", "time", "Publish time", "summary", "News summary" ) )); Map newsData = agent.aiExtract(schema); // Output results List> articles = (List>) newsData.get("articles"); for (Map article : articles) { System.out.println("๐Ÿ“ฐ " + article.get("title")); System.out.println("โฐ " + article.get("time")); System.out.println("๐Ÿ“„ " + article.get("summary") + "\n"); } browser.close(); } } } ``` #### Android Automation Example ```java package com.example; import com.midscene.core.Agent; import com.midscene.android.AndroidDevice; import com.midscene.android.AndroidUIContextProvider; import java.util.concurrent.CompletableFuture; public class AndroidExample { public static void main(String[] args) { // Connect to Android device AndroidDevice device = new AndroidDevice(); CompletableFuture connectFuture = device.connect(); connectFuture.join(); // Wait for connection to complete try { // Create Agent Agent agent = new Agent(new AndroidUIContextProvider(device)); // Launch application agent.aiAction("Launch the settings app"); // Perform operations agent.aiAction("Tap on the Wi-Fi option"); agent.aiAssert("The Wi-Fi settings page is open"); System.out.println("โœ… Android automation operation completed!"); } finally { device.disconnect(); } } } ``` ## ๐Ÿ“– Documentation - [Project Overview](wiki/้กน็›ฎๆฆ‚่ฟฐ.md) - *Chinese only* - [Installation and Configuration](wiki/ๅฎ‰่ฃ…้…็ฝฎ.md) - *Chinese only* - [Quick Start](wiki/ๅฟซ้€Ÿๅผ€ๅง‹.md) - *Chinese only* - [API Reference](wiki/api-reference/README.md) - [Example Code](wiki/examples/README.md) - [Frequently Asked Questions](wiki/ๅธธ่ง้—ฎ้ข˜.md) - *Chinese only* - [Core Concepts](wiki/ๆ ธๅฟƒๆฆ‚ๅฟต/README.md) - *Chinese only* - [Platform Integration](wiki/ๅนณๅฐ้›†ๆˆ/README.md) ## ๐Ÿ†š Comparison with Traditional Tools | Feature | Traditional Automation Tools | Midscene Java | |---------|------------------------------|---------------| | **Learning Curve** | Steep, requires learning complex APIs | Gentle, natural language driven | | **Code Readability** | Obscure and hard to understand | Intuitive and easy to understand | | **Maintenance Cost** | High, requires extensive modifications for page changes | Low, AI automatically adapts to changes | | **Element Locating** | Manual selector writing | AI intelligent locating | | **Error Handling** | Manual handling of various exceptions | AI automatic retry and recovery | | **Cross-Platform** | Requires learning different tools | Unified interface | | **Code Quality** | Varies by project | Systematically refactored, modular design | ## ๐Ÿค Contribution Guidelines We welcome all forms of contributions! Whether it's submitting bug reports, feature requests, documentation improvements, or code contributions. ### How to Contribute 1. Fork this repository 2. Create your feature branch (`git checkout -b feature/AmazingFeature`) 3. Commit your changes (`git commit -m 'Add some AmazingFeature'`) 4. Push to the branch (`git push origin feature/AmazingFeature`) 5. Create a Pull Request ### Development Environment Setup ```bash # Clone the repository git clone https://github.com/Master-Frank/midscene-java.git cd midscene-java # Build the project mvn clean install # Run tests mvn test ``` ### Code Standards - Follow commit message conventions from [Conventional Commits](https://www.conventionalcommits.org/) - Add corresponding test cases for new features - Add JavaDoc documentation for public APIs - Keep code modular, avoid overly long methods ## ๐Ÿ“„ License This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. ## ๐Ÿ“ Credits Thanks to Midscene Project: https://github.com/web-infra-dev/midscene for inspiration and technical references ## ๐Ÿ“ž Contact Us - **GitHub**: [Master-Frank/midscene-java](https://github.com/Master-Frank/midscene-java) - **Issue Reporting**: [GitHub Issues](https://github.com/Master-Frank/midscene-java/issues) - **Discussions**: [GitHub Discussions](https://github.com/Master-Frank/midscene-java/discussions) --- โญ If this project helps you, please give us a star!