ChromeDriver and WebDriver for Selenium through Te

2019-01-29 14:03发布

enter image description here

I'm doing exactly what the website says but it results in 4 errors...

2条回答
何必那么认真
2楼-- · 2019-01-29 14:41

From your code, i didn't see any imports and class name was different from filename(As @yong mentioned in his comment).

Your Java file name should always reflect the public class defined within that file. Otherwise, you will get a compiler error.

Modify your code like this : (you had a typo, firstPackage)

package firstPackage;

import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeTest {

    @Test
    public void LaunchChrome_method(){
        System.setProperty("webdriver.chrome.driver","D:\\Drivers\\chromedriver.exe"); 
        WebDriver driver= new ChromeDriver();
        driver.get("http://www.google.com");
    }

}
查看更多
不美不萌又怎样
3楼-- · 2019-01-29 14:46

There are exactly 4 errors as follows:

  • Error: ChromeDriver cannot be resolved to a type

  • Error: Test cannot be resolved to a type

    • Solution: You need to add the following import

      import org.testng.annotations.Test;
      //or
      import org.junit.Test;
      
  • Error: The public type ChromeTest must be defined in its own file

  • Error: WebDriver cannot be resolved to a type

    • Solution: You need to add the following import

      import org.openqa.selenium.WebDriver;
      

Best Practice

  • You have to keep the filename (currently Chrom.java) and your classname (currently ChromeTest) identical as a mandatory measure.
  • You need to mention the related imports whenever you are using any class. You can Mouse Hover over the error and choose the relevant import.
  • You should either add the testng jars or the junit jars but not both of them.
查看更多
登录 后发表回答