Skip to main content

How to Handle Plain Javascript alert dialog buttons using Selenium with Java?




I have HTML page like this,
<input type="button" id="btnlogin" value="Login" onclick="goToRegister()">

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

@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");
  }
Scenario:
When I execute these scripts using TestNG Suite, I'm getting the Login page and script is passing the credentials to username and password and finally clicks on the Login button.
After clicked login button, javascript alert appears...then I'm getting again the login page instead of Register.html :(

Question: How to handle this scenario?

If anyone know answer for the above scenario, please post your comments here. Thanks

Comments

Popular posts from this blog

Query to find Nth Maximum and Minimum value from the Table

Hi, This query will fetch the rows which is the most large value / maximum value. --Maximum value SELECT MAX( spend )   FROM edw_aggregate.vw_agg_order_automation  WHERE spend < ( SELECT MAX( spend )                  FROM edw_aggregate.vw_agg_order_automation ); The above query will only fetch top most value from the table. If we want to fetch Nth  maximum / minimum value, look into this query.   --Nth Minimum/Max SELECT MIN( spend )   FROM edw_aggregate.vw_agg_order_automation   where spend in (SELECT top 5 spend FROM edw_aggregate.vw_agg_order_automation order by spend desc) Thanks

Selenium with Java Interview Questions and Answers

What exception will be thrown when we use "findElement" method? NoSuchElementException will be thrown when the locator is not found in the DOM and no longer available. How will you handle popups windows in Selenium? We can handle popups in Selenium is simple. It provides various methods from WebDriver interface. They are, 1. getWindowHandle() 2. getWindowHandles() getWindowHandle() - deals with current window getWindowHandles() - it manages with multiple windows and returns set of instances Let me show an example to understand this better, import java.util.Iterator; import java.util.Set; import org.openqa.selenium.firefox.*; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import org.testng.annotations.AfterTest; public class PopupDemo {  private WebDriver driver;  @BeforeTest  public void setUp()  { System.setProperty("webdriver.firefox.bi