获得Appium驱动程序"空指针异常"

时间:2018-06-03 11:58:28

标签: android selenium appium

我一直在与Selenium和Testng合作。我能够执行我的脚本,没有这个空指针错误,直到昨天。任何人都可以告诉我我的剧本有什么问题。

package testCases;


import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import utilities.Constant;
import utilities.ExcelUtils;
import pageObjects.DearoLoginPage;
import pageObjects.JobCardsPage;
import TestData.MongoDBConnector;
import utilities.AndroidAppSetup;


public class Main_TestCase {

AndroidDriver<MobileElement> driver;
DearoLoginPage objLogin = new DearoLoginPage(driver);
MongoDBConnector objDB = new MongoDBConnector();
AndroidAppSetup objAndroidAppSetup = new AndroidAppSetup();


@BeforeClass
public void setUp() throws Exception{
    //This is to open the Excel file. Excel path, file name and the sheet name are parameters to this method
    ExcelUtils.setExcelFileSheet(Constant.Path_TestData+Constant.File_TestData, "Sheet1");
    ////Get the Desired Capabilities
    driver = (AndroidDriver<MobileElement>) objAndroidAppSetup.setupCapabilities();
    System.out.println("driver2 =" + driver); 
    Thread.sleep(2000);
}

@Test
public void loginRegisteredMobile() throws Exception {
    //Get the data from excel datasheet
    String MobileNumber = ExcelUtils.getCellData(1,0);
    System.out.println("driver3=  " + driver); 
    objLogin.MobileNumberOnLogin().clear();
    //Enter Mobile number
    objLogin.MobileNumberOnLogin().sendKeys(MobileNumber);
    //Click on Next button
    objLogin.NextButtonOnLogin().click();
}

我的另一个包有以下代码

package utilities;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import java.net.URL;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import utilities.Constant;
import utilities.ExcelUtils;

public class AndroidAppSetup {
 public AndroidDriver<MobileElement> driver;

 public AndroidDriver<MobileElement> setupCapabilities() throws Exception{
    ExcelUtils.setExcelFileSheet(Constant.Path_TestData+Constant.File_TestData, "Sheet1");
    //Get data from excelsheet
    String DeviceName = ExcelUtils.getCellData(1,6);
    String DeviceId = ExcelUtils.getCellData(1,7);
    String AndroidVersion = ExcelUtils.getCellData(1,8);
    String AppPackage = Constant.AppPackage;
    String AppActivity = Constant.AppActivity;
    String AppiumURL = Constant.AppiumURL;

    //Set the Desired Capabilities
    DesiredCapabilities caps = new DesiredCapabilities();
    caps.setCapability("deviceName", DeviceName);
    caps.setCapability("udid", DeviceId); //Give Device ID of your mobile phone 
    caps.setCapability("platformName", "Android");
    caps.setCapability("platformVersion", AndroidVersion);
    caps.setCapability("appPackage", AppPackage);
    caps.setCapability("appActivity", AppActivity);
    caps.setCapability(CapabilityType.TAKES_SCREENSHOT, "true");
    driver = new AndroidDriver<MobileElement>( new URL(AppiumURL), caps);
    System.out.println("driver1= " + driver); 
    return driver;
}

}

pageObjects.DearoLoginPage在下面的类

中定义
package pageObjects;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class DearoLoginPage {

    private static MobileElement element = null;
  //private AppiumDriver<MobileElement> driver;
    private AndroidDriver<MobileElement> driver;

    public DearoLoginPage(AndroidDriver<MobileElement> driver) {
        // private AndroidDriver<MobileElement> driver;
        this.driver = driver;
        }

    public MobileElement MobileNumberOnLogin(){
        System.out.println("driver4 = " + driver); 
        element = driver.findElement(By.id("com.carworkz.debug:id/et_login_mobile_no"));
        return element;
}

当我执行我的脚本时,我得到Null指针异常,如下所示,

INFO: Detected dialect: OSS
driver1= Android: null
driver2 =Android: null
driver=Android: null
driver4 = null
FAILED: loginRegisteredMobile
java.lang.NullPointerException
at pageObjects.DearoLoginPage.MobileNumberOnLogin(DearoLoginPage.java:26)
at testCases.Main_TestCase.loginRegisteredMobile(Main_TestCase.java:148)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:669)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:877)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1201)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:776)
at org.testng.TestRunner.run(TestRunner.java:634)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
at org.testng.SuiteRunner.run(SuiteRunner.java:334)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
at org.testng.TestNG.runSuites(TestNG.java:1161)
at org.testng.TestNG.run(TestNG.java:1129)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

请帮我解决此问题。

1 个答案:

答案 0 :(得分:0)

在驱动程序初始化之前创建并初始化DearoLoginPage类实例对象。请更改下面的初始化,然后检查。

修改后的Main_TestCase类:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.Assert;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import utilities.Constant;
import utilities.ExcelUtils;
import pageObjects.DearoLoginPage;
import pageObjects.JobCardsPage;
import TestData.MongoDBConnector;
import utilities.AndroidAppSetup;


public class Main_TestCase {

AndroidDriver<MobileElement> driver;
//removed the initialization part and initialization will happen in BeforeClass Method
DearoLoginPage objLogin; 
MongoDBConnector objDB = new MongoDBConnector();
AndroidAppSetup objAndroidAppSetup = new AndroidAppSetup();


@BeforeClass
public void setUp() throws Exception{
    //This is to open the Excel file. Excel path, file name and the sheet name are parameters to this method
    ExcelUtils.setExcelFileSheet(Constant.Path_TestData+Constant.File_TestData, "Sheet1");
    ////Get the Desired Capabilities
    driver = (AndroidDriver<MobileElement>) objAndroidAppSetup.setupCapabilities();

    //DearoLoginPage object initialization is added
    objLogin= new DearoLoginPage(driver);
    System.out.println("driver2 =" + driver); 
    Thread.sleep(2000);
}

@Test
public void loginRegisteredMobile() throws Exception {
    //Get the data from excel datasheet
    String MobileNumber = ExcelUtils.getCellData(1,0);
    System.out.println("driver3=  " + driver); 
    objLogin.MobileNumberOnLogin().clear();
    //Enter Mobile number
    objLogin.MobileNumberOnLogin().sendKeys(MobileNumber);
    //Click on Next button
    objLogin.NextButtonOnLogin().click();
}