Reputation: 339
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).
Upvotes: 1
Views: 2746
Reputation: 2240
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();
Upvotes: 0
Reputation: 234
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();
}
Upvotes: 0
Reputation: 1
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.
Upvotes: 0
Reputation: 1005
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();
}
}
Upvotes: 1