如何检查按钮是否被点击,IF表单实际上已提交Selenium / TestNG

时间:2016-01-07 15:39:32

标签: java selenium selenium-webdriver testng

我是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 | ");
    }

测试显示错误消息为" logout Failed"但它应该显示第一条消息"登录失败"如何检查登录和表单是否已成功提交?

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

任何想法都是我错了吗?

2 个答案:

答案 0 :(得分:0)

试试这个:

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

答案 1 :(得分:0)

提交表单后,您应该让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
        }               
    });