无法调用“Object.getClass()”,因为“对象”为空-Java 16

gwo2fgha  于 2022-12-17  发布在  Java
关注(0)|答案(2)|浏览(781)

我正试图设置一个Appium项目用于学习目的。我已经创建了一个框架的基本结构,但它抛给我以下错误:
步骤初始化异常:无法为TestAppPage创建步骤库:无法调用“Object.getClass()”,因为“object”为空 *
下面是我的一些项目类:
TestAppPage.java:

package pages;

import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import net.thucydides.core.annotations.Step;
import org.openqa.selenium.WebElement;

public class TestAppPage extends AppiumBaseScreen {
    @AndroidFindBy(id ="IntegerA")
    @iOSXCUITFindBy(accessibility = "IntegerA")
    private WebElement txtFirstNumberField;

    @AndroidFindBy(id ="IntegerB")
    @iOSXCUITFindBy(accessibility = "IntegerB")
    private WebElement txtSecondNumberField;

    @AndroidFindBy(id ="ComputeSumButton")
    @iOSXCUITFindBy(accessibility = "ComputeSumButton")
    private WebElement btnComputeSum;

    @AndroidFindBy(id ="Answer")
    @iOSXCUITFindBy(accessibility = "Answer")
    private WebElement lblSumResults;

    @Step("Enter 1st number")
    public void enterFirstNumber(int number) {
        setText(String.valueOf(number), txtFirstNumberField);
    }
    @Step("Enter 2nd number")
    public void enterSecondNumber(int number) {
        setText(String.valueOf(number), txtSecondNumberField);
    }
    @Step("Press sum button")
    public void pressSumButton() {
        btnComputeSum.click();
    }

    @Step("Return sum of numbers")
    public String getSumOfNumbers() {
        return lblSumResults.getText();
    }

}

字符串
AppiumBaseScreen.java:

package pages;

import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import static driver.DriverBase.getAppiumDriver;

public class AppiumBaseScreen {

    public AppiumBaseScreen() {
        PageFactory.initElements(new AppiumFieldDecorator(getAppiumDriver()), this);
    }

    protected void setText(String text, WebElement element) {
        click(element);
        element.clear();
        element.sendKeys(text);
    }

    protected void click(WebElement element) {
        moveFocusOnElement(element);
        element.click();
    }

    protected void moveFocusOnElement(WebElement element) {
        new Actions(getAppiumDriver()).moveToElement(element).perform();
    }

}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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>org.appiumpractice</groupId>
    <artifactId>AppiumPractice</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>16</maven.compiler.source>
        <maven.compiler.target>16</maven.compiler.target>
        <serenity.version>3.2.5</serenity.version>
        <serenity.cucumber.version>3.2.5</serenity.cucumber.version>
        <cucumber.version>7.3.3</cucumber.version>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>

        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>${serenity.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>

        <dependency>
            <groupId>org.jeasy</groupId>
            <artifactId>easy-random-core</artifactId>
            <version>5.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>5.2.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.17.2</version>
        </dependency>

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>com.github.javafaker</groupId>
            <artifactId>javafaker</artifactId>
            <version>1.0.2</version>
        </dependency>

    </dependencies>

</project>

DriverBase.java:

package driver;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.options.XCUITestOptions;
import io.appium.java_client.safari.options.SafariOptions;

import java.net.URL;

public class DriverBase {
    private static AppiumDriver driver;

    public static void initializeDriver(URL appiumURL, XCUITestOptions capabilities) {
       initIOSDriver(appiumURL, capabilities);
    }
    public static void initializeDriver(URL appiumURL, SafariOptions capabilities) {
        initIOSDriver(appiumURL, capabilities);
    }

    public static AppiumDriver getAppiumDriver() {
        return driver;
    }

    private static void initIOSDriver(URL appiumURL, XCUITestOptions capabilities) {
        driver = new IOSDriver(appiumURL, capabilities);
    }

    private static void initIOSDriver(URL appiumURL, SafariOptions capabilities) {
        driver = new IOSDriver(appiumURL, capabilities);
    }
    public static void closeDriver() {
        driver.close();
        driver.quit();
    }
}

当我尝试使用页面工厂与元素交互时,出现上述错误:

element.sendKeys(text);

如果我使用以下代码,一切都正常

driver.findElement(element).sendKeys(userName);
rjzwgtxy

rjzwgtxy1#

这是一个版本问题,尝试将JDK降级到11这修复了我的问题。希望它也能修复你的问题。

ffvjumwh

ffvjumwh2#

此问题已通过以下两项解决:
1.添加以下依赖项

<dependency>
         <groupId>cglib</groupId>
         <artifactId>cglib</artifactId>
         <version>3.3.0</version>
     </dependency>

1.降级到Java 11

相关问题