简体   繁体   English

需要帮助,因为我的Java代码随机崩溃

[英]Need help as my Java code crashes randomly

I am new to Selenium and Java. 我是Selenium和Java的新手。 I have almost 950 lines of code in the only single java class that i have. 我在我唯一的java类中有近950行代码。 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. 它就像是随机崩溃的5次中的2次。我给出了代码的初始部分以及控制台在崩溃之前打印的最后一个字符串。

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. 我正在使用Java,Selenium,Eclipse,Win 8,IE 10.我使用JDK 8. Eclipse没有显示任何错误,为什么代码崩溃,老实说,我不知道在哪里检查我的代码崩溃的原因。

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); 了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");'

Replace 更换

 idriver.switchTo().frame("fraCureMD_Menu");

with

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

And see what it is printing on console. 并查看它在控制台上打印的内容。

1) Play around with temporarily disabling the thread (Experiment with values in Thread.sleep() statements.) 1)暂时禁用该线程(在Thread.sleep()语句中试验值。)

2)use: try { try { idriver.switchTo().frame("fraCureMD_Menu"); 2)使用:try {try {idriver.switchTo()。frame(“fraCureMD_Menu”); } catch(Exception e) { e.printStackTrace();} } catch(例外e){e.printStackTrace();}

}catch(Exception e) {System.out.println(e)} } catch(例外e){System.out.println(e)}

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

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