10

I am running several selenium automated tests using Maven. When I'm debugging in Eclipse, I usually just right click on testing.xml and Run As > TestNG Suite. But running in Jenkins needs to be ran using mvn test. But when I run that, I get several errors:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project ecom: Compilation failure: Compilation failure:
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[15,43] package com.company.automation.ecom.pages does not exist
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[16,43] package com.company.automation.ecom.pages does not exist
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[110,13] cannot find symbol
[ERROR] symbol:   class Header
[ERROR] location: class com.company.automation.ecom.HelperMethods
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[110,62] cannot find symbol
[ERROR] symbol:   class Header
[ERROR] location: class com.company.automation.ecom.HelperMethods
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[113,9] cannot find symbol
[ERROR] symbol:   class SignIn
[ERROR] location: class com.company.automation.ecom.HelperMethods
[ERROR] /Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[113,58] cannot find symbol
[ERROR] symbol:   class SignIn
[ERROR] location: class com.company.automation.ecom.HelperMethods

I know the files are present, when I run as TestNG suite everything works with no issues. Additionally, when I run which java -version I get this:

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)

I know it's a configuration issue somewhere, but I don't know enough about maven/java configuration to figure it out. The files that Maven is telling me are gone are my files, and they are absolutely present. Here is my pom.xml file:

<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>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <properties>
                        <property>
                            <name>listener</name>
                            <value>com.kirklands.automation.ecom.retry.MyTestListenerAdapter</value>
                        </property>
                    </properties>
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <groupId>com.kirklands.automation</groupId>
    <artifactId>ecom</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>ecom</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>io.github.bonigarcia</groupId>
            <artifactId>webdrivermanager</artifactId>
            <version>1.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
    </dependencies>
</project>

Project Structure:

src/main/java
    {package com.company.automation.ecom}
        CreditCard.java
        HelperMethods.java
src/test/java
    {package com.company.automation.ecom.pages}
        Header.java
        SignIn.Java
        (etc...)
    {package com.company.automation.ecom.tests}
        HeaderTests.java
        (etc...)
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
kroe761
  • 3,296
  • 9
  • 52
  • 81
  • Do you have the same issue when you run `mvn test` in your local environment? – alayor Apr 05 '17 at 19:42
  • Yeah. if I run `mvn test` from the command line (in the same folder as my pom.xml file) OR if I right click on the project in eclipse and Run As > Maven Test, I get the same output. – kroe761 Apr 05 '17 at 19:44
  • You need to switch your maven-compiler-plugin to use source/target 1.8 for java. Default is 1.5. See https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html – M. Deinum Apr 05 '17 at 19:44
  • Thanks very much, that solved one of my problems! I now no longer get the message about switching to source 7 or higher to enable strings in cases. But, I am still getting the `package does not exist` errors. – kroe761 Apr 05 '17 at 19:48
  • is `com.company.automation.ecom.pages` package in a different maven module? – alayor Apr 05 '17 at 19:50
  • I hate to be 'that guy'... what's a maven module? – kroe761 Apr 05 '17 at 19:55
  • Could you show your project structure? – alayor Apr 05 '17 at 20:00
  • Just added some of the project structure, indicating where the problem files are. Hope this helps... – kroe761 Apr 05 '17 at 20:05
  • If I'm reading that project structure correctly, your source files (CreditCard.java, HelperMethod.java) are in `src/main/java`, but should be in `src/main/java/com/company/automation/ecom` – Gus Apr 05 '17 at 20:32
  • @kroe761 is your issue solved? – catch23 Apr 06 '17 at 13:06
  • @nazar_art - Could you please help here https://stackoverflow.com/questions/52510682/maven-package-does-not-exist-for-enum ? – MasterJoe Sep 26 '18 at 06:00

2 Answers2

15

Looks like your issue appears due to fact that you trying to access from your:

src/main/java

Test sources - which are located:

src/test/java

Here is the exact snippet of the log you posted:

/Users/kroe761/Documents/workspace/ecom/src/main/java/com/company/automation/ecom/HelperMethods.java:[15,43] package com.company.automation.ecom.pages does not exist

It will work from another side: if you will use your sources (src/main/java) from test scope (src/test/java).

Maven has his own lifecycle.

It has a strict consequence:

  • compile - compile the source code of the project
  • test - test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed

And during compile you can compile only your sources. However, it depends on your tests (pages package), which can't be compiled at this moment, because it will compile only at test phase.
Thus compilation fails.

For solving try to change your project structure, a little bit:

src/main/java
    {package com.company.automation.ecom}
        CreditCard.java
        HelperMethods.java
    {package com.company.automation.ecom.pages}
        Header.java
        SignIn.Java
        (etc...)
src/test/java
    {package com.company.automation.ecom.tests}
        HeaderTests.java

And your tests should use sources (core & pages) without any problem.

catch23
  • 17,519
  • 42
  • 144
  • 217
  • 1
    Yes, that does appear to have solved it. Thanks for the maven clarification as well! – kroe761 Apr 06 '17 at 13:19
  • I have a similar problem. I'd prefer not to move the classes in Main into the test folder just for this purpose. Any other suggestions ? https://stackoverflow.com/questions/52510682/maven-package-does-not-exist-for-enum – MasterJoe Sep 26 '18 at 05:59
  • @testerjoe2 You asking me about suggestion and at the same time, I don't see any support for existing answer. – catch23 Sep 26 '18 at 11:40
3

You need to run maven clean install command first.

That command will compile your missing package and it will even run the tests.

UPDATE: You should change your com.company.automation.ecom.pages package to your src/main/java folder. so that it can be compiled in maven compile phase.

alayor
  • 4,537
  • 6
  • 27
  • 47
  • 2
    I tried running `mvn clean install` but I got the same error. – kroe761 Apr 05 '17 at 19:55
  • I have a similar problem. I'd prefer not to move the classes in Main into the test folder just for this purpose. Any other suggestions ? https://stackoverflow.com/questions/52510682/maven-package-does-not-exist-for-enum – MasterJoe Sep 26 '18 at 05:59