简体   繁体   English

webDriver java selenium:如果/ else具有asserttrue assertEquals和condition或/ and

[英]webDriver java selenium: if /else with asserttrue assertEquals and condition or /and

I search since 3 days now. 我从3天开始搜索。

Hello 你好

I want to verify that if condition1 AND condition2 is true OR condition3 AND condition4 is true, my test is Ok. 我想验证如果condition1 AND condition2为true或condition3 AND condition4为true,则我的测试正常。

this code doesn't return error, the test runs passed but doesn't return the good message. 此代码不返回错误,测试运行通过,但不返回正确的消息。 it return "KO, the color doesn't match with the status." 它返回“ KO,颜色与状态不匹配”。 instead of O"OK, the color is red and the status is NOK." 而不是O“ OK,颜色为红色,状态为NOK”。 And i think that if the status doesn't match with the color, the test will be passed too (??) 而且我认为,如果状态与颜色不匹配,则测试也会通过(??)

Thanks for your help. 谢谢你的帮助。

Isabelle. 伊莎贝尔。

PS: you can answer in french !! PS:您可以用法语回答!

//1 store the value of the status // 1存储状态值

String DisplayedStatus = driver.findElement(By.xpath("//span[@class='eaDetails-ContentRegion-statustext']")).getText();
    System.out.println("the status displayed is: " + DisplayedStatus);

//2 store the color of the bar // 2存储条形的颜色

String ColorRed = "rgb(227, 34, 25)";
    System.out.println("Red is: "+ColorRed);
String ColorGreen = "rgb(137, 186, 23)";
    System.out.println("Green is:"+ColorGreen);
String DisplayedStatusColor = driver.findElement(By.xpath("//div[@class='eaDetails-ContentRegion-bar']")).getAttribute("style");
    //System.out.println("Styles of bar is: "+DisplayedStatusColor);
// keep bgcolor only
String BgColor1 = DisplayedStatusColor.split("background-color:")[1];
    //System.out.println("Color of bar is: "+BgColor1);
String BgColor = BgColor1.split(";")[0]; // remove the semi comma
    System.out.println("Color of bar is: "+BgColor);'

// check that "the bar is red and the status is NOK" or "the bar is green and status is OK". //检查“条形为红色且状态为NOK”或“条形为绿色且状态为OK”。

 // assertTrue((BgColor.compareTo(ColorRed) == 0)&&(DisplayedStatus.compareTo("Status: NOK") == 0));      
 // assertTrue((BgColor.compareTo(ColorGreen) == 0)&&(DisplayedStatus.compareTo("Status: OK") == 0));

  if((BgColor.compareTo(ColorRed) == 0)&&(DisplayedStatus.compareTo("Status: NOK") == 0)){
        System.out.println("OK, the color is red and the status is NOK.");
  }
  else {
      if((BgColor.compareTo(ColorGreen) == 0)&&(DisplayedStatus.compareTo("Status: OK") == 0)){
            System.out.println("OK, the color is green and the status is OK.");
      }

      System.out.println("KO, the color doesn't match with the status.");
  } 
}`

Replace your code with simpler dummy values, that way you will know where your if's behave as you expected. 用更简单的伪值替换您的代码,这样您就可以知道if的行为在哪里。 Every part of logic could be replaced with simpler. 逻辑的每个部分都可以用更简单的替换。 That way you could check if your if logic is correct. 这样,您可以检查逻辑是否正确。 Use simple steatment like 使用简单的策略

boolean condition1=false; boolean condition2=true;

code that works: 起作用的代码:

// check that "the bar is red and the status is NOK" or "the bar is green and status is OK". //检查“条形为红色且状态为NOK”或“条形为绿色且状态为OK”。

  String StatusIsOK = "Status: OK"; String StatusIsNOK = "Status: NOK"; if(BgColor.equals(ColorRed)){ assertTrue(DisplayedStatus.compareTo(StatusIsNOK) == 0); System.out.println("OK, the color is red and the status is NOK."); } else{ if(BgColor.equals(ColorGreen)){ assertTrue(DisplayedStatus.compareTo(StatusIsOK) == 0); System.out.println("OK, the color is green and the status is OK."); } else{ System.out.println("FAILURE: the color "+BgColor+" and the status "+DisplayedStatus+" don't match."); } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM