我知道这个问题已经问了上千遍了,但是我仍然不能完全理解问题的重点,特别是在我的情况下。因此,我有一个依赖于TestNG和Selenium Java库的简单项目,并且已在全球范围内安装了这些库,因此我的项目只是从“全局”范围导入它们。
所以要解决该问题,我应该将该全局文件夹添加到我的类路径中?还是从一开始就不合适,我不应该在项目中全局使用库吗?
C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\src\main\java>javac GoogleSearchTest.java
GoogleSearchTest.java:1: error: package org.openqa.selenium does not exist
import org.openqa.selenium.By;
^
GoogleSearchTest.java:2: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebDriver;
^
GoogleSearchTest.java:3: error: package org.openqa.selenium does not exist
import org.openqa.selenium.WebElement;
^
GoogleSearchTest.java:4: error: package org.openqa.selenium.chrome does not exist
import org.openqa.selenium.chrome.ChromeDriver;
^
GoogleSearchTest.java:5: error: package org.testng.annotations does not exist
import org.testng.annotations.BeforeClass;
^
GoogleSearchTest.java:6: error: package org.testng.annotations does not exist
import org.testng.annotations.Parameters;
^
GoogleSearchTest.java:7: error: package org.testng.annotations does not exist
import org.testng.annotations.Test;
^
GoogleSearchTest.java:12: error: cannot find symbol
private static WebDriver driver;
^
symbol: class WebDriver
location: class GoogleSearchTest
GoogleSearchTest.java:14: error: cannot find symbol
@BeforeClass
^
symbol: class BeforeClass
location: class GoogleSearchTest
GoogleSearchTest.java:23: error: cannot find symbol
@Test
^
symbol: class Test
location: class GoogleSearchTest
GoogleSearchTest.java:24: error: cannot find symbol
@Parameters("queryText")
^
symbol: class Parameters
location: class GoogleSearchTest
GoogleSearchTest.java:17: error: cannot find symbol
driver = new ChromeDriver();
^
symbol: class ChromeDriver
location: class GoogleSearchTest
GoogleSearchTest.java:26: error: cannot find symbol
WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
^
symbol: class WebElement
location: class GoogleSearchTest
GoogleSearchTest.java:26: error: cannot find symbol
WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
^
symbol: variable By
location: class GoogleSearchTest
GoogleSearchTest.java:28: error: cannot find symbol
WebElement searchButton = driver.findElement(By.name("btnK"));
^
symbol: class WebElement
location: class GoogleSearchTest
GoogleSearchTest.java:28: error: cannot find symbol
WebElement searchButton = driver.findElement(By.name("btnK"));
^
symbol: variable By
location: class GoogleSearchTest
16 errors
GoogleSearchTest.java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
public class GoogleSearchTest {
private static WebDriver driver;
@BeforeClass
public void setup () {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
}
@Test
@Parameters("queryText")
public void doSearch(String queryText) {
WebElement searchField = driver.findElement(By.cssSelector("#lst-ib"));
searchField.sendKeys(queryText);
WebElement searchButton = driver.findElement(By.name("btnK"));
searchButton.click();
}
}
答案 0 :(得分:0)
我终于明白应该怎么做。 因此,要编译类,然后运行TestNG测试,我做到了:
javac -cp C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\lib\* GoogleSearchTest.java
java -cp C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\src\main\java\;C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\lib\* org.testng.TestNG testng.xml
更详细的是,在编译类的第一行中,应该有以下命令模板:
javac -cp "full path to libs folder, where project libraries located" "name of class to compile"
第二行运行TestNG测试的模板是:
java -cp "full path to folder where testng.xml file located";"full path to libs folder, where project libraries located" "testNG filename with extension"
正如您所看到的,这非常烦人。我应该学习正确的方法来进行类似的测试而不会头痛...
P.S。毕竟,我只是学习Maven,现在就不需要这些命令了)