Jeffrey Lee
Jeffrey Lee

Reputation: 51

Not able to use TestNG test annotation without providing full package name

In the latest Intellij IDEA Ultimate, i'm not able to use the @Test annotation without getting red error lines. Only way it works is if I provide the full package name like below:

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class Test {
private WebDriver driver;

@BeforeTest
public void setup(){
    System.setProperty("webdriver.chrome.driver", "/Users/jeff/IdeaProjects/Practice/src/chromedriver");
    driver = new ChromeDriver();
    driver.get("http://google.com");
}

@org.testng.annotations.Test
public void test(){
    WebElement searchBox = driver.findElement(By.id("lst-ib"));
    searchBox.sendKeys("Hello World");
    searchBox.sendKeys(Keys.RETURN);
}

@AfterTest
public void tearDown() throws Exception{
    Thread.sleep(3000);
    driver.quit();
}
}

The other annotations work fine as you can see. And since I have the options Add unambiguous imports on the fly and optimize imports on the fly checked in the preferences, when I go ahead and add the following:

import org.testng.annotations.Test;

It gets greyed out since it's not in use...but it is.

Here is my maven pom.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.Practice</groupId>
    <artifactId>Practice</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.10</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-chrome-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-chrome-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-firefox-driver -->
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.mashape.unirest</groupId>
            <artifactId>unirest-java</artifactId>
            <version>1.4.9</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpasyncclient</artifactId>
            <version>4.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
            <version>4.3.6</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20140107</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

Upvotes: 1

Views: 2321

Answers (1)

CrazyCoder
CrazyCoder

Reputation: 401965

Your class is named Test itself, hence the fully qualified annotation class name is required to prevent the conflict. Rename the class to SomeTest and you will be able to use @Test annotation with import.

Upvotes: 2

Related Questions