简体   繁体   中英

appium test findElement button click doesnt work when it should

I'm trying to run a test in eclipse using appium for android. While the test starts and the application launches it throws an error on the first command while trying to press a button. I'm pretty sure that the id for the button is correct since running the same code with selendroid worked. The error at failure trace shows java.lang.NullPointerException. Here is my code:

package thePack;

import static org.junit.Assert.*;
import io.appium.java_client.AppiumDriver;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

public class theTest {


static AppiumDriver driver;

@Before
public void setUp() throws MalformedURLException, InterruptedException, Exception
{
    WebDriver dr;

    File app = new File("C:\\development\\src\\main\\resources\\app.apk");
    DesiredCapabilities capabilities= new DesiredCapabilities();
    capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
    capabilities.setCapability("deviceName", "Vodafone Smart 4G");
    capabilities.setCapability("platformVersion", "4.2.2");
    capabilities.setCapability("platformName", "Android");
    capabilities.setCapability("app", app.getAbsolutePath());       
    capabilities.setCapability("appium-version", "1.2.1");        
    capabilities.setCapability("appPackage", "mypackage.mine.net");       
    capabilities.setCapability("appActivity", "mypackage.mine.net.activities.mainActivity");


    dr = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) ;


}

@Test
public void login() throws Exception

{
    Thread.sleep(0500);         
    driver.findElement(By.id("english")).click();
    Thread.sleep(0500);
}

Any ideas? Thanks!

the line:

capabilities.setCapability(CapabilityType.BROWSER_NAME, "");

indicates that your test will be performed on the browser, and this should open the browser when start your test. Thus your elements will never be found

Figured out the issue. For me the problem was the Appium version itself. I was running the 1.3.4.1 and using an Android 4.2.2 device. As soon as i switched to version 1.2.4.1 for Appium i had no problems!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM