-3

I know that this question was already asked thousand times, but I still can't fully understand the point of the problem, especially in my case. So, I have simple project with dependency of TestNG and Selenium Java libraries, and I have these libraries installed globally, so my project just import them from "global" scope.

So to solve the problem I should add that global folder to my classpath ? Or this in not right from the beginning and I should not use libraries globally in projects ?

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();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Wonderio619
  • 135
  • 1
  • 3
  • 11
  • What do u mean by "Globally"? – Infamous Nov 10 '18 at 13:03
  • In "Project structure" window, there are "Global libraries" in "Platform settings". I installed libraries from there before starting the project, so all imports where available, and those libraries appeared in "Libraries" in "Project settings". And also in "Modules" -> "Dependencies". I mean libraries is not in my project structure, they in some separate IntelliJ IDEA folder. – Wonderio619 Nov 10 '18 at 13:08
  • Can you show the code and the exception that you are getting while building?. Plus, I honestly advise you to use something like Gradle or Maven to manage your dependencies and the building process rather than relying heavily on IDE tools. Javac is a command used to compile the code provided by JDK itself (not by IDE), and javac doesn't know about the classpath (IDE does, that's why you can run the project from the IDE run button). – Infamous Nov 10 '18 at 13:31
  • I updated question with exceptions and class I tried to compile. I also moved my dependency libraries in lib folder in project structure. – Wonderio619 Nov 10 '18 at 13:44
  • 1
    Possible duplicate of [how to include libraries in java without using an IDE](https://stackoverflow.com/questions/5112607/how-to-include-libraries-in-java-without-using-an-ide) – Infamous Nov 10 '18 at 13:50
  • Basically, getting the relavant jars into the classpath should do it, BUT you'll probably write more than one class of code and it will be tiresome to do this javac and then java thingy repeatedly. Try learning maven or Gradle; :D – Infamous Nov 10 '18 at 13:54
  • Sorry man, but I still wondering how it should look, I mean is this will be right - javac -classpath E:\Path\to\lib\* E:\Path\to\class\to\compile\ GoogleSearchTest.java – Wonderio619 Nov 10 '18 at 13:57
  • This is how it worked, finally. javac -classpath C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\lib\* GoogleSearchTest.java – Wonderio619 Nov 10 '18 at 14:15
  • But what is wrong with this line now - java -cp C:\Users\Yaroslav\IdeaProjects\GoogleSearchTest\src\main\java\ org.testng.TestNG testng.xml, it fails with Error: Could not find or load main class org.testng.TestNG – Wonderio619 Nov 10 '18 at 14:24
  • Run it with the testNG "JAR" not with the testng.xml – Infamous Nov 10 '18 at 14:29
  • Infamous, you can check my answer to this question now ... Clearly I should do it another way. – Wonderio619 Nov 10 '18 at 14:41
  • The answer you have provided is specific to your question and anyone who stumbles upon this answer will have a hard time understanding what really is the solution to this particular question. In your answer, provide reasons what was wrong, what causes this and how your answer solves this. The current answer belong to a comment not to an answer – Infamous Nov 10 '18 at 14:49
  • Infamous, after all, I just learn Maven and there is no need for these commands now ) – Wonderio619 Nov 13 '18 at 08:32

1 Answers1

0

I finally understand how it should be done. So, to compile class and then run TestNG test I done this:

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

More detailed, in first line to compile class there should be a following command template:

javac -cp "full path to libs folder, where project libraries located" "name of class to compile"

And for second line, which run TestNG test, template is:

java -cp "full path to folder where testng.xml file located";"full path to libs folder, where project libraries located" "testNG filename with extension"

And this is very tiresome, as you can see. I should learn proper way to run similar tests without headache ...

P.S. After all, I just learn Maven and there is no need for these commands now )

Wonderio619
  • 135
  • 1
  • 3
  • 11