使用 Appium+Junit 写的自动化 Hybrid APP 架构的代码,问题是 Junit 中的两个 test 方法,同样的元素,第一个 test 方法中可以获取并执行相关操作,在第二个 test 方法中,总是报错:Couldn't find element by xpath,具体代码如下:

public class LoginTest {

    private static TestParams testData;

    private static boolean isAppOpen = false;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        AppDriverSetting.setDriver();   
        isAppOpen = true;
        testData = new TestParams(LoginTest.class.getSimpleName());
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        if(AppDriver.getCurrentDriver() != null)
            AppDriver.getCurrentDriver().quit();    
    }

    @Before
    public void setUp() throws Exception {
        if(!isAppOpen) {
            AppDriver.launchApp();
            isAppOpen = true;
        }       
    }

    @After
    public void tearDown() throws Exception {
        if(AppDriver.getCurrentDriver() != null) {
            AppDriver.getCurrentDriver().closeApp();
            isAppOpen = false;
        }
    }

    /**
     * testLogin Login with correct user name and password.
     */
    @Test
    public void testLogin() {
        String username = testData.getValue("Username");
        String password = testData.getValue("Password");

        LoginApp loginApp = new LoginApp();
        Assert.assertTrue(loginApp.login(username, password));  
    }

    /**
     * testLogin1 Login without entering password.
     */
    @Test
    public void testLogin1() {
        String username = testData.getValue("Username1");
        String password = testData.getValue("Password1");

        LoginApp  loginApp  = new LoginApp();
        Assert.assertTrue(loginApp.login(username, password));      
    }
}

如代码所示,相同的操作,在 testLogin()方法中可以成功执行,但是在 testLogin1()方法中总是报错:Couldn't find element by xpath,这跟我 setUp()和 tearDown()中执行的操作有关系 么?

崩溃 ing,恳请各位大牛指点迷津啊,急等!拜谢!


↙↙↙阅读原文可查看相关链接,并与作者交流