cucumber.runtime.CucumberException: java.lang.NullPointerExc



我正在使用Cucumber,Maven和TestNG和Appium执行我的硒测试。当我通过TestNG.xml使用TestNG开始这种自动化时。应用程序已启动,但运行功能文件时出错 硒黄瓜.我收到以下错误。请帮助解决此错误。

cucumber.runtime.CucumberException: java.lang.NullPointerException
at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:69)
at runner.RunnerTest.feature(RunnerTest.java:54)
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:133)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:584)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:172)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:804)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:145)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.util.ArrayList.forEach(ArrayList.java:1257)
at org.testng.TestRunner.privateRun(TestRunner.java:770)
at org.testng.TestRunner.run(TestRunner.java:591)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:402)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:396)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:355)
at org.testng.SuiteRunner.run(SuiteRunner.java:304)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1180)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1102)
at org.testng.TestNG.runSuites(TestNG.java:1032)
at org.testng.TestNG.run(TestNG.java:1000)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Caused by: java.lang.NullPointerException
at com.odealMobile.Login.ClickLogin(Login.java:41)
at ✽.When click on login button(src/main/java/features/Android/Login.feature:6)

插件版本详情:

  • 测试NG - 6.7
  • 黄瓜 - 1.2.6
  • Maven - 3.6.3
  • Appium - 1.15.1

我的测试类;

package com.odealMobile;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import runner.RunnerTest;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class Login extends RunnerTest {
String loginButtonId = "login";
String registerButtonId = "register";
String TCKNId = "tckn";
String passwordId = "password";
String profileImageId = "profile_image_view";
@Given("^odeal app is opened$")
public void CheckOpenedPage() {
System.out.print(By.id(loginButtonId));
wait.until(ExpectedConditions.elementToBeClickable(By.id(loginButtonId)));
//loginButtonId bekleniyor.
wait.until(ExpectedConditions.elementToBeClickable(By.id(registerButtonId)));
}
@When("^click on login button$")
public void ClickLogin() {
// loginButtonId ye tıklanıyor.
driver.findElement(By.id(loginButtonId)).click();
}
@Then("^login page will be opened$")
public void CheckLoginPage() {
wait.until(ExpectedConditions.elementToBeClickable(By.id(loginButtonId)));
}
@When("^user enters valid TCKN$")
public void EnterTCKN() {
driver.findElement(By.id(TCKNId)).click();
driver.findElement(By.id(TCKNId)).setValue("23231487730");
}
@And("^user enters valid password$")
public void EnterPassword() {
driver.findElement(By.id(passwordId)).click();
driver.findElement(By.id(passwordId)).setValue("135246");
}
@And("^clicks on login button$")
public void ClickLoginonLoginPage() {
driver.findElement(By.id(loginButtonId)).click();
}
@Then("^home page will be opened$")
public void CheckHomePage() {
wait.until(ExpectedConditions.elementToBeClickable(By.id(profileImageId)));
if (driver.findElement(By.id(profileImageId)).isDisplayed()) {
System.out.println("Login Success");
} else {
System.out.println("Login Failed");
}
}
}

我的跑步者班;

package runner;
import cucumber.api.CucumberOptions;
import cucumber.api.testng.CucumberFeatureWrapper;
import cucumber.api.testng.TestNGCucumberRunner;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@CucumberOptions(
plugin = {
"pretty",
"json:target/report/cucumber2.json"
},
features = {
"src/main/java/features/Android/Login.feature"
},
glue = {
"com/odealMobile"
}
)
public class RunnerTest {
public AppiumDriver < MobileElement > driver;
public WebDriverWait wait;
private TestNGCucumberRunner testNGCucumberRunner;
@BeforeClass(alwaysRun = true)
public void setUpClass() throws Exception {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("deviceName", "TestDevice-1");
cap.setCapability("automationName", "UiAutomator2");
cap.setCapability("udid", "emulator-5554");
cap.setCapability("platformName", "Android");
cap.setCapability("platformVersion", "10.0");
//cap.setCapability("autoGrantPermissions", "true");
cap.setCapability("noReset", "false");
cap.setCapability("clearSystemFiles", "true");
cap.setCapability("appPackage", "com.telera.merchant.stage.debug");
cap.setCapability("appActivity", "com.telera.merchant.splash.SplashActivity");
driver = new AndroidDriver < MobileElement > (new URL("http://127.0.0.1:4723/wd/hub"), cap);
wait = new WebDriverWait(driver, 10);
testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
}
@Test(groups = "Cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
public void feature(CucumberFeatureWrapper cucumberFeature) {
testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
}
@DataProvider
public Object[][] features() {
return testNGCucumberRunner.provideFeatures();
}
@AfterClass(alwaysRun = true)
public void tearDownClass() throws Exception {
Thread.sleep(50000);
driver.quit();
//testNGCucumberRunner.finish();  
}
}

我的庞.xml;

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>appium.mobile.test</groupId>
<artifactId>odealMobile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>Odeal Mobile</name>
<description>Appium Testing for Mobile </description>
<properties>
<testng.version>6.7</testng.version>
<cucumber.version>1.2.6</cucumber.version>
</properties>
<dependencies>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-guice</artifactId>
<version>1.2.6</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-html</artifactId>
<version>0.2.3</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-testng</artifactId>
<version>${cucumber.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>${cucumber.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-jvm -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-jvm</artifactId>
<version>${cucumber.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/tag-expressions -->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>tag-expressions</artifactId>
<version>2.0.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

我的测试.xml;

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite">
<test thread-count="5" name="Test">
<classes>
<class name="runner.RunnerTest"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->

错误的重要部分就在这里:

Caused by: java.lang.NullPointerException
at com.odealMobile.Login.ClickLogin(Login.java:41)
at ✽.When click on login button(src/main/java/features/Android/Login.feature:6)

当您收到空指针异常时,这意味着变量或字段没有值。要么是因为它已设置为 null,要么是因为从未设置过。

那么会发生什么呢?以一种非常简化的方式:

RunnerTest runner = new RunnerTest();
runner.setUpClass() // This sets runner.driver
Login login = new Login();
login.ClickLogin() // This tries to use login.driver

首先创建RunnerTest类的实例,并在调用setupClass时设置driver。 然后创建登录管理器的新实例。由于这是一个不同的实例,因此即使它扩展了RunnerTest类,也永远不会设置driver变量。因此,当使用调用ClickLogin时,会发生空指针。

您可以通过几种方式解决此问题。最简单的方法可能是使driver静态,不必Login扩展RunnerTest并将driver引用为RunnerTest.driver

public class RunnerTest{  
public static AppiumDriver<MobileElement> driver;
}
public class Login {
@When("^click on login button$")
public void ClickLogin() {
// loginButtonId ye tıklanıyor.
RunnerTest.driver.findElement(By.id(loginButtonId)).click();
}
}

相关内容

最新更新