Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Find the attached screen shot for reference(Scripts with result)

I executed some selenium scripts, but they are not running properly. Execution is taking the scripts randomly and start execution. I want to execute the scripts as per script order. Please tell me a solution for this.

For example the scripts' order is:

  1. Loginpage (1)
  2. Dashboard (2)
  3. firstlink (3)
  4. secondlink (4)
  5. thirdlink (5)

Execution order:

  1. firstlink (3)
  2. Dashboard (2)
  3. secondlink (4)
  4. loginpage (1)
  5. thirdlink (5)

How to execute these scripts in the proper order?

share|improve this question
1  
Scripts are not able to run randomly just only in that case when you call them randomly. Computers do that what you tell them. Did you write the code? Do you know who did it? Did you ask him/her what is the expected behaviour or execution order? On the other hand, please provide more information because not possible to answer your question. Or the answer will be yes, or 42. ;) –  SayusiAndo Jan 28 at 7:37
add comment

3 Answers

Two parts to this answer: First, to answer the question, there are many test runners. Some of them by default will run in a particular order, it may be the order they appear in code, or alphabetically, or some other order. There is often a way to tell the test runner to execute them in a particular order by providing some additional command line parameters, or altering the config. From your screenshot you're using junit, junit actually does not have a way to force the order of execution, see this for details: http://stackoverflow.com/questions/3693626/how-to-run-test-methods-in-specific-order-in-junit4

The second part is not directly answering the question, but is important and is also discussed in the linked answer from stackoverflow. You should avoid having tests that need to be run in a specific order. What if you want to execute only the last test, or debug the last test? Do you have to wait for all of the other tests to execute? What if one of the tests early in the chain fails, likely all of the rest of the tests will fail as well. What happens when you get a large suite of tests and you want to execute them in parallel? It is typically a best practice to have individual tests self contained and not relying on other other tests for the reasons outlined above.

share|improve this answer
1  
See for example sqa.stackexchange.com/questions/76/…. –  user246 Jan 28 at 17:14
add comment

There is no default provisioning in JUnit to do this the way you want. By using MethodSorters.NAME_ASCENDING you can ensure order by naming tests in alphabetical order as showed in other answer. Alternatively, you can create your own OrderedRunner and an annotation to fix this type of ordering.

public class OrderedRunner extends BlockJUnit4ClassRunner
{

  public OrderedRunner(Class klass) throws InitializationError
  {
    super(klass);
  }

  @
  Override
  protected List computeTestMethods()
  {
    List list = super.computeTestMethods();
    List copy = new ArrayList(list);
    Collections.sort(copy, new Comparator()
    {
      public int compare(FrameworkMethod o1, FrameworkMethod o2)
      {
        SeleniumMethodOrder order1 = o1.getMethod()
          .getAnnotation(SeleniumMethodOrder.class);
        SeleniumMethodOrder order2 = o2.getMethod()
          .getAnnotation(SeleniumMethodOrder.class);
        if(order1.order() == order2.order())
        {
          return 0;
        }
        else if(order1.order() > order2.order())
        {
          return 1;
        }
        else
        {
          return -1;
        }
      }
    });
    return copy;
  }
}

This makes use of an annotation that is created as below

@java.lang.annotation.Target(value = {java.lang.annotation.ElementType.METHOD})
@ java.lang.annotation.Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME)
public@ interface SeleniumMethodOrder
{
  public abstract int order() default 1;
}

Now you can write tests as shown below and provide order for them...

@RunWith(OrderedRunner.class)

public class StorageServiceWdTest extends WebDriverTestBase {


    @Test
    @SeleniumMethodOrder(order = 1)
    public void testDeleteFile() {
        logger.info("adding file");
    }


    @Test
    @SeleniumMethodOrder(order = 2)
    public void testAddFile() {
        logger.info("adding file");
    }

    @Test
    @SeleniumMethodOrder(order = 3)
    public void testMoveFile() {
        logger.info("moving file");
    }
}

This will ensure that the tests will always run the given order parameter of SeleniumMethodOrder annotation.

Source: http://neopatel.blogspot.in/2013/06/junit4-custom-method-order-or-custom.html

share|improve this answer
    
Thanks for your answer. I tried this annotations in my script. But still i am facing the same problem. can you please tell me, where i need to add this annotation "@RunWith(OrderedRunner.class)". –  sathi Feb 4 at 10:20
    
It is not just annotation, you have to create the annotation as well. You have to put the annotation above your test class as shown in the code. Previously the annotation line is not highlighted along with the code. Now I updated it to reflect that. –  Buddha Feb 4 at 12:30
add comment

There is a new method you could use which is the @FixMethodOrder(MethodSorters.NAME_ASCENDING) and place this outside of your class, e.g.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)

public class HowDoIRunMyTestsInOrderIWant {
public static ChromeDriver chrome;

@BeforeClass
public static void launchBrowser(){
some code goes in here

    }

@Test
public void aLogIn(){
your code = chrome.findElement.blahblah
}
@Test
public void bAnotherTest(){
some code in here
}

So by naming them in an alphabetical order they will run as you wish.

Hope it helps you.

share|improve this answer
    
Thanks for your reply. I dont know how to use this annotation. I created annotation class. Inside that method what i need to mention.. Can you please explain me briefly. –  sathi Feb 4 at 10:27
    
So for each @Test method name, you will specify the order to follow in alphabetical ordering. So.. Test 1 public void aThisIsMyFirstTest() Test 2 public void bThisIsMySecondTest() Test 3 public void cThisIsMyThirdTest() Test 4 public void dThisIsMyFourthTest() Test 5 public void dThisIsMyFifthTest() and so on. Just like I said in my answer. Does that make sense? –  Django_Tester Feb 4 at 12:21
    
yes this is fine. I created method like you mentioned above. But i created annotation method as "public @interface FixMethodOrder { -------------------- }" - inside this method, instead of dash line, what i need to write. Also i added this annotation(@FixMethodOrder(MethodSorters.NAME_ASCENDING)) outside of my class. But i am getting error message. Can you tell me the solution.. –  sathi Feb 5 at 4:37
    
@sathi You only need to add the FixMethodOrder once, and declare it outside of your class. You do not need to add it again when annotating methods. This is probably why you are receiving an error. Try removing it from the method I let me know if you are still receiving an error. If so, please post the stack trace. –  Django_Tester Feb 7 at 9:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.