Appium Couldn't find element by xpath

willow_na · 2016年11月03日 · 最后由 willow_na 回复于 2016年11月04日 · 1087 次阅读

使用 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,恳请各位大牛指点迷津啊,急等!拜谢!

共收到 4 条回复 时间 点赞

执行 testLogin1() 的时候,先判断下是否在当前页面

你可以看下 app 状态,是不是还是登录状态,你只是关闭 App,并没有退出登录啊。

#1 楼 @nickli 可是我第一个方法中给的是错误的密码,根本就没能登录,而且第二次 launchApp 后,我明明看到 app 是在 login 的页面,而且用户名密码都是空的,但是就是找不到用户名和密码的 input element...

#3 楼 @yzx200712256 请问有什么好的方法判断是不是在当前页面呢?

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