Pushpraj Singh
Pushpraj Singh

Reputation: 61

How to Initialise the chrome driver at the class level, using System.set...?

I have been using Firefox to run my test cases. But now I want to use Chrome. I want to initialize chrome at the class level, just like I was using Firefox. But setting system property at class level is giving error, what can I do? Using properties file would work, if yes, how??

public class BaseClass {
System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver"); 
public static WebDriver driver = new ChromeDriver();

public void test(){
driver.get("http://asdf.com");

----
---
 }

}

Upvotes: 2

Views: 20638

Answers (4)

Sriram
Sriram

Reputation: 449

Please declare it this way.. it should work

public class abcd {


public static WebDriver driver;


@BeforeMethod
public static void start() 
{
    File file = new File("D:/abcd/chromedriver.exe");
    System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
    driver = new ChromeDriver();
            }
}

It should work.. I had the same error. but when u initialize this way it works. Please try and let us know.

And also if u dont want to close the browser session try using @BeforeClass and @AfterClass. It runs once before the entire test once

Upvotes: 2

jabbrwcky
jabbrwcky

Reputation: 638

You cold do it with a static initializer block like this:

public class BaseClass {

  static {
    System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");
  }

  protected WebDriver driver = new ChromeDriver();

  @Test
  public void test(){
    driver.get("http://asdf.com");
  }
}

As you have not stated which test framework you are using you might do it like this in TestNG (which I would recommend anyway):

public class BaseClass {

  @BeforeSuite
  public void setupChromeDriver() {
    System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");
  }

  public static WebDriver driver = new ChromeDriver();

  public void test(){
    driver.get("http://asdf.com");
  }
}

The @BeforeSuite annotation ensures that the method is executed before the first test of a test suite is run, so this should be early enough anyway.

Upvotes: 4

Abhijeet Vaikar
Abhijeet Vaikar

Reputation: 1656

Why not try initializing Chrome Driver in a @BeforeTest method in your base class. What I have done is like this:

public class BaseTest { 

    /*
     * 
     * This is a base class for all Test classes that we'll create to write tests in.
     * A test-data set will belong to one/set of tests.
     */


    protected WebDriver driver;
    protected CustomLogger logger;
    protected DependencyChecker dcheck;
    protected TestDataReader td;
    protected PropReader p;
    protected HashMap<String, String> testDataMap;
    private String testDataFilePath;


    protected BaseTest(String testDataFilePath) 
    {
        this.testDataFilePath = testDataFilePath;
        p = new PropReader("environmentConfig.properties");
    }


    @BeforeTest(description="Preparing environment for the test..")
    public void prepareTest()
    {

        //other code
        System.setProperty(p.get("chromeDriverName"),p.get("chromeDriverPath"));
        File chrome = new File("/usr/bin/google-chrome");
        ChromeOptions options = new ChromeOptions();
        options.setBinary(chrome);
        logger.log("Launching browser..");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        //other code        
    } 
    }


I don't know why would you want to initialise it at the class level. The above code works perfectly fine.

Upvotes: 1

System.setProperty("webdriver.chrome.driver","/home/Desktop/chrome32/chromedriver");

this line should come inside a method , you cant use it directly inside your class body

Upvotes: 1

Related Questions