【问题标题】:How to check if button clicked, IF form is actually submitted Selenium / TestNG如何检查按钮是否被点击,如果表单实际上是提交 Selenium / TestNG
【发布时间】:2016-01-11 19:51:04
【问题描述】:

我是 java/selenium/testNG 的新手,我有一个问题是我使用 dataprovider 运行了 4 组数据,但是,一组数据不正确,只是为了测试测试是否有效。我首先检查按钮是否显示然后单击。我希望能够检查是否单击了按钮,然后检查是否已提交表单,如果未提交表单则打印错误。我不确定如何执行此操作,我的代码如下:

WebElement  login = driver.findElement(By.id("dijit_form_Button_1_label"));


      //If statement - will check if element is Displayed before clicking on login button.
      if(login.isDisplayed()){
          login.click();
          //Main Event is logged If Passed
          Reporter.log("Login Form Submitted  | ");
          logger1.info("Submit Button Clicked");
      }else{  

          Reporter.log("Login Failed  | ");
          Assert.fail("Login Failed - Check Data | ");
          //Main Event Log Fail  
         }  




    WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));

    if(logout.isDisplayed()){
        logout.click();
    Reporter.log("Logout Successful  | ");
    }else {
        Reporter.log("Logout Failed");
        Assert.fail("Logout Failed - Check Data | ");
    }

测试显示一条错误消息为“注销失败”,但它应该显示第一条消息“登录失败”我如何检查登录和表单是否已成功提交?

它打印第二条消息的原因是代码运行通过了该点并且 selenium 看不到显示的注销按钮。

有什么想法是我错了吗?

【问题讨论】:

    标签: java selenium selenium-webdriver testng


    【解决方案1】:

    试试这个:

    private static By outButton = By.id("dijit_form_Button_0_label");
    if(driver.findElements(outButton).size() > 0)
    {
        logout.click();
        Reporter.log("Logout Successful  | ");
    }
    

    【讨论】:

    • 抱歉,不确定是否正确。我想检查是否显示了 web 元素。如果是,则单击该元素并提交表单,但是,我希望它此时还检查表单是否已提交以及表单是否失败。
    • Reporter.log("登录失败 | "); Assert.fail("登录失败 - 检查数据 | ");数据错误导致登录失败不显示
    【解决方案2】:

    提交表单后,您应该让 webdriver 等待某个条件为真。

    类似的东西

    int timeout = 30
    WebDriverWait wait = new WebDriverWait(driver, timeout);
    wait.until(new Function <WebDriver, Boolean>(){
        //@Override
        public Boolean apply(WebDriver driver) {                    
                //if the login has been successful return true
                //if not, return false
            }               
        });   
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-19
      • 1970-01-01
      • 2021-03-08
      • 1970-01-01
      • 2011-05-11
      • 2013-05-22
      • 2013-05-09
      • 2013-07-26
      相关资源
      最近更新 更多