2016-03-15 47 views
2

我需要检查标题为“title_I_need”的按钮是否存在。如果存在按下它,如果不是按另一个。所有这些东西在javaScript中。如何使用“if”运算符进行Appium测试

我做了什么,我记录在Appium.App测试,并添加验证,如果按钮存在。由于我对JavaScript并不熟悉,所以我开始使用Objective-C。但是因此它总是点击title_I_need按钮,但是我的期望是else_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 

回答

2

要做到这一点,最简单的方法是使用findElementsBy(注意小号),这将返回一个数组。然后检查数组是否为空。把它放在一个函数中,并把它称为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; 
    } 
} 
+0

将无法​​验证这一点。在与我的团队讨论问题后,我们决定在Android上切换到Robotium并在iOS上切换到UI Automation。 –

相关问题