Appium appium 怎么能连续执行一个测试类中的多个测试方法呢?

梁慧玲 · 2014年12月19日 · 最后由 扮猪吃老虎 回复于 2014年12月29日 · 1775 次阅读

执行的第二个方法要在第一个基础上执行
如果不 quit 的话 又说不能开启新的 session ,可是 quit 后呢 应用都结束了,没法在原来基础上跑
难道说 appium 只能跑一整个流程?

共收到 14 条回复 时间 点赞

@nancy2896 @jerome
请使用@BeforeClass和@AfterClass注释 ,并且去掉对 TestCase 的继承,将 setup 和 teardown 方法改为静态方法如下

public class LoginTest{
   @BeforeClass
   public static void setup(){

   }

  @AfterClass
  public static void teardown(){

  }

  @Test
  public void test1(){}

  //......
}

不要继承 TestCase,使用@Before和@After因为这俩注释会在每个@Test执行前后都执行一次,导致出现 appium 的 session 会初始化两次

有依赖关系的,放在一个 testcase 里面。
然后请保持 testcase 之间的独立性。

#1 楼 @umbrella1978
testStudentlist() 这个方法就不会执行

package appiumdemo.mytest;

import io.appium.java_client.AppiumDriver;

import java.io.File;
import java.net.URL;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**

  • Unit test for simple App.
    */
    public class Login1Test extends TestCase
    {

    private AppiumDriver driver;
    /**

    • Create the test case *
    • @param testName name of the test case */ public Login1Test( String testName ) { super( testName ); }

@Before
protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();

File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/apps/teacher");
File app = new File(appDir, "teacher-4.0y--2014-12-11-1851-.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("udid","430072fe4758a07f");
capabilities.setCapability("deviceName","430072fe4758a07f");
capabilities.setCapability("platformVersion", "4.3");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "air.com.xueersi.ICSClient3");
capabilities.setCapability("appActivity", "air.com.xueersi.ICSClient3.LoginActivity");
//设置中文输入的属性
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
driver = new AppiumDriver(new URL("http://127.0.0.1:4492/wd/hub"), capabilities);

}

@After
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
driver.quit();

}

/**
* @return the suite of tests being tested
*/
public static Test suite()
{
return new TestSuite( Login1Test.class );
}

/**
* Rigourous Test :-)
* @throws InterruptedException
*/
@org.junit.Test
public void testLogin() throws InterruptedException
{
List textFieldsList = driver.findElementsByClassName("android.widget.EditText");

textFieldsList.get(0).sendKeys("滕俊龙");
Thread.sleep(2000);

textFieldsList.get(1).sendKeys("020639");
Thread.sleep(2000);

driver.findElementByName("登录").click();
Thread.sleep(2000);
}

@org.junit.Test
public void testStudentlist() throws InterruptedException

{
Thread.sleep(2000);
WebElement Studentlistbutton = driver.findElement(By.id("air.com.xueersi.ICSClient3:id/menu_student"));
Studentlistbutton.click();

}

}

#2 楼 @anikikun

什么意思?怎么保持 case 的独立性?我想依赖之前的页面

#6 楼 @umbrella1978 恩 我按你说的方法试了 好了 非常感谢

我也遇到这个问题了,我本来把 appium 的启动环境放在 testcase 的的配置中,然后执行 login 方法,再然后后续的其他 case 都依赖 Login,但是呢,执行完 login 这条 case 以后,就会出现 quit 的情况。结果第二条 case 就只能再跑一次 login 方法了,很浪费时间。
我目前的想法是,第一次启动的时候使用 app 的启动 activity,然后跑其他 case 的时使用应用的其他 activity。现在还没有理顺,不知道能不能行通了。

TestNG, 用 Xml 运行,case 之间继承 driver,写一个 driver 的继承类,有活动的 driver 则继承,driver 为空则新建。

贴一下你的脚本代码,要不然看不出是什么问题

#11 楼 @sunrise
用了以后报这个错误 不支持啊
The attribute dependsOnMethods is undefined for the annotation type Test

还有我说的是不同的类中,这个类要依赖另一个类的执行结果

#12 楼 @nancy2896 看下 jar 包有没有导进去。
你可以在 login 方法中去调用另一个类的方法,然后再 dependsOnMethods,原则上应该可行,具体我没试过,你可以试试。
dependsOnMethods:依赖某个方法,如果方法已执行过则继续,如果方法没执行过则先执行该方法。

#13 楼 @sunrise 是导入 testng.jar 包么

10楼 已删除
public class LoginTest{
   @BeforeClass
   public static void setup(){

   }

   @AfterClass
   public static void teardown(){

  }

   @Test
   public void login(){

  }

   @Test(dependsOnMethods = { "login" })
   public void testcase(){

  }
}

试试看吧

#6 楼 @umbrella1978
另一个测试类怎么能延续前面的测试呢?我现在是两个应用要交互,我得写多个测试类:
比如我在测试类 A 中执行了方法 a,完成了登录
我在测试类 B 中想执行登录后的操作,能不能不再重复登录动作?

或者说能不能在测试类 A 中执行完登录后暂停,然后我执行另外一个类,完了再回来执行测试类 A,有办法实现么?
谢谢

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册