【问题标题】:How to use "if" operator for Appium tests如何使用“if”运算符进行 Appium 测试
【发布时间】:2016-06-30 12:28:29
【问题描述】:

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

我在 Appium.App 测试中记录了我所做的,并在按钮存在时添加了验证。由于我对 JavaScript 不太熟悉,所以我从 Objective-C 开始。但结果它总是点击 title_I_need 按钮,但我的期望是 else 分支与 other_title 按钮。

我可以用 Appium 做这样的检查吗?如果是,我该如何使用 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

【问题讨论】:

    标签: javascript appium appium-ios


    【解决方案1】:

    最简单的方法是使用findElementsBy(注意s),它将返回一个数组。然后只需检查数组是否为空。把它放在一个函数中,并称之为doesElementExists。对应的 java 方法是:

    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;
        }
    }
    

    【讨论】:

    • 将无法验证这一点。在与我的团队讨论问题后,我们决定切换到 Android 上的 Robotium 和 iOS 上的 UI 自动化。
    猜你喜欢
    • 2018-04-15
    • 2017-11-07
    • 1970-01-01
    • 2018-05-18
    • 2019-01-13
    • 2015-01-04
    • 2018-07-26
    • 2013-09-10
    • 2013-11-24
    相关资源
    最近更新 更多