Unable to import Java Packages

resolved

#1

Hi Team,

We are trying to write a function for Capturing screenshot using Selenium Java. But we are unable to import java packages. Please find the attached sample code below. When we try to run this, it throws a compilation error.

public void GetScreenshot()
{
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//The below method will save the screen shot in d drive with name “screenshot.png”
FileUtils.copyFile(scrFile, new File(“C:\screenshot.png”));
}


Release notes v0.4.0
#2

Hi @saravanan_palanivel!

Currently, the automatic code fix for importing java packages is not supported.
After adding the following package imports to your code, and modify the checked exceptions it worked for me.

// Ranorex Webtestit Test File

package uitest.tests;

import uitest.TestNgTestBase;
import uitest.pageobjects.*;
import org.testng.annotations.Test;

//those were missing
import java.io.*;
import org.openqa.selenium.*;
import org.apache.commons.io.*;

class TC1 extends TestNgTestBase {
  @Test
  public void checkForTotalAmountUsing3Items() throws IOException {
    ItemsOverviewPo overview = new ItemsOverviewPo(driver).open("http://demoshop.webtestit.com/");

    GetScreenshot();
  }

  public void GetScreenshot() throws IOException
  {
    File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(scrFile, new File("C:\\tmp\\screenshot.png")); 
  }
}

We have the auto import feature already on our backlog, thanks for pointing our attention to this.


#3

Hi @mgrundner,

I tried the following code, which you mentioned in the previous comment. But while executing am getting this error. Please find the attached error log.

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.7.0:testCompile (default-testCompile) on project webtestit: Compilation failure: Compilation failure:
[ERROR] /D:/webtestit/src/test/java/uitest/pageobjects/newpofile.java:[53,24] cannot find symbol
[ERROR] symbol: class TakeScreenshot
[ERROR] location: class uitest.pageobjects.newpofile
[ERROR] /D:/webtestit/src/test/java/uitest/pageobjects/newpofile.java:[54,7] cannot find symbol
[ERROR] symbol: variable FileUtils
[ERROR] location: class uitest.pageobjects.newpofile
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException

I am not sure what i am missing, I added the Java package imports in my code, still i get this error in my line "[Java] File cannot be resolved to a type"

Thanks,
Saravanan


#4

Hi @saravanan_palanivel!

Can you provide the contents of the ‘newpofile.java’ class? it is difficult to determine only with the stack trace.

Thanks
Manuel


#5

Hi @mgrundner,

Please find the code below,

// Ranorex Webtestit Page Object File

package uitest.pageobjects;

import org.apache.commons.io.IOExceptionWithCause;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.By;

import java.io.*;

public class newpofile {
  protected WebDriver driver;
  protected WebDriverWait wait;

  // Generated elements
  private By Log_In = By.xpath("/html//div[@id='header']/div/div[2]/ul//a[@href='app']");
  private By Email_address = By.xpath("/html//input[@id='loginUsername']");
  private By Password = By.xpath("/html//input[@id='loginPassword']");
  private By signin_button = By.xpath("/html//input[@id='signin_button']");
  

  // Constructor
  public newpofile(WebDriver driver) {
    this.driver = driver;
    this.wait = new WebDriverWait(driver, 10);
  }

  public newpofile open(String url) {
    this.driver.get(url);

    return this;
  }

  // Generated actions

  public newpofile performNewAction1() {
    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Log_In)).click();

    GetScrenn();

    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Email_address)).sendKeys("Sample");

    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Password)).sendKeys("sym");

    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.signin_button)).click();

    return this;
  }

  public void GetScrenn() throws IOExceptionWithCause
  {
      File scrfile = ((TakeScreenshot)driver).getScreenshotAs(OutputType.FILE);
      FileUtils.copyFile(scrfile, new File("C:\\Screenshot.png"));
  }
}

Thanks,
Saravanan


#6

Hi @saravanan_palanivel!

You’ve got a few mistakes in your code:

  1. The class is called TakesScreenshot (note the ‘s’)
  2. IOExceptionWithCause is deprecated, use IOException instead
  3. FileUtils are located in the org.apache.commons.io package, so I imported all of them using the import org.apache.commons.io.*;
  4. Cause GetScrenn may throw an exception you need to change the signiture of performNewAction1 to check the exception. public newpofile performNewAction1() throws IOException

Please see my corrected sample below.

// Ranorex Webtestit Page Object File

package uitest.pageobjects;

import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import org.openqa.selenium.By;

import java.io.*;
import org.apache.commons.io.*; //import the whole package

public class newpofile {
  protected WebDriver driver;
  protected WebDriverWait wait;

