0
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;


public class  Test {
    static WebDriver driver;
    String expectedurl="http://examplezone.com.au/account-page/";

    @BeforeClass
    public  static void setup()
    {
        System.setProperty("webdriver.chrome.driver", "E:\\Selenium Jar\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @org.junit.Test
    public  void test1_login(){
        driver.get("http://examplezone.com.au/");
        WebElement loginmain=driver.findElement(By.id("menu-item-14235"));
        loginmain.click();
    }

    @org.junit.Test
    public void test2_blankdata()
    {
        driver.get("http://examplezone.com.au/loginregister/");   
        //driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        String userblank=" ";
        String passblank=" ";

        WebElement username=driver.findElement(By.name("log"));
        username.clear();
        username.sendKeys(userblank);

        WebElement password=driver.findElement(By.name("pwd"));
        password.clear();
        password.sendKeys(passblank);

        WebElement submit=driver.findElement(By.name("Submit"));
        submit.click();

        String actual=driver.getCurrentUrl();

        Assert.assertEquals(expectedurl, actual);
    }

    @org.junit.Test
    public void test3_blankpassword()
    {
        driver.get("http://examplezone.com.au/loginregister/");   
    //  driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        String userblank="rutvisoni";
        String passblank="";

        WebElement username=driver.findElement(By.name("log"));
        username.clear();
        username.sendKeys(userblank);

        WebElement password=driver.findElement(By.name("pwd"));
        password.clear();
        password.sendKeys(passblank);

        WebElement submit=driver.findElement(By.name("Submit"));
        submit.click();

        String actual=driver.getCurrentUrl();

        Assert.assertEquals(expectedurl, actual);
    }

    @org.junit.Test
    public void test4_blankusername()
    {
        driver.get("http://examplezone.com.au/loginregister/");   
        //driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        String userblank=" ";
        String passblank="12Abcd";

        WebElement username=driver.findElement(By.name("log"));
        username.clear();
        username.sendKeys(userblank);

        WebElement password=driver.findElement(By.name("pwd"));
        password.clear();
        password.sendKeys(passblank);

        WebElement submit=driver.findElement(By.name("Submit"));
        submit.click();

        String actual=driver.getCurrentUrl();

        Assert.assertEquals(expectedurl, actual);
    }

    @org.junit.Test
    public void test5_invaliddata()
    {
        driver.get("http://examplezone.com.au/loginregister/");   
        //driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);

        String userblank="rutvisoni1";
        String passblank="12Abcdfdg";

        WebElement username=driver.findElement(By.name("log"));
        username.clear();
        username.sendKeys(userblank);

        WebElement password=driver.findElement(By.name("pwd"));
        password.clear();
        password.sendKeys(passblank);

        WebElement submit=driver.findElement(By.name("Submit"));
        submit.click();

        String actual=driver.getCurrentUrl();

        System.out.println("Invaliddata->"+actual);

        Assert.assertEquals(expectedurl, actual);
    }

    @org.junit.Test 
    public  void  test6_validdata(){
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        driver.get("http://examplezone.com.au/loginregister/");   

        WebElement username=driver.findElement(By.name("log"));
        username.clear();
        username.sendKeys("rutvisoni");

        WebElement password=driver.findElement(By.name("pwd"));
        password.clear();
        password.sendKeys("12Abcd");

        WebElement submit=driver.findElement(By.name("Submit"));
        submit.click();

        String actual=driver.getCurrentUrl();

        Assert.assertEquals(expectedurl, actual);       
    }

    @AfterClass
    public static void test7_close()
    {
        driver.close();
    }
}

I have created testcases for the login page using JUnit in eclipse IDE. I want to run function sequentially like this way-

test2_blankdata()
test3_blankpassword()
test4_blankusername()
test5_invaliddata()
test6_validdata()

But it's not working, Please go through the above code and give me any suggestions on this problem?

N00b Pr0grammer
  • 4,503
  • 5
  • 32
  • 46
Rutvi Soni
  • 116
  • 4

3 Answers3

1

Unit tests should be stateless. You can not and should not guarantee which order they will run. If you need to run in a specific order, then you are doing something wrong. Any unit test case should not rely on another. Moreover, any unit test should not "pollute" the testing environment. You should setup the test environment before start, execute the test case, and clean up any dirty work afterwards.

An easier way to do unit testing would be mock testing.

EDIT:

Hmm.You are not really doing junit testing. This is more like web testing/functional testing. My bad. I should have paid more attention.But principles still remain the same. Setting up the test environment (even if it requires clicking this and that links, entering some data somewhere, hitting a button etc. Perform the test case. Assert the outcome. And then clean up).

Alp
  • 3,027
  • 1
  • 13
  • 28
1

Just have one test and have this test call the methods in order:

@org.junit.Test 
public  void  test6_validdata(){
    test2_blankdata()
    test3_blankpassword()
    test4_blankusername()
    test5_invaliddata()
    test6_validdata()
}

This is mainly a question of how to layout your code. In order to do effective unit testing you should still stick to the common guidelines.

Matthias
  • 3,458
  • 4
  • 27
  • 46
0

Sorts the test methods by the method name by @FixMethodOrder(MethodSorters.NAME_ASCENDING)

Rutvi Soni
  • 116
  • 4