使用TestNG的空指针异常

时间:2015-08-18 15:07:21

标签: selenium-webdriver testng appium

我正在尝试自动化并将Selenium与Appium集成,我正在使用Eclipse和TestNG执行。

现在在一个项目中,我有一个包含两个类的包;第一个类用于登录App,第二个类用于注销

头等代码 -

    package com.gma.test;

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

    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.Test;

    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.android.AndroidDriver;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    public class LoginApp {
        public AppiumDriver driver;

        @BeforeSuite
        public void beforeMethod() throws InterruptedException {

            File app = new File("C:\\Users\\mc30058\\Downloads\\gma-QA-RELEASE-QRCode-07022015_v4.5.4028.apk");
            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("deviceName","Android Emulator");
            capabilities.setCapability("platformVersion", "4.4");
            capabilities.setCapability("app", app.getAbsolutePath());

            try {
                driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
            } catch (MalformedURLException e) {

                e.printStackTrace();
            }

            driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//android.widget.Button[@resource-id='com.mcdonalds.app:id/button_choose_closest']")).click();
            driver.findElement(By.xpath("//android.widget.Button[@text='Continue']")).click();
            driver.findElement(By.xpath("//android.widget.Button[@text='OK']")).click();

        }

        @Test (priority=1)
        public void Login() {

            driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);

            driver.findElement(By.xpath("//android.widget.TextView[@text='Sign In']")).click();
            driver.findElement(By.xpath("//android.widget.EditText[@resource-id='com.mcdonalds.app:id/signin_edittext_email']")).sendKeys("anuj.shrivastava@us.mcd.com");
            driver.findElement(By.xpath("//android.widget.EditText[@resource-id='com.mcdonalds.app:id/signin_edittext_password']")).sendKeys("Anujtest2");

            currentTime();
            driver.findElement(By.xpath("//android.widget.Button[@text='Sign In']")).click();
            driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
            driver.findElement(By.xpath("//android.widget.Button[@text='Yeah, count me in!']")).isDisplayed();
            currentTime();
            driver.findElement(By.xpath("//android.widget.Button[@text='No, thanks. I like to be out of the loop']")).click();
        }

        private void currentTime() {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
            System.out.println( sdf.format(cal.getTime()) );
        }

    }

Second class code: 

    package com.gma.test;

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

    import org.openqa.selenium.By;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    import io.appium.java_client.AppiumDriver;
    import io.appium.java_client.android.AndroidDriver;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;

    public class Logout {

        public AppiumDriver driver;




        @Test (priority=3)
        public void LogoutApp() {

            driver.findElement(By.xpath("//android.widget.TextView[@content-desc='Navigation Button']")).click();

            driver.findElement(By.xpath("//android.widget.TextView[@text='Sign Out']")).click();
                driver.findElement(By.xpath("//android.widget.Button[@text='Sign Out']")).click();
        }
    }

来自Testng.xml的内容

<suite name = "GMA Automation">
    <test name = "GMA Automation">
        <classes>
            <class name = "com.gma.test.LoginApp"/>
            <class name = "com.gma.test.Logout"/>
        </classes>
    </test>
</suite>

执行第一堂课后,我收到Null Pointer异常。 Appium服务器停止说不再收到命令。请帮忙。

2 个答案:

答案 0 :(得分:0)

您正在获得NPE b'coz,您已在Logout课程中声明了另一个驱动程序参考public AppiumDriver driver;

您正在使用此驱动程序引用而不进行实例化。

答案 1 :(得分:0)

在logout类中,您忘记实例化AppiumDriver驱动程序。

试试这个:

公共AppiumDriver驱动程序;

driver = new AndroidDriver();