Aakash Goyal
Aakash Goyal

Reputation: 305

Unable to run TestNG test case in Eclipse with ChromeDriver

I have written below code to open a site in chrome browser and verify its title. but when using System.setProperty() to set the ChromeDriver Path, it gives me syntax error and when I commented the line I get:

java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property..

My Code :

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.Assert;
    import org.testng.annotations.Test;

   public class FirsttestNGFile {
    String BaseURL = "http://newtours.demoaut.com/";
    System.setProperty("webdriver.chrome.driver", "E:\\Automation Jars\\chromedriver_win32\\chromedriver.exe"); -- If I comment this line, I get Illegal state Exception for chromedriver path; if not commented , I get syntax error
    WebDriver driver = new ChromeDriver();  

    @Test
    public void verifyHomePageTitle() {     
    driver.get(BaseURL);
    String ExpectedTitle = "Welcome: Mercury Tours";
    String ActualTitle = driver.getTitle();
    Assert.assertEquals(ExpectedTitle, ActualTitle);
    driver.quit();          
    }
  }  

Upvotes: 0

Views: 2045

Answers (3)

Sudheer Kumar R
Sudheer Kumar R

Reputation: 1

Update chrome driver path in environmental variables path and then try to use below code in your script

@BeforeClass
public void setup() {
    WebDriver driver = new ChromeDriver();
}

Upvotes: 0

Kishan Patel
Kishan Patel

Reputation: 1383

You cannot define System.setProperty Globally. Use below code and try:

WebDriver driver;
@Before
public void browser(){
 System.setProperty("webdriver.chrome.driver", "D:\\Selenium\\CP-SAT\\Chromedriver\\chromedriver.exe"); 
 driver = new ChromeDriver(); 
}

@Test
public void verifyHomePageTitle() {     

    String BaseURL = "http://newtours.demoaut.com/";
    driver.get(BaseURL);
    String ExpectedTitle = "Welcome: Mercury Tours";
    String ActualTitle = driver.getTitle();
    Assert.assertEquals(ExpectedTitle, ActualTitle);
    }

@Test
public void a() {
    driver.get("https://www.google.co.in/?gfe_rd=cr&ei=6PDbV-qTAZHT8gecr4qQBA");    
} 

@After
public void close(){
    driver.quit();
    }
}

If you are using Junit then use @Before or If you are using TestNG then @BeforeTest.

Reply me for further query. Happy Learning. :-)

Upvotes: 1

juherr
juherr

Reputation: 5740

You should consider to use https://github.com/bonigarcia/webdrivermanager which will make the job for you:

ChromeDriverManager.getInstance().setup();

Upvotes: 1

Related Questions