2gis / Winium

Automation framework for Windows platforms
Mozilla Public License 2.0
384 stars 124 forks source link

Unable to click on Subject field in MS Outlook 2016 application. #68

Open bharadwaj-pendyala opened 6 years ago

bharadwaj-pendyala commented 6 years ago

Trying to automate a new scenario where we open the Outlook application and follow below steps.

Enter to email address. Enter subject. Enter email content. Able to automate till Step 1 and Step 3. Only step 2 is not being identified. PFB the below source code. Tried all possible combinations for subject properties. Tried to find it using name - "Subject" and using class name and also automation id. But no luck. Kind of stuck and need help regarding the same.

Thank You.

package test;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import Allocator.Allocator;

public class MSOutlook extends Allocator {

    public void openOutlook() throws InterruptedException, IOException {
        setupEnvironment("Outlook");
        Thread.sleep(10000);        
    }

    public void sampleOps() {
        driver.findElement(By.name("File Tab")).click();
        driver.findElement(By.name("Exit")).click();
    }

    public void sendEmail() throws InterruptedException, AWTException {
        driver.findElement(By.name("New Email")).click();
        Thread.sleep(2000);
        WebElement to = driver.findElement(By.name("To"));
        WebElement cc = driver.findElement(By.name("Cc"));
        WebElement sub = driver.findElement(By.className("Static"));
        to.sendKeys("b.pendyala@accenture.com");
        sub.sendKeys("Subject");

        //Insert Subject
        driver.findElement(By.name("Page 1 content")).sendKeys("Test Email");
        driver.findElement(By.name("Send")).click();
        driver.close();
    }

    public static void main(String args[]) throws InterruptedException, IOException, AWTException {
        MSOutlook c = new MSOutlook();
        c.openOutlook();
        c.sendEmail();
    }

}
ankitvaish04 commented 6 years ago

You have to be a little bit smart while locating elements using Winium, its kind of complex at times, but can be achieved over the time. Try the following code, which finds the correct xpath in a hierarchical manner starting from the new email window down till the subject line and then enters the subject text. Replace control types, names etc. in the code below if you find them different using spy:

public static void main(String[] args) throws MalformedURLException, InterruptedException {
    // TODO Auto-generated method stub
    DesktopOptions option = new DesktopOptions();
    option.setApplicationPath("C:\\Program Files\\Microsoft Office\\Office15\\OUTLOOK.exe");
    WiniumDriver driver = new WiniumDriver(new URL("http://localhost:9999"), option);
    Thread.sleep(3000);
    driver.findElementByName("Home").click();
    driver.findElementByName("New Email").click();
    Thread.sleep(3000);
    driver.findElement(By.xpath("/*[contains(@ControlType,'ControlType.Window') and contains(@Name,'Untitled - Message')]"
                             + "//*[contains(@ControlType, 'ControlType.Pane') and contains(@Name,'Form Regions')]"
                             + "//*[contains(@ControlType, 'ControlType.Edit') and contains(@Name,'Subject')]"

                               )).sendKeys("This is a test");
    Thread.sleep(5000);
}