Thread.sleep() cause error in testng

1,444 views
Skip to first unread message

Wen Bin, Soso Su

unread,
Aug 12, 2011, 5:21:48 AM8/12/11
to selenium-users
     I am now start to learn testng, after I remove a testcase from junit to testng, the command "Thread.sleep()" cause error:Unhandled exception type InterruptedException
     I think it will not hard,who can help me ,thanks...

--
Soso

Mark Collin

unread,
Aug 12, 2011, 5:46:19 AM8/12/11
to seleniu...@googlegroups.com

Well you shouldn’t really be using Thread.sleep in 99% of cases anyway so removing it would be a good idea.

 

You would have to show us your code to get any detailed help.

--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


-- This message contains confidential information and is intended only for the individual named. If you are not the named addressee you should not disseminate, distribute or copy this e-mail. Please notify the sender immediately by e-mail if you have received this e-mail by mistake and delete this e-mail from your system. If you are not the intended recipient you are notified that disclosing, copying, distributing or taking any action in reliance on the contents of this information is strictly prohibited. If you have received this email in error please notify postm...@ardescosolutions.com

Wen Bin, Soso Su

unread,
Aug 12, 2011, 6:05:07 AM8/12/11
to seleniu...@googlegroups.com
package com.test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
import static org.testng.Assert.assertTrue;
import org.testng.annotations.Test;

public class NewTest {
String url = "http://www.google.com";
private Selenium selenium= new DefaultSelenium("localhost", 4444, "*firefox", url);

@Test
public void f() {
selenium.start();
selenium.open("http://bpmdev3.nmg.com.hk/suite/portal/login.htm");
selenium.type("id=username", "sososu");
selenium.type("id=password", "so1357");
selenium.click("id=submitButton");
selenium.waitForPageToLoad("30000");

//login process
selenium.selectFrame("fContent");
selenium.click("id=applicationNameLink");
selenium.click("link=Leave Application 假期申請應用程式");

//wait
while (!(selenium.isTextPresent("Leave Form 假期申請表")==true)) {   
      Thread.sleep(1000);
    }

//start process
selenium.click("css=#noStartForm_form_207 > ul > a");
selenium.waitForPopUp("startFormWin", "30000");
selenium.selectWindow("name=startFormWin");
selenium.click("name=buttonLabel");
selenium.waitForPageToLoad("30000");

//input username
Thread.sleep(1000);
selenium.type("id=textControl_fd_component_JSON_TASK_NSnumber2", "20110003");
 
and there are many code behind ...

2011/8/12 Mark Collin <ma...@ardescosolutions.com>



--
Soso

Wen Bin, Soso Su

unread,
Aug 12, 2011, 6:07:59 AM8/12/11
to seleniu...@googlegroups.com
I have to use it ,or else ,the test case can't be continue and Easy to occur problem
--
Soso

niharika varshney

unread,
Aug 12, 2011, 6:22:27 AM8/12/11
to seleniu...@googlegroups.com
Soso,

For any method that throws a nonruntime exception, you either need to handle the exception using a try catch block or further throw it to your calling method.

So you can either surround your Thread.sleep with a try catch block
 or
declare your method as public void f() throws InterruptedException{ 

Alternatively you can try using new SeleneseTestBase.pause(waittime)  - which does the first thing mentioned above..i.e. puts thread.sleep in a try catch block..

Regards,
Niharika

poobathy r

unread,
Aug 12, 2011, 6:24:17 AM8/12/11
to seleniu...@googlegroups.com
U can write a Seperate function for Wait for Text present and u can call the function here ...

Example

public static void waitForTextPresent(String Text, int timeout) throws Exception {
        for (int second = 0;; second++) {
        if (second >= timeout)
        {
        System.out.println("Timeout. Unable to find the Specified element" + Text);
        break;
        }
        try {
        if (selenium.isTextPresent(Text))
        break;
        } catch (Exception e) {
        }
        Thread.sleep(1000);
        }
        }

2011/8/12 Wen Bin, Soso Su <sos...@nmg.com.hk>

Smita Sinha

unread,
Aug 12, 2011, 6:25:47 AM8/12/11
to seleniu...@googlegroups.com
 while(!(s.isElementPresent(xpath))){
   s.setSpeed("10");
   Thread.sleep(10);
  }
 
I think this will work.
Do give it a try. I am using this.
 
Reagrds,
Smita

2011/8/12 poobathy r <poob...@gmail.com>

Wen Bin, Soso Su

unread,
Aug 12, 2011, 6:31:30 AM8/12/11
to seleniu...@googlegroups.com
The try it just now , and you are right , but if I throw exception it cause more error(before @Test)...

2011/8/12 niharika varshney <niharika...@gmail.com>



--
Soso

santoshsarma jayanthi

unread,
Aug 12, 2011, 9:13:10 AM8/12/11
to seleniu...@googlegroups.com
You just write separate method to wait for some particular Object/Element to present in that page. 

And call that method by passing the Element/Locator, where ever you want.

In that method you just use the try,catch statments.

sample
WaitforObject(element)
{
     try
     {
         if(!selenium.isElementPresent(element))
                 {
                               Thread.sleep(5000);
                   }
     }
     catch(Exception e)
     {
             e.printStackTrace();
     } 
Regards,
-SantoshSarma

Reply all
Reply to author
Forward
0 new messages