Jonathan
Jonathan

Reputation: 395

Junit automation test in sequential order

I created Junit test in one class in Java. Basically one class with multiple test test cases within that class and when the test runs the chrome page comes up with a blank page. It runs out of order and its like for each @test senario it resets bringing up another chrome page instead of executing each test within the same page and going to the next test so on. When I run this all in ( one @test case ) it works fine.

I use protractor and program the same way with describes and its and have no problem. I am trying to do the same in Java with Junit so if a test case fails I can go to that one Test case quickly. I have a @Before and @Test and @after annotations

Please help I am not as experienced with Java.My code is below.

 public class BankrateAlt {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    System.setProperty("webdriver.chrome.driver", "C:\\eclipse\\Java\\chromedriver\\chromedriver.exe");  
    System.setProperty("webdriver.gecko.driver", "C:\\eclipse\\Java\\geckodriver\\geckodriver.exe");  
    driver = new ChromeDriver();
    baseUrl = "http://www.bankrate.com/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test 
  public void moveToElementMortgages() throws InterruptedException{
      Actions builder = new Actions(driver);
      WebElement element = driver.findElement(By.linkText("MORTGAGES"));
      builder.moveToElement(element).perform();
      Thread.sleep(2000);
  }

  @Test 
  public void clickOnCalculators() throws InterruptedException{
      Actions builder = new Actions(driver);
      WebElement calculator = driver.findElement(By.cssSelector("a[href*='mortgage-calculators']"));
      builder.moveToElement(calculator).perform();
      calculator.click();
      Thread.sleep(2000);
  }

  @Test 
  public void nextPageScrollDown() throws InterruptedException{ 
      ((JavascriptExecutor)driver).executeScript("scroll(0, 400)");
      Thread.sleep(2000);
  }

  @Test 
  public void clickMorgageLoanPaymentCalc() throws InterruptedException{  

      WebElement MorgageCalc = driver.findElement(By.linkText("Mortgage loan payment calculator"));  
      MorgageCalc.click();
      Thread.sleep(2000);
  }

  @Test 
  public void scrollDownCalcPage() throws InterruptedException{  

      ((JavascriptExecutor)driver).executeScript("scroll(0, 400)");
        Thread.sleep(2000);
  }

  @Test 
  public void clearMortgageSendKeys() throws InterruptedException{  

      WebElement MortgageField = driver.findElement(By.id("KJE-LOAN_AMOUNT"));
      MortgageField.clear();
      Thread.sleep(1000);

      WebElement LoanAmount = driver.findElement(By.name("LOAN_AMOUNT"));
      LoanAmount.sendKeys("$240,000");
      Thread.sleep(3000);
  }

Upvotes: 0

Views: 1787

Answers (3)

Mani
Mani

Reputation: 131

@Before ist executed before each @Test method @BeforeClass is what you want.

@BeforeClass
public static void setUpBeforeClass() throws Exception { 

}

To run the tests ordered you can use the @FixMethodOrder annotation.

Upvotes: 0

Arne Burmeister
Arne Burmeister

Reputation: 20594

For a unit test that should never be required but you have an integration test workflow where this is somehow useful to get more details failures.

You can use the FixMethodOrder annotation on the test class but you would need to name your methods in a proper way. I would suggest a prefix to your current names like step1_, step2_ etc.

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BankrateAlt {
  @Test 
  public void step1_moveToElementMortgages() {…}
  @Test
  public void step2_clickOnCalculators() {…}
  …
}

As the setup is done per method you have to switch to the one per class behaviour of @BeforeClass (has to be static).

Upvotes: 1

Augusto
Augusto

Reputation: 29887

JUnit doesn't run tests in order, as unit tests should be able to run in isolation (and as a consequence in any order).

This is a trivial fix, but something you can do is remove all the @Test annotations from all methods and add the following test:

@Test 
public void fullTest() {
  moveToElementMortgages();
  clickOnCalculators();
  nextPageScrollDown(); 
  clickMorgageLoanPaymentCalc();
  scrollDownCalcPage();
  clearMortgageSendKeys();
}

If you want to do this in a more refined way, do read about Page Object pattern. Or the more recent Screenplay pattern.

Upvotes: 3

Related Questions