【问题标题】:Use a string outside of if and else statement to set string value在 if 和 else 语句之外使用字符串来设置字符串值
【发布时间】:2019-02-15 19:04:38
【问题描述】:

我只是对我遇到的问题提出了一个足够简单的问题。我正在尝试将字符串值设置为"0",如果不存在 web 元素,否则字符串是 webelement 值(使用getText)。但是,我似乎无法在 if 和 else 语句之外使用这些值。我该怎么做呢?

这是我的代码

String players_in_game = null;

public void join_game_user_increases_register() throws Exception {

    WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);
    wait.until(ExpectedConditions.visibilityOf(countdownLabel));

    if (!num_of_players_in_game.isDisplayed()) {
        String players_in_game = "0";
    } else {
        String players_in_game = num_of_players_in_game.getText();
    }

    System.out.println(players_in_game);
    int first_num = Integer.parseInt(players_in_game);

【问题讨论】:

  • players_in_game 已在第一行声明为 String,因此请从 if 和 else 语句中删除它。

标签: java selenium if-statement testing automation


【解决方案1】:

你可以试试这个:

String players_in_game = null;

public void join_game_user_increases_register() throws Exception {

WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);
wait.until(ExpectedConditions.visibilityOf(countdownLabel));

try {
    if (num_of_players_in_game.isDisplayed()) {
        String players_in_game = num_of_players_in_game.getText();
    }
} catch (Exception e) {
    String players_in_game = "0";
}

System.out.println(players_in_game);
int first_num = Integer.parseInt(players_in_game);

【讨论】:

    【解决方案2】:

    Java 允许在类级别进行变量隐藏。因此,您实际上可以在任何方法中声明一个与类变量同名的变量。在您的情况下,变量名称是players_in_game

    您可以在方法中再次定义该变量,但该新变量的范围会有所不同。因此,如果您想在方法中设置该类级别的字符串,请不要定义新变量并使用类级别变量。

    所以只需使用以下代码:

    if (!num_of_players_in_game.isDisplayed()) {
        players_in_game = "0";
    } else {
        players_in_game = num_of_players_in_game.getText();
    }
    

    其他人已经用代码回答了。我只是想解释一下原因。

    【讨论】:

      【解决方案3】:

      使用下面的代码:

      WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10);
      wait.until(ExpectedConditions.visibilityOf(countdownLabel));
      
      String players_in_game = "0";
      if(num_of_players_in_game.isDisplayed()){
         players_in_game = num_of_players_in_game.getText();   
      }
      
      System.out.println(players_in_game);
      int first_num = Integer.parseInt(players_in_game);
      

      或者:

      String players_in_game = num_of_players_in_game.isDisplayed() ? num_of_players_in_game.getText() : "0";
      

      或者:

      List<WebElements> num_of_players_in_game = driver.findElements(By....);
      String players_in_game = num_of_players_in_game.size()==0 ? "0": num_of_players_in_game.get(0).getText();
      

      【讨论】:

      • 谢谢,我在这里也遇到了问题,如果找不到元素 num_of_players_in_game 测试是否失败?
      • 是的,如果元素不存在,num_of_players_in_game.isDisplayed() 将抛出错误。检查我的答案更新。只需使用 findElements 并检查大小。
      【解决方案4】:

      由于您已经在代码的第一行中将该变量声明为类成员,只需删除 String 即可不将该名称重新声明为局部变量,而是使用该字段:

      if(!num_of_players_in_game.isDisplayed()){
          players_in_game = "0";
      } else {
          players_in_game = num_of_players_in_game.getText();
      }
      

      【讨论】:

      • 不知道谁投了反对票,但我投了赞成票以补偿。不是出于怜悯,而是因为你是对的。 OP 在类级别定义了players_in_game 字段,因此删除String 意味着它将在方法中使用该字段。我会亲自删除类级别的 String 并使用 @sers 提供的第一个 sn-p,但这并不意味着您对 OP 提供的代码的回答是错误的。
      • @KevinCruijssen 起初由于问题中的格式错误,我没有看到字段声明,因此我提供了带有局部变量的相同代码。然后我编辑了整个东西,假设使用一个字段是要求。
      猜你喜欢
      • 2018-07-10
      • 1970-01-01
      • 2018-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 2016-05-15
      相关资源
      最近更新 更多