簡體   English   中英

如何在 Appium 測試中使用“if”運算符

[英]How to use "if" operator for Appium tests

我需要檢查是否存在標題為“title_I_need”的按鈕。 如果存在按它如果不按另一個。 所有這些東西都在 javaScript 中。

我所做的我記錄在 Appium.App 測試中,並添加了按鈕是否存在的驗證。 由於我對 JavaScript 不太熟悉,所以我從 Objective-C 開始。 但結果它總是點擊 title_I_need 按鈕,但我的期望是其他分支與 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

最簡單的方法是使用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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM