如何使用"if" Appium测试操作员 [英] How to use "if" operator for Appium tests

查看:131
本文介绍了如何使用"if" Appium测试操作员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检查标题为"title_I_need"的按钮是否存在.如果存在,则按它,如果不按,则按另一个.所有这些东西都在javaScript中.

I need to check if button with title "title_I_need" exist. And if exist to press it if not press another one. All this stuff in javaScript.

我在Appium.App测试中记录的内容并添加了验证(如果存在按钮).由于我对JavaScript不太熟悉,因此我从Objective-C开始.但是结果是它总是单击title_I_need按钮,但我的期望是其他都带有other_title按钮.

What I did I recorded in Appium.App test and added verification if button exist. As I'm not familiar with JavaScript to much I started with Objective-C. But as a result it always clicks title_I_need button but my expectation is else branch with other_title button.

我可以对Appium做这种检查吗?如果是,我该如何使用JavaScript(node.js)?

Can I do such check with Appium? If yes, How can I do this with JavaScript (node.js)?

#import <Selenium/SERemoteWebDriver.h>

@implementation SeleniumTest

-(void) run
{
    SECapabilities *caps = [SECapabilities new];
    [caps addCapabilityForKey:@"appium-version" andValue:@"1.0"];
    [caps setPlatformName:@"iOS"];
    [caps setPlatformVersion:@"8.4"];
    [caps setDeviceName:@"device_name"];
    [caps setApp:@"/path/AppName.app"];
    NSError *error;
    SERemoteWebDriver *wd = [[SERemoteWebDriver alloc] initWithServerAddress:@"0.0.0.0" port:4723 desiredCapabilities:caps requiredCapabilities:nil error:&error];
    //check for element with wrong not existed title to go to else branch
    if ([wd findElementBy:[SEBy name:@"wrong_title"]]){
    [[wd findElementBy:[SEBy name:@"title_I_need"]] click];
  } else {
    [[wd findElementBy:[SEBy name:@"other_title"]] click];
  }
}

@end

推荐答案

最简单的方法是使用findElementsBy(注意 s ),该方法将返回一个数组.然后只需检查数组是否为空.将其放在函数中,并调用诸如doesElementExists之类的名称.与此相对应的java方法是:

The easiest way to do this is to use findElementsBy (notice the s) which will return an array. Then simply check if the array is empty. Put this in a function and call it something like doesElementExists. A corresponding java method to this would be :

public boolean doesElementExists(By by) {
    try {
        List allElements = driver.findElements(by);
        if ((allElements == null) || (allElements.size() == 0))
            return false;
        else
            return true;
    } catch (java.util.NoSuchElementException e) {
        return false;
    }
}

这篇关于如何使用"if" Appium测试操作员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