@BeforeClass does not start the tests

637 views Asked by At

@BeforeClass does not start my tests in Webdriver, Java, and I don't know where do I go wrong

@BeforeClass
public static void setup() {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get(baseUrl + "login");
    driver.findElement(By.id("username")).sendKeys("myUserName");
    driver.findElement(By.id("password")).sendKeys("myPassword"); 
    driver.findElement(By.id("loginBTN")).click();
}

After the code I start the regular testing:

@Test
public void firstTest() {
    //myTestCode
}

After attempting to run, all tests fail, the webdriver does not start, etc...

It would be nice to have this since I have to test a page where I have to be logged in (with @Before the webdriver starts before each test, so obviously I would need the @BeforeClass for this one.)

2

There are 2 answers

2
Zach On
@BeforeClass
public static void setup() {

//This needs to be here for this to run and having this here means its only local to this method
Webdriver driver;

driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(baseUrl + "login");
driver.findElement(By.id("username")).sendKeys("myUserName");
driver.findElement(By.id("password")).sendKeys("myPassword"); 
driver.findElement(By.id("loginBTN")).click();

}

Then your Test will work

@Test

public void firstTest() {
//myTestCode

}

1
Anand Somani On

Sample Code : Hopes this will work.

public class OpenBrowsers {

WebDriver driver = null;

@BeforeClass
public void beforeClass() {
    System.out.println("beforeClass");
    driver = new FirefoxDriver();
}

@Test
public void openGoogle() {
    System.out.println("openGoogle");
    driver.get("www.google.com");
}

@Test
public void openYahoo() {
    System.out.println("openYahoo");
    driver.get("www.yahoo.com");
}

@AfterClass
public void afterClass() {
    driver.close();
    System.out.println("afterClass");
}}