1

I am using selenium webdriver , via Java & TestNG.

I've just tried the following code: (for starting chrome browser),

package testng1package;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.AssertJUnit;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import org.testng.annotations.AfterTest;


public class TestNGFile {

    //using firefox
    //public WebDriver driver = new FirefoxDriver() ;

    //using Chrome
    System.setProperty("webdriver.chrome.driver", "C://Users//Roey//Desktop//chromedriver.exe");
    public WebDriver driver = new ChromeDriver();   
    String baseurl = "http://newtours.demoaut.com/" ;

    @BeforeTest
    public void StartBrowser() {

    }

    @Test
    public void Test1() {

        driver.get(baseurl);
        String expectedTitle = "Welcome: Mercury Tours" ; 
        String actualTitle = driver.getTitle();
        AssertJUnit.assertEquals(actualTitle , expectedTitle) ;
        driver.quit();        
    }

    @AfterTest
    public void terminateBrowser() {
        driver.quit();              
    }

}

the test contain error on the system.setproperty, and says:

Multiple markers at this line
- Syntax error on token(s), misplaced construct(s)
- Syntax error on tokens, delete these tokens

If I am cutting and pasting this code line into the @test - it's ok, but I want to use it from the @BeforeTest or the beginning ( as it is it now).

1
  • 1
    You need to show us more code. Also post the entirety of the error stack. Commented Oct 20, 2014 at 17:14

4 Answers 4

1

EDIT:

Ok so first setup your driver in a method.
Secondly the path to your chromedriver on windows will need backslashes, not forward slashed.

This works.

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

public class TestNGFile {
    @Test
    public void Test1() {
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Roey\\Desktop\\chromedriver.exe");
        WebDriver chromeDriver = new ChromeDriver();
        String baseurl = "http://newtours.demoaut.com/" ;
        chromeDriver.get(baseurl);
        String expectedTitle = "Welcome: Mercury Tours" ;
        String actualTitle = chromeDriver.getTitle();
        AssertJUnit.assertEquals(actualTitle , expectedTitle) ;
        chromeDriver.quit();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

tnx , its working that way , but is there way to put the chrome driver under 'BeforeTest' - and use it during the @Test ?
Do you really need to do anything in a @BeforeTest? Just declare WebDriver chromeDriver at a class level.
0
    System.setProperty("webdriver.chrome.driver", "C:/Users/dell/Downloads/chromedriver.exe");
    WebDriver d = new ChromeDriver();
    d.get("Any URL");

Note-In the location of chromedriver.exe single forward slash would do.Hope this works fine for you.

Comments

0

I am using Eclipse. I place the chromedriver.exe in the project workspace, you don't need the full path in System.setProperty then as Selenium knows where to look. I then set it in the @Before.

@Before
public void setUp() {
    System.setProperty("webdriver.chrome.driver", "chromedriver.exe");
    driver = new ChromeDriver();
}

Comments

0

If you use Maven add these 2 dependencies in your pom.xml and you will be fine and now you can remove the System.setProperty line. With this technique the project has less hardcode method.

 <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>3.3.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.2</version>
        <scope>test</scope>
    </dependency>

Also you need to add this line to you preject.

        WebDriverManager.chromedriver().setup();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.