Search code examples
javaseleniumappiumwinappdriver

Can't use findElementByName on Outlook mail when control via Appium with Java code


I am importing Appium (java-client-7.3.0.jar) and Selenium library for my project. My function:

import org.junit.*;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By.ByClassName;
import org.openqa.selenium.By.ByName;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.ArrayList;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.net.URL;

import io.appium.java_client.android.nativekey.KeyEvent;
import io.appium.java_client.windows.WindowsDriver;
import io.appium.java_client.windows.WindowsElement;

import java.awt.RenderingHints.Key;
import java.awt.Robot;
import java.lang.reflect.Array;

public class WinAppEmail {
    private static WindowsDriver<WindowsElement> session = null;

     public void setup() {
            try {
                DesiredCapabilities capabilities = new DesiredCapabilities();
                capabilities.setCapability("app", "C:\\Program Files\\Microsoft Office\\Office16\\OUTLOOK.EXE");

                session = new WindowsDriver<WindowsElement>(new URL("http://127.0.0.1:4723"), capabilities);

                session.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

                String strCurrentWindow="" ; 
//              strCurrentWindow = session.getWindowHandle();  // => Can't not get current handle

//              Robot robot = new Robot();
//              robot.keyPress(java.awt.event.KeyEvent.VK_CONTROL);
//              robot.keyPress(java.awt.event.KeyEvent.VK_N);
//              robot.keyRelease(java.awt.event.KeyEvent.VK_N);
//              robot.keyRelease(java.awt.event.KeyEvent.VK_CONTROL);

                session.findElementByName("New Email").click(); // => Can't find 


            }catch(Exception e){
                e.printStackTrace();
            } finally {
            }
        }
}

My code have 2 issue. 1. It can't get current window handle (strCurrentWindow = session.getWindowHandle();) 2. It can't click button on Outlook mail(session.findElementByName("New Email").click();) Please, help me! thanks all.

exception: Feb 07, 2020 3:55:42 PM io.appium.java_client.remote.AppiumCommandExecutor$1 lambda$0 INFO: Detected dialect: OSS --------------- Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/StringUtils at io.appium.java_client.internal.ElementMap.getElementClass(ElementMap.java:77) at io.appium.java_client.internal.JsonToMobileElementConverter.newRemoteWebElement(JsonToMobileElementConverter.java:67) at io.appium.java_client.internal.JsonToMobileElementConverter.apply(JsonToMobileElementConverter.java:57) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:561) at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41) at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1) at io.appium.java_client.windows.WindowsDriver.execute(WindowsDriver.java:1) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323) at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61) at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1) at io.appium.java_client.windows.WindowsDriver.findElement(WindowsDriver.java:1) at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:404) at io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:117) at io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1) at io.appium.java_client.windows.WindowsDriver.findElementByName(WindowsDriver.java:1) at Allegro.WinAppEmail.setup(WinAppEmail.java:59) at Allegro.MainFunction.main(MainFunction.java:9) Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.StringUtils at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 17 more


Solution

  • are you able to open Outlook? if no , then try changing the path for the same to :

    C:/Program Files (x86)/Microsoft Office/root/Office16/OUTLOOK.EXE
    

    I was able to do both the thing i.e. getting the windowHanlde as well as clicking on new mail , using this path.