【问题标题】:Selenium message-error and message-success in javajava中的Selenium消息错误和消息成功
【发布时间】:2019-04-19 02:08:42
【问题描述】:

我在查找不存在的元素时遇到了问题。当我尝试登录应用程序时,如果登录失败,它将显示元素 -

dr.findElement(By.className("message-error ")).getText();

登录成功后会显示:

dr.findElement(By.className("message-success")).getText();

当我运行代码但它没有找到元素时,执行停止并出现异常:element is not found

String mes=null;        

mes=dr.findElement(By.className("message-success")).getText();
if(mes!=null) {
    File out= new File("success.txt");
    FileWriter fr =new FileWriter(out,true);
    PrintWriter pw=new PrintWriter(fr);
    pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
    pw.close();
}

    mes=dr.findElement(By.className("message-error")).getText();
    if(mes!=null) {
        File out= new File("error.txt");
        FileWriter fr =new FileWriter(out,true);
        PrintWriter pw=new PrintWriter(fr);
        pw.println(mes+"|"+user.get(i)+"|"+pass.get(i));
        pw.close();
    }

元素没有出现。

例如,成功元素在成功之前不会显示,错误元素在出现错误之前不会出现在 CSS 中。

那么我怎样才能告诉它元素是应该退出还是活下来还是出现做一个动作呢?

如果登录成功,if 语句中的正确做法是什么?这样做会导致登录失败吗?

【问题讨论】:

    标签: java selenium selenium-webdriver automated-tests


    【解决方案1】:

    这是对我有用的问题的答案

        try {
        WebElement sucmes=dr.findElement(By.xpath("//div[@class='message']"));
        String suclogin="login success:";
        if(sucmes.getText().contains(suclogin)) {
        File suclogin= new File("suclog.txt");
                    FileWriter suclogr =new FileWriter(suclogin,true);
                    PrintWriter suclogrw=new PrintWriter(suclogr);
                    suclogrw.println(sucmes.getText());
                    suclogrw.close();
                }else{
    //the other action here 
    }
        }
    

    【讨论】:

      【解决方案2】:

      我的选择是使用 Webdriver 等待。这是查找元素的完美方式。

          public WebDriverWait(WebDriver driver,15)
          WebElement successmessage = wait.until(ExpectedConditions.visibilityOfallElementLocatedby(By.className("message-success")));
         sucessmessage.getText();
      

      visibilityOfallElementLocatedby:

      检查网页上与定位器匹配的所有元素是否可见的期望。可见性是指元素不仅显示出来,而且高度和宽度都大于0。

      上面我写的是成功的消息,类似的方式尝试无效登录。

      要使用不同类型的等待,请查看此文档 - https://seleniumhq.github.io/selenium/docs/api/java/allclasses-noframe.html

      在该文档中搜索等待。

      【讨论】:

        【解决方案3】:

        我认为你有相同的元素,但类会根据应用程序行为发生变化。假设如果您能够登录应用程序,那么它会显示带有属性 message-success 的类的消息元素,如果它不允许,则在同一元素中显示带有属性“消息错误”的类的错误消息。

        我在下面的代码中处理了同样的事情-

        // first get the message element with some other locator or other attribute (don't use class name)
        
        WebElement message = driver.findElement(By.locator);
        
        if(message.getAttribute("class").contains("message-success")){
        
            System.out.println("Success message is " + message.getText())        
            // write the code to perform further action you want on login success
        
        }else if (message.getAttribute("class").contains("message-error")){
        
            System.out.println("Error message is " + message.getText())  
            // write the code to perform further action you want on login Fail
        
        }else{
        
            System.out.println("Message is empty" + message.getText())
        }
        

        如果您有其他疑问,请告诉我。

        【讨论】:

          【解决方案4】:

          使用 WebdriverWait 等待成功消息的可见性,

           // After  valid login 
            WebDriverWait wait = new WebDriverWait(driver, 60);
            WebElement successmessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-success")));
            sucessmessage.getText();
          

          同样的错误信息,

          // After  invalid login
           WebElement errormessage wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message-error")));
           errormessage.getText();
          

          【讨论】:

          • 事情是程序在出现错误时停止,如果正确,则在 if 语句中使用它,如果成功文件执行此操作,则执行错误操作
          • 你是在测试还是报废?
          猜你喜欢
          • 2016-12-14
          • 1970-01-01
          • 2020-06-11
          • 2023-04-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-09
          • 1970-01-01
          相关资源
          最近更新 更多