错误:Appium测试中的InvalidTestClassError

huangapple go评论78阅读模式
英文:

Error InvalidTestClassError in Appium test

问题

我正在尝试学习如何在Appium中自动化移动应用程序测试。
我遇到了一个奇怪的问题。测试抛出了一个错误:

org.junit.runners.model.InvalidTestClassError: 无效的测试类 'com.example.TestMovil':

  1. main() 方法不应该是静态的
  2. main 方法不应该带参数

最奇怪的是,当我编写测试时,一切都正常工作,只是在几天后重新启动时出现了问题。
我展示给你代码:

package com.example;

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

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class TestMovil {
    @Test
    
    public static void main(String[] args) {
        
    try{
        testAutoMovil();
    }catch(Exception exp){
        System.out.println(exp.getCause());
        System.out.println(exp.getMessage());
        exp.printStackTrace();
    }
}

    public static void testAutoMovil() throws MalformedURLException{

        AndroidDriver<MobileElement> driverAndroid;
        System.out.println("Creación del driver");

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("deviceName", "Redmi Note 8 Pro");
        capabilities.setCapability("udid", "heifzt9xypk7jbtc");
        capabilities.setCapability("platformName", "android");
        capabilities.setCapability("platformVersion", "10");
        capabilities.setCapability("appPackage", "com.xxx.xxx");
        capabilities.setCapability("appActivity", "com.xxx.views.activities.contexts.login.LoginActivity");

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

        driverAndroid = new AndroidDriver<MobileElement> (url, capabilities);
        driverAndroid.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println("Aplicación comienza");

        MobileElement iniciarSesion = driverAndroid.findElement(By.id("com.xxx.xxx:id/activity_onboarding_sign_in_button"));
        iniciarSesion.click();
    }
}

尝试在Android设备上测试登录会话。

英文:

I'm trying to learn how to automate mobile app tests in Appium.
I have a strange problem. The test throws me an error:

org.junit.runners.model.InvalidTestClassError: Invalid test class 'com.example.TestMovil':

  1. Method main() should not be static
  2. Method main should have no parameters

The strangest thing of all is that when I wrote the test everything worked fine, it was when I restarted it days later that it started to fail.
I show you the code:

package com.example;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class TestMovil {
@Test
public static void main(String[] args) {
try{
testAutoMovil();
}catch(Exception exp){
System.out.println(exp.getCause());
System.out.println(exp.getMessage());
exp.printStackTrace();
}
}
public static void testAutoMovil() throws MalformedURLException{
AndroidDriver&lt;MobileElement&gt; driverAndroid;
System.out.println(&quot;Creaci&#243;n del driver&quot;);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(&quot;deviceName&quot;, &quot;Redmi Note 8 Pro&quot;);
capabilities.setCapability(&quot;udid&quot;, &quot;heifzt9xypk7jbtc&quot;);
capabilities.setCapability(&quot;platformName&quot;, &quot;android&quot;);
capabilities.setCapability(&quot;platformVersion&quot;, &quot;10&quot;);
capabilities.setCapability(&quot;appPackage&quot;, &quot;com.xxx.xxx&quot;);
capabilities.setCapability(&quot;appActivity&quot;, &quot;com.xxx.views.activities.contexts.login.LoginActivity&quot;);
URL url = new URL(&quot;http://localhost:4723/wd/hub&quot;);
driverAndroid = new AndroidDriver&lt;MobileElement&gt; (url, capabilities);
driverAndroid.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(&quot;Aplicaci&#243;n comienza&quot;);
MobileElement iniciarSesion = driverAndroid.findElement(By.id(&quot;com.xxx.xxx:id/activity_onboarding_sign_in_button&quot;));
iniciarSesion.click();
}
}

Try to test a login session on Android device

答案1

得分: 0

你的测试应该放在src/test文件夹下,因此不应该有main方法。如果你使用IDE,应该删除main方法,右键单击文件并运行它,否则,你需要使用CLI。

最终结果应该如下所示:

package com.example;

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

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class TestMovil {
    
    @Test
    public static void testAutoMovil() throws MalformedURLException{

        AndroidDriver<MobileElement> driverAndroid;
        System.out.println("Creación del driver");

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability("deviceName", "Redmi Note 8 Pro");
        capabilities.setCapability("udid", "heifzt9xypk7jbtc");
        capabilities.setCapability("platformName", "android");
        capabilities.setCapability("platformVersion", "10");
        capabilities.setCapability("appPackage", "com.correos.prepago");
        capabilities.setCapability("appActivity", "com.marcanditasl.bnext.architecture.views.activities.contexts.login.LoginActivity");

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

        driverAndroid = new AndroidDriver<MobileElement> (url, capabilities);
        driverAndroid.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println("Aplicación comienza");

        MobileElement iniciarSesion = driverAndroid.findElement(By.id("com.correos.prepago:id/activity_onboarding_sign_in_button"));
        iniciarSesion.click();
    }
}
英文:

Your tests should be under src/test folder, therefore there is no main method. You should remove the main method, right click on the file and run it if you use IDE, otherwise, you need to use CLI.

The end result would look like this :

package com.example;

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

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;

public class TestMovil {
    
    @Test
    public static void testAutoMovil() throws MalformedURLException{

        AndroidDriver&lt;MobileElement&gt; driverAndroid;
        System.out.println(&quot;Creaci&#243;n del driver&quot;);

        DesiredCapabilities capabilities = new DesiredCapabilities();

        capabilities.setCapability(&quot;deviceName&quot;, &quot;Redmi Note 8 Pro&quot;);
        capabilities.setCapability(&quot;udid&quot;, &quot;heifzt9xypk7jbtc&quot;);
        capabilities.setCapability(&quot;platformName&quot;, &quot;android&quot;);
        capabilities.setCapability(&quot;platformVersion&quot;, &quot;10&quot;);
        capabilities.setCapability(&quot;appPackage&quot;, &quot;com.correos.prepago&quot;);
        capabilities.setCapability(&quot;appActivity&quot;, &quot;com.marcanditasl.bnext.architecture.views.activities.contexts.login.LoginActivity&quot;);

        URL url = new URL(&quot;http://localhost:4723/wd/hub&quot;);

        driverAndroid = new AndroidDriver&lt;MobileElement&gt; (url, capabilities);
        driverAndroid.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        System.out.println(&quot;Aplicaci&#243;n comienza&quot;);

        MobileElement iniciarSesion = driverAndroid.findElement(By.id(&quot;com.correos.prepago:id/activity_onboarding_sign_in_button&quot;));
        iniciarSesion.click();
    }
}

huangapple
  • 本文由 发表于 2023年6月19日 18:01:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76505548.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定