简体   繁体   English

检查表单是否实际提交-Java / Selenium / TestNG

[英]Checking if form was actually submitted - Java/Selenium/TestNG

I want to be able to check if a form is submitted and then if the form isn't submitted and an error message appears to print the error to the reporter log or logger. 我希望能够检查是否已提交表单,然后是否未提交表单,并且出现错误消息以将错误打印到报告日志或记录器。

My code is below for clicking a login button:please Check 我的代码如下,用于单击登录按钮:请检查

  WebElement  login = driver.findElement(By.id("dijit_form_Button_1_label")); 
  login button.
  if(login.isDisplayed()){

      login.click();

      Reporter.log("Login Form Submitted  | ");
      logger1.info("Submit Button Clicked");

  } else {
      Reporter.log("Login Failed  | ");
      Assert.fail("Login Failed - Check Data | ");       
  }

I'm passing in parameters to fill out the login form, I have set up one incorrect value in my data provider, however, when it gets to this bit of code the form is always submitted so if the data is correct the code runs fine and prints login form successful etc. 我正在传递参数以填写登录表单,但是我在数据提供程序中设置了一个不正确的值,但是,当涉及到这段代码时,始终会提交表单,因此,如果数据正确,则代码可以正常运行并成功打印登录表单等。

If the login form doesn't submit but is clicked so you are returned to the same login screen with an error, The else part of the code is never printed? 如果未提交登录表单但单击了登录表单,那么您将返回到同一登录屏幕,但出现错误,该代码的else部分永远不会被打印?

So I need to be able to click the button if the button is clicked but failed to login then print the else to the logger/reporter log. 因此,如果单击了按钮但登录失败,那么我需要能够单击该按钮,然后将else打印到记录器/报告器日志中。

How do I get the else part of the code to print if login does not happen? 如果未发生登录,如何获取要打印的代码的else部分?

You need to cross check login is done or not. 您需要交叉检查登录是否完成。 for valid credentials try to cross check any message like welcome or may log out button etc.. if they are not display, fail it. 要获得有效的凭据,请尝试交叉检查任何消息(如“欢迎”或可能退出按钮等)。如果未显示,则将其失败。

for wrong credentials, cross check error message. 对于错误的凭据,请交叉检查错误消息。 if error message is displayed pass else it fail. 如果显示错误消息,则通过,否则失败。

below is one example, where i am looking for message to display after clicked on button. 以下是一个示例,我在其中单击按钮后正在寻找要显示的消息。 it may help you to give good idea 它可以帮助您提出好主意

 //System.setProperty("WebDriver.chrome.driver", "C:\\Selenium Webdrivers\\chromedriver_win_26.0.1383.0\\chromedriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.get("http://seleniumtrainer.com/components/buttons/");

    driver.findElement(By.id("button1")).click();

    //by using if condition to cross check
    try{

        if(driver.findElement(By.id("demo")).isDisplayed()==true){

            System.out.println("clicked correctly");
        }

    }catch(Exception e){

        System.out.println("U Clicked Button1 text is not displayed. so failing or throwing again to fail test");
        throw new Exception(e.getMessage());
    }

    //simply by using testng assertion
    Assert.assertEquals(driver.findElement(By.id("demo")).getText(), "U Clicked Button1");

Thanks 谢谢

You have got the sequence of execution wrong. 您的执行顺序错误。

  1. Enter the form values 输入表单值
  2. Click on login button if displayed 单击登录按钮(如果显示)
  3. Check login button is displayed (This is for checking whether the form is submitted successfully or not.) If login button is displayed (form is not submitted) look for the error and print it. 显示“检查登录”按钮(用于检查表单是否已成功提交。)如果显示登录按钮(未提交表单),请查找错误并进行打印。

Psuedo code should look like below. 伪代码应如下所示。

// Enter the form values

// Check if login button and click on it to submit the form 
WebElement loginButton = driver.findElement(By.id("dijit_form_Button_1_label"));
if (loginButton.isDisplayed) {
    loginButton.click();
    Reporter.log("Submit Button Clicked");
}

// Now check again for the login button. 
// If login button is displayed it means that the form submission
// is not successful and you have returned to the same page.
// We are using a try catch block here because if the element is not found
// ie. if form submission is successful NoSuchElementException would be thrown.
try {
    // Element has to be found again else StaleElementReferenceException
    // might be thrown because of page reload.
    loginButton = driver.findElement(By.id("dijit_form_Button_1_label"));
    // if login button is found again, this means form submission is not successful
    Reporter.log("Form Not submitted");
} catch (Exception e) {
    // Login button is not found. Form is submitted
    Reporter.log("Form submitted successfully");
}

Hope this helps you. 希望这对您有所帮助。

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

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