0

I am writing a class with the following code in a maven3.6.1 project. I am using JDK 1.8.201 and declared in maven plugins section to use JDK 1.8 for source and target.

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class MySeleniumTest {
    WebDriverManager.chromedriver().setup();//Syntax error on token ".", { expected
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.google.com");//Syntax error, insert "}" to complete Class
    driver.quit();
 }

I am getting the syntax errors as shown in the comments in the same line as the code, as shown above. Even after running the maven clean and update project multiple times in Eclipse, these errors won't go away. I could not figure out why these errors are coming.

PraNuta
  • 629
  • 3
  • 12
  • 33

2 Answers2

1

You have to put your code inside a method and you can not run directly from class scope

so modify your as like below it will run

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class MySeleniumTest {
   public static void main(String[] args) {
      WebDriverManager.chromedriver().setup();
      WebDriver driver = new ChromeDriver();
      driver.get("http://www.google.com");
      driver.quit();
   }
}

Also please check here for sample code

Sasikumar Murugesan
  • 4,412
  • 10
  • 51
  • 74
  • How dumb of me. Not because I am un aware of it, but I did not observe it. It is called "ineptitude". Knowing it, but not have the sense to use it. – PraNuta Jul 12 '19 at 17:22
0
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;

public class MySeleniumTest {
    public WebDriver driver;
    @Test
    void testMethodName(){
    WebDriverManager.chromedriver().setup();
    driver = new ChromeDriver();
    driver.get("http://www.google.com");
    driver.quit();
    }
    
 }

So here in the class, we can declare a public driver so that we can use the driver in all the methods of this class. Furthermore, I have created a method to run the test as it is required to be included in a class to run the test.