Shaikh Farzan
Shaikh Farzan

Reputation: 83

In my Junit test case, all test after the first one fail

So I have this basic JUnit Program, In the @beforeclass i open the browser and maximize it and then open the browser navigate to a site and check for the element. However only the first case is executed always. All the subsequent test cases fails. Any anyone tell me what mistake I am doing here

Code:

package JUnitTesting;

import static org.junit.Assert.*;

import java.util.concurrent.TimeUnit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FlockTeamURLDomain {
    static WebDriver driver;
    String TeamUrl = "http://farzanshaikh.flock.co/";
    String TeamName = "<script>farzan</script>";
    String TeamDomain = "farzanshaikh.com";

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        Thread.sleep(2000);

    }

    @Test
    public void OpenTheTeamURL() throws InterruptedException {
        driver.get(TeamUrl);
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
        String Title = driver.getTitle();
        System.out.println("The title of the Page is: "+Title);
        if(Title.equals("Flock - Team")){
            System.out.println("The Title is Correct");
        }
        else{
            System.err.println("The Title is InCorrect");
        }
        Thread.sleep(2000);
    }
    @Test
    public void CheckTheFooter() {
        boolean FlockFooter = driver.findElement(By.cssSelector("//div[@id='team-page']/div[2]/div[5]")).isDisplayed();
        System.out.println("Is the Footer Present? "+FlockFooter);

    }
    @Test
    public void CheckAndClickLogo() {
        boolean FlockLogo = driver.findElement(By.xpath("//div[@id='team-page']//img")).isDisplayed();
        System.out.println("Is Flock Logo Displayed "+FlockLogo);
    }
    @Test
    public void CheckTheHeader() {
        boolean FlockHeaderLogo = driver.findElement(By.xpath("//div[@id='step-2-block']/span")).isDisplayed();
        System.out.println("Is the Header Element Present? "+FlockHeaderLogo);

    }



    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        Thread.sleep(3000);
        driver.quit();
    }



}

Upvotes: 0

Views: 584

Answers (3)

mohamed faisal
mohamed faisal

Reputation: 177

I think this is because you are navigating to the required URL only in your first test and no other tests are navigating to the required URL
try adding the navigation step to the setUpBeforeClass method

try below :

package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class FlockTeamURLDomain {
    static WebDriver driver;
    String TeamUrl = "http://farzanshaikh.flock.co/";
    String TeamName = "<script>farzan</script>";
    String TeamDomain = "farzanshaikh.com";

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        Thread.sleep(2000);
        driver.get(TeamUrl);

    }

    @Test
    public void OpenTheTeamURL() throws InterruptedException {
        driver.manage().timeouts().implicitlyWait(3000, TimeUnit.SECONDS);
        String Title = driver.getTitle();
        System.out.println("The title of the Page is: "+Title);
        if(Title.equals("Flock - Team")){
            System.out.println("The Title is Correct");
        }
        else{
            System.err.println("The Title is InCorrect");
        }
        Thread.sleep(2000);
    }
    @Test
    public void CheckTheFooter() {
        boolean FlockFooter = driver.findElement(By.cssSelector("//div[@id='team-page']/div[2]/div[5]")).isDisplayed();
        System.out.println("Is the Footer Present? "+FlockFooter);

    }
    @Test
    public void CheckAndClickLogo() {
        boolean FlockLogo = driver.findElement(By.xpath("//div[@id='team-page']//img")).isDisplayed();
        System.out.println("Is Flock Logo Displayed "+FlockLogo);
    }
    @Test
    public void CheckTheHeader() {
        boolean FlockHeaderLogo = driver.findElement(By.xpath("//div[@id='step-2-block']/span")).isDisplayed();
        System.out.println("Is the Header Element Present? "+FlockHeaderLogo);

    }



    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        Thread.sleep(3000);
        driver.quit();
    }



}

Upvotes: 1

adee alamsz
adee alamsz

Reputation: 393

If I understand correctly you wanted the test to execute what were left off by previous test right, then you need to make sure the execution sequence is correct, although IMHO this is not how test should be created

I think it is better to put it in sequence

def smoke_test_flock_team_domain():
""" test to check completeness of the page """
       open the page
       assert title
       assert footer
       assert header
       assert logo
       click logo and assert result

Upvotes: 0

GhostCat
GhostCat

Reputation: 140417

BeforeClass runs only once.

Try to use @Before instead. That annotation makes the method to be run before each test case.

Upvotes: 0

Related Questions