import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class M365WordAutomation {
    public static void main(String[] args) {
        // Set the path to the chromedriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize a new WebDriver instance
        WebDriver driver = new ChromeDriver();

        // Maximize the browser window
        driver.manage().window().maximize();

        // Set a default implicit wait time
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        try {
            // Navigate to the Microsoft 365 login page
            driver.get("https://www.office.com/");

            // Click on the Sign in button
            WebElement signInButton = driver.findElement(By.linkText("Sign in"));
            signInButton.click();

            // Enter your email and password to log in
            // Note: You will need to replace these selectors and input values with the appropriate ones for your login page
            WebElement emailField = driver.findElement(By.id("i0116"));
            emailField.sendKeys("your-email@example.com");
            WebElement nextButton = driver.findElement(By.id("idSIButton9"));
            nextButton.click();

            // Wait for the password field to be visible and enter the password
            WebElement passwordField = driver.findElement(By.id("i0118"));
            passwordField.sendKeys("your-password");
            WebElement signInButtonPassword = driver.findElement(By.id("idSIButton9"));
            signInButtonPassword.click();

            // If there's a "Stay signed in?" prompt, handle it
            WebElement staySignedInButton = driver.findElement(By.id("idSIButton9"));
            staySignedInButton.click();

            // Navigate to Word
            WebElement wordApp = driver.findElement(By.cssSelector("a[data-task='Word']"));
            wordApp.click();

            // Wait for Word to open and click on 'New blank document'
            WebElement newBlankDocument = driver.findElement(By.cssSelector("div[aria-label='New blank document']"));
            newBlankDocument.click();

            // Switch to the editor frame
            driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Editor']")));

            // Enter some text into the Word document
            WebElement editorBody = driver.findElement(By.xpath("//body[@contenteditable='true']"));
            editorBody.click();
            editorBody.sendKeys("Hello, this is a test.");

        } finally {
            // Close the browser after a short wait
            try {
                Thread.sleep(5000); // Wait for 5 seconds to see the entered text
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            driver.quit();
        }
    }
}