Search code examples
javaeclipseseleniumcrash

Need help as my Java code crashes randomly


I am new to Selenium and Java. I have almost 950 lines of code in the only single java class that i have. when i run this code, it crashes randomly. Sometimes it will work fine, sometimes it will crash randomly anywere. It is like 2 out of 5 times it crashes randomly.I am giving the initial part of the code and the last string the console prints before crashing.

Kindly help. I am using Java, Selenium,Eclipse, Win 8, IE 10. I am using JDK 8. Eclipse does not show any error why code crashed, and honestly i dont know where to check why my code crashed.

One last request, i think i have given less information in this question, please tell me what to add more in it. Many thanks.

My code crashes and the last thing it prints is "In the Dashboard now" in the console.

` public class Login {

/**
 * @param args
 */
public static void main (String[] args) throws Exception {
    System.out.println("hello world");

    //String timeStamp = new SimpleDateFormat("MM/dd/yyyy").format(Calendar.getInstance().getTime());
    //System.out.println(timeStamp - );



    File file = new File("C:/selenim/IEDriverServer.exe");
    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
    WebDriver idriver = new InternetExplorerDriver();       

    System.out.println("instantiation");

       // Setting for Chrome        
  //WebDriver cdriver = new ChromeDriver();
  //cdriver.get("http://cmdlhrstg05/curemd/datlogin.asp");  //** getting and setting chrome driver values
  //cdriver.findElement(By.id("txtUserName")).sendKeys("haseeb");
  //cdriver.findElement(By.id("Password")).sendKeys("s");
  //cdriver.findElement(By.id("button")).click();

        // Setting for IE
    String parentWindow= idriver.getWindowHandle();
    idriver.get("http://cmdlhrstg05/curemd/datlogin.asp");    //** getting and setting ie driver values
    idriver.findElement(By.id("vchLogin_Name")).sendKeys(new String[] {"haseeb"});
    idriver.findElement(By.id("vchPassword")).sendKeys(new String[] {"s"});
    idriver.findElement(By.id("LoginImg")).click();



     Thread.sleep(1000); // Wait for some 5 seconds
     String actualTitle; // = idriver.getTitle();



           for (String handle : idriver.getWindowHandles()) {

           idriver.switchTo().window(handle);


        }


           actualTitle = idriver.getTitle();
           String expectedTitle = " Personal: Dashboard";  
           Thread.sleep(500);


           System.out.println("In the dashboard now");

Thread.sleep(1000);

           // To switch the frame to click the Patient CTA in universal links
           idriver.switchTo().frame("fraCureMD_Menu");  
           System.out.println("In the main menu now");'

Solution

    1. Play around with temporarily disabling the thread (Experiment with values in Thread.sleep() statements.)

    2)use:

    try
    {
        try 
        { 
           idriver.switchTo().frame("fraCureMD_Menu"); 
        } 
        catch(Exception e) 
        { 
           e.printStackTrace();
        }
    
    }
    catch(Exception e) 
    {
        System.out.println(e)
    }