  // Generated elements
  private By Log_In = By.xpath("/html//div[@id='header']/div/div[2]/ul//a[@href='app']");
  private By Email_address = By.xpath("/html//input[@id='loginUsername']");
  private By Password = By.xpath("/html//input[@id='loginPassword']");
  private By signin_button = By.xpath("/html//input[@id='signin_button']");
  

  // Constructor
  public newpofile(WebDriver driver) {
    this.driver = driver;
    this.wait = new WebDriverWait(driver, 10);
  }

  public newpofile open(String url) {
    this.driver.get(url);

    return this;
  }

  // Generated actions

  public newpofile performNewAction1() throws IOException { //Check the exception
    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Log_In)).click();

    GetScrenn();

    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Email_address)).sendKeys("Sample");

    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.Password)).sendKeys("sym");

    this.wait.until(ExpectedConditions.visibilityOfElementLocated(this.signin_button)).click();

    return this;
  }

  public void GetScrenn() throws IOException //IOExceptionWithCause is deprecated so use IOException instead
  {
      File scrfile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //Typo in TakeScreenshot -> TakesScreenshot
      FileUtils.copyFile(scrfile, new File("C:\\Screenshot.png"));
  }
}

#7

Hi @mgrundner,

I have modified and tried your code but still i am getting an error. Below I have attached an image, on this line I am getting an error,

File

While importing java package, I am getting the below error,

Java_error

I am attching pom.xml file below, please do let us know if i am missing something.


4.0.0
uitest
webtestit
1-0
jar

<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.14.2</version>
    </dependency>
    <dependency>
        <groupId>ru.stqa.selenium</groupId>
        <artifactId>webdriver-factory</artifactId>
        <version>4.3</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.10.0</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>htmlunit-driver</artifactId>
        <version>2.29.2</version>
    </dependency>
    <dependency>
        <groupId>com.codeborne</groupId>
        <artifactId>phantomjsdriver</artifactId>
        <version>1.4.4</version>
        <exclusions>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-java</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.seleniumhq.selenium</groupId>
                <artifactId>selenium-remote-driver</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
            <filtering>true</filtering>
        </testResource>
    </testResources>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.21.0</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
                </suiteXmlFiles>
                <reportNameSuffix>${report.name.suffix}</reportNameSuffix>

                <systemPropertyVariables>
                    <application.properties>/application.properties</application.properties>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>
<!-- profiles -->
<profiles>
    <!-- browsers -->
    <profile>
        <id>firefox</id>
        <properties>
            <capabilities>/firefox.capabilities</capabilities>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>android</id>
        <properties>
            <capabilities>/android.capabilities</capabilities>
        </properties>
    </profile>
    <profile>
        <id>chrome</id>
        <properties>
            <capabilities>/chrome.capabilities</capabilities>
        </properties>
    </profile>
    <profile>
        <id>ie</id>
        <properties>
            <capabilities>/ie.capabilities</capabilities>
        </properties>
    </profile>
    <profile>
        <id>safari</id>
        <properties>
            <capabilities>/safari.capabilities</capabilities>
        </properties>
    </profile>
    <profile>
        <id>phantomjs</id>
        <properties>
            <capabilities>/phantomjs.capabilities</capabilities>
        </properties>
    </profile>
    <profile>
        <id>opera</id>
        <properties>
            <capabilities>/opera.capabilities</capabilities>
        </properties>
    </profile>
    <profile>
        <id>htmlunit</id>
        <properties>
            <capabilities>/htmlunit.capabilities</capabilities>
        </properties>
    </profile>
    <!-- environments -->
    <profile>
        <id>localhost</id>
        <properties>
            <site.url>http://localhost/</site.url>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>devhost</id>
        <properties>
            <site.url>http://devhost/</site.url>
        </properties>
    </profile>
    <profile>
        <id>testhost</id>
        <properties>
            <site.url>http://testhost/</site.url>
        </properties>
    </profile>
    <profile>
        <id>prodhost</id>
        <properties>
            <site.url>http://prodhost.com/</site.url>
        </properties>
    </profile>
    <!-- grid -->
    <profile>
        <id>nogrid</id>
        <properties>
            <grid.url></grid.url>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>grid</id>
        <properties>
            <grid.url>${grid.url}</grid.url>
            <remote.platform key="remote-platform">${remoteplatform}</remote.platform>
            <remote.version key="remote-version">${remoteversion}</remote.version>
        </properties>
    </profile>
</profiles>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

#8

Hi @saravanan_palanivel!

I checked your source and it seems fine.
Can you please check which version of the JDK is installed and whats on your JAVA_HOME path?

Maybe you got some old/incopatible version of the JDK installed that gets Webtestit confused.


#9

Hi @saravanan_palanivel!

We added the import package feature with v0.4.0!


archived #10