FileUtils not showing suggestion to import predefined class for Screenshot functionality in selenium WebDriver - selenium

I am not allowed to use FileUtils in the program and error is shown when doing so. Even no suggestion is showing to import this pre-defined Class. I tried to search the solution but what I found was to import the class. But in my case even suggestion is not showing to import any class. Hovering on the "FileUtils" shows the suggestion to create FileUtils class/interface. Below is my code:
package captureScreenshot;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils; //Getting Error at this line
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import com.google.common.io.Files;
public class FacebookScreenshot {
#Test
public void captureScreenshot() throws IOException
{
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.facebook.com");
driver.findElement(By.xpath("//input[#name='firstname']")).sendKeys("Anil Kumar");
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyfile(source,new File("./Screenshots/facebook.png")); //Getting error at this line
driver.quit();
}
}

FileUtils Class
FileUtils Class is defined in org.apache.commons.io.FileUtils which provides the general file manipulation utilities in the following areas :
writing to a file
reading from a file
make a directory including parent directories
copying files and directories
deleting files and directories
converting to and from a URL
listing files and directories by filter and extension
comparing file content
file last changed date
calculating a checksum
org.apache.commons.io is bundled along with selenium-server-standalone-x.y.z by default and available ready to use.
But the behavior you are observing is pretty much inline with your usecase where you mentioned that you are not allowed to use FileUtils in the program. It can be either of the scenarios as mentioned below :
Incase you are using JARs from selenium-java-3.9.1 client, the JAR containing org.apache.commons.io is not being added to your project.
Incase you are using Maven with selenium-java-3.9.1 client dependency the modules containing FileUtils Class have been excluded.
For the following above mentioned reasons, when you mention FileUtils in your program it doesn't show any suggestion to import the class. Moreover if you forcefully provide the import, it will show error on that line.

The line FileUtils.copyFile(); has been updated to FileHandler.copy()

Yes with Selenium Latest version we should use FileHandler.copy()
It works and doesn't throw any error.
// Take Screenshots example
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileHandler.copy(scrFile, new File("<your path>\\screenshot.png"));

download maven commons-io jars from: https://mvnrepository.com/artifact/commons-io/commons-io
and add jars to your build Path in your project

In the above FileHandler.copy(scrFile, new File("\screenshot.png"));
here copy -the method copy(File, File) is undefined for the type FileHandler is occured

Related

Selenium - Import devtools

I am capturing network HTTP requests. This technique is discussed here https://applitools.com/blog/selenium-4-chrome-devtools/#h.bc0ghombcetz
When I try to import the selenium class for 'Network', I get several options :
-org.openqa.selenium.devtools.v103.network.Network
-org.openqa.selenium.devtools.v102.network.Network
-org.openqa.selenium.devtools.v101.network.Network
How I could pick a "general" import (without specifying the version) because I dont want to change with each new release the import version? Any ways to do that?

IntelliJ changes npm pkg import paths on refactor move

When I move a file or directory, IntelliJ fixes all relative import paths according to the new path.
Since last update the package imports also get changed, for example:
import { fromJS } from 'immutable';
import glamorous from 'glamorous';
changes to
import { fromJS } 'immutable/dist/immutable-nonambient';
import glamorous from 'glamorous/typings/glamorous';
I get that the named imports (or lack of) probably match in both cases, but since the imports are absolute it doesn't really make sense to change the import paths.
Is there any way to disable this?

intellij cannot resolve symbol "File"

I have this in my script
import groovy.io.FileType
.....
derp = new File("blah")
The script works but inetillij complains it cant resolve "File" and it suggested I import a totally different wrong library for it (com.jidesoft.icons.IconSet)
I already tried invalidating cache and restarting
How do I get intelllij to import groovy.io.FileType? I cant even find a way to suppress error either it doesnt give me that option
groovy.io.FileType is an enum class. It appears your variable derp would be of type File, not FileType.
You can statically import enums from the FileType class (for example):
import static groovy.io.FileType.*
In my Intellij on Java 8 the File class comes from the java.io package in a .groovy file.

Selenium/Python:TypeError:undound method get()

I'm a new in Python with Selenium. I tried to test my first python/selenium code and got an error.
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
import time
# Create a new instance of the Firefox driver
driver = webdriver.Firefox
# go to the google home page
driver.get("http://www.google.com")
Here, I've got the error:
**TypeError:unbound method get() must be called with Webdriver instance as first argument (got str instance instead)**
Anybody knows how to tackle this issue?
Thanks in advance!
You need () after webdriver.Firefox to actually call the class's constructor and create an instance.

Selenium tool for UI

I installed selenium tool in my firefox. I recorded the login details testcases in selenium and saved it, whenever i want to test the login module i used to play the recorded testcases. Its quite simple and easy.
I want to improve the code wise testing using selenium.
Could you please help me how to perform that?
Take a look at:
SeleniumHQ
Documentation
Please make your question a little bit more specific.
basic steps to begin writing selenium tests are the following:
1) start with selenium IDE in ffox (as you mentioned in your quiestion)
2) record test
3) export your test as java/junit4/webdriver
4) set up e.g. intelij IDEA ide in the following way:
-set up maven.dont forget about environment variables
- open idea > project structure. select your installed JDK
- create project > crate project from scratch
- maven module
- create from archetype ( tciked checkbox).
select maven-archetype-quickstart. press finish
- you've created new project. Now add approptiate dependency in POM.xml
<dependency> <groupId>org.seleniumhq.
selenium</groupId> <artifactId>selenium-
java</artifactId> <version>2.24.1</version>
</dependency>
-select red underlined pieces of code. press alt+enter . maven gonna update repository indices
- now you can copy/past to the create project your recorded selenium test. it gonna be smth like that:
import com.thoughtworks.selenium.SeleneseTestBase;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
public class HomePageTest extends SeleneseTestBase{
static WebDriver driver;
#Before
public void openFirefox(){
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
#Test
public void testHomePage(){
driver.get("https://www.google.by/");
WebElement search = driver.findElement(By.xpath("//*[#id=\"gbqfq\"]"));
search.sendKeys("laptop");
search.submit();
}
#After
public void closeFirefox(){
// driver.quit();
}
}
press run. enjoy)