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

查看:28
本文介绍了如何使用“如果"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 按钮,但我的期望是 else 分支与 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;
    }
}

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

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