Search code examples
javamavenselenium-webdriverintellij-ideaide

Not able to use the Reference Variable to access methods in Intellij Maven project


I'm new to coding and I will try to be as specific as possible. I will attach images for the steps I performed.

  1. I created a maven project in IntelliJ. New Project and what I selected to create the project
  2. The POM.xml file which was created automatically after I created the project. Auto generated pom.xml
  3. I added the Selenium and Testng dependencies to the POM.xml and removed the JUNIT dependency as I wont be using it. Testng and Selenium Dependency
  4. The Libraries are added to the project.
  5. When I create a WebDriver class reference in the Test.java its give me suggestions. I create a reference variable. WebDriver reference variable
  6. When I try to use the reference variable, it does not work. It wont give me any suggestions nor can I use any methods with it. WebDriver Reference Variable which can't be used

I'm not sure why this is happening and I have been trying to figure out what wrong with no luck.

  1. I have tried deleting the class and creating a new one in both main/java and test/java
  2. I have tried deleting the project and creating a new one.
  3. Tried rebuilding the project.
  4. Removing the dependencies and adding them again.

Can anyone please help me with this? I have not used IntelliJ before and I'm not sure why it keeps happening.


Solution

  • The problem is that you are typing code outside of a method. Your code is in the area reserved for class variable definitions, etc.

    Change your code to the below and it should work.

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    public class AppTest {
        private int i = 0; // class variables/fields go here, not code.
    
        public static void main(String[] args) throws Exception {
            String url = "https://www.google.com";
    
            WebDriver driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get(url);
    
            driver.findElement(By.cssSelector("textarea[name='q']")).sendKeys("Selenium/r/n");
        }
    }