【问题标题】:Cannot inject @Test annotated Method with [class io.appium.java_client.android.AndroidDriver]无法使用 [class io.appium.java_client.android.AndroidDriver] 注入 @Test 注释方法
【发布时间】:2021-11-11 20:09:27
【问题描述】:

我正在尝试运行我的 TestNG 测试并收到此错误:

Cannot inject @Test annotated Method [Test1] with [class io.appium.java_client.android.AndroidDriver].

不知道必须做什么。我正在使用 Appium whit TestNG。

@Listeners(ExtentITestListenerAdapter.class)
public class TestNG {

    public AppiumDriver <AndroidElement>driver;

    @Test
    public void Test1(AndroidDriver driver) {
        MobileElement el1 = (MobileElement) driver.findElementById("skip_button");
        el1.click();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
        TouchAction touchAction = new TouchAction(driver);
        touchAction.tap(new PointOption().withCoordinates(725, 993)).perform();
        touchAction.tap(new PointOption().withCoordinates(293, 1005)).perform();
        touchAction.tap(new PointOption().withCoordinates(1175, 1531)).perform();
        touchAction.tap(new PointOption().withCoordinates(1166, 2029)).perform();
        touchAction.tap(new PointOption().withCoordinates(763, 2036)).perform();
        MobileElement el2 = (MobileElement) driver.findElementById("continue_button");
        el2.click();
    }

    @BeforeClass
    public void setUp() throws MalformedURLException {
        DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
        desiredCapabilities.setCapability("platformName", "Android");
        desiredCapabilities.setCapability("deviceName", "Pixel_4_2");
        desiredCapabilities.setCapability("automationName", "UiAutomator2");
        desiredCapabilities.setCapability("udid", "emulator-5554");
        desiredCapabilities.setCapability("app", "C:\\Users\\Diego\\Desktop\\flow-music-develop-dev-debug-v0.11.0-SNAPSHOT-209.apk");
        desiredCapabilities.setCapability("ensureWebviewsHavePages", true);

        URL remoteUrl = new URL("http://localhost:4723/wd/hub");

        driver = new AndroidDriver(remoteUrl, desiredCapabilities);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    }

    @After
    public void tearDown() {
        driver.quit();
    }

}

【问题讨论】:

  • 方法[coordsWizardFuncionando2]在哪里?你确定你提供了这个产生错误的类吗?
  • 是的,这是方法,我忘记在这里改名字了,但这是方法

标签: java appium


【解决方案1】:

如果你想在测试方法中使用参数,你需要提供DataProvider或者使用来自文件testng.xml的参数

文档链接:https://testng.org/doc/documentation-main.html#parameters

在这种情况下,只需去掉方法的参数,即public void Test1(AndroidDriver driver) -> public void Test1()

【讨论】:

  • 如果我删除参数并使用 public void Test1() 我得到这个错误 java.lang.NullPointerException: Cannot invoke "io.appium.java_client.AppiumDriver.findElementById(String)" 因为 "this.司机”为空
  • 您真的尝试删除它吗?或者您只是认为driver 将为空。您已经在 @BeforeClass driver = new AndroidDriver(remoteUrl, desiredCapabilities); 中实例化了 driver ---> driver 不能为空。
【解决方案2】:

我找到了解决办法

@BeforeMethod
public void setUp() throws Exception{

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setCapability("platformName", "Android");
    desiredCapabilities.setCapability("deviceName", "Pixel_4_2");
    desiredCapabilities.setCapability("automationName", "UiAutomator2");
    desiredCapabilities.setCapability("udid", "emulator-5554");
    desiredCapabilities.setCapability("app", "C:\\Users\\Diego\\Desktop\\flow-music-develop-dev-debug-v0.11.0-SNAPSHOT-209.apk");
    desiredCapabilities.setCapability("ensureWebviewsHavePages", true);



    URL remoteUrl = new URL("http://localhost:4723/wd/hub");

    driver = new AndroidDriver(remoteUrl, desiredCapabilities);
    String sessionId = driver.getSessionId().toString();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


}

我使用@BeforeMethod 声明这个方法,如果你种子这个方法包含这个 “驱动程序=新的AndroidDriver(remoteUrl,desiredCapabilities);” 使用这个我没有问题。 都来了

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 2016-02-13
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多