如何使用Selenium和Java处理普通的Javascript警报对话框按钮?

时间:2017-07-04 15:04:24

标签: java selenium automation webdriver alert

我有像这样的 HTML页面

<input type="button" id="btnlogin" value="Login" onclick="goToRegister()">

javascript 是,

<script language="javscript" type="text/javascript">
    function goToRegister()
    {
        alert("Login Successful!");
        window.location.href="Register.html";
    }
</script>

selenium脚本中,

  @Test
  public void goToRegisterPage()
  {
      System.out.println("goToRegisterPage");
      WebDriverWait wait = new WebDriverWait(driver,10);
      Alert alert = wait.until(ExpectedConditions.alertIsPresent());
      alert.accept();

      driver.get("file:///X://selenium//Register.html");
  }

  @BeforeMethod
  public void beforeMethod()
  {
      System.out.println("beforeMethod called " + visitcount++);

      System.setProperty("webdriver.gecko.driver", "X:\\Selenium\\geckodriver-v0.17.0-win32\\geckodriver.exe");
      driver = new FirefoxDriver();
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

      driver.get("file:///X://selenium//Login.html");
  }

情境:

当我使用 TestNG Suite 执行这些脚本时,我正在获取登录页面,脚本会将凭据传递给用户名和密码,最后点击登录按钮。

点击登录按钮后,会出现javascript警告...然后我再次登录页面而不是 Register.html :(

问题:如何处理这种情况?

谢谢,
Karunagara Pandi G

0 个答案:

没有答案