【问题标题】:Null Pointer Exception using TestNG使用 TestNG 的空指针异常
【发布时间】:2015-11-11 14:37:51
【问题描述】:

我正在尝试自动化并将 Selenium 与 A​​ppium 集成,并且我正在使用带有 TestNG 的 Eclipse 来执行。

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

头等舱代码-

    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>

我在执行第一堂课后收到空指针异常。 Appium 服务器停止说没有收到更多命令。请帮忙。

【问题讨论】:

  • 你能分享堆栈跟踪吗?
  • 如果您提供 Stacktrace 它将帮助我们找到问题,但是现在从您提供的代码中,您缺少优先级 2。您已将注销测试用例赋予优先级 3,但它会达到优先级 2。谢谢

标签: selenium-webdriver testng appium


【解决方案1】:

您正在获得 NPE,因为您在 Logout 类中声明了另一个驱动程序引用 public AppiumDriver driver;

并且您正在使用此驱动程序引用而没有实例化。

【讨论】:

    【解决方案2】:

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

    试试这个:

    公共 AppiumDriver 驱动程序;

    driver = new AndroidDriver();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-09
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-31
      相关资源
      最近更新 更多