Selenium javax.imageio.IIOException: Can't read input file - selenium

I am facing this error for my Selenium script...
I am trying to generate the pdf evidence of the testing i am doing and while running my script i am getting this error
My Code:
package test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.googlecode.seleniumjavaevidence.report.GenerateEvidenceReport;
import com.googlecode.seleniumjavaevidence.selenium.SeleniumEvidence;
import java.util.List;
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.*;
public class NewTest {
WebDriver driver = new FirefoxDriver();
List<SeleniumEvidence> evidence ;
String exception ;
#BeforeTest
public void setUp() throws Exception {
driver.get("https://www.google.co.in");
}
#Test
public void testLogin() throws Exception {
try {
// driver.findElement(By.id("lst-ib"));
evidence.add(new SeleniumEvidence("get evidence from here",((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64)));
} catch (Exception e) {
evidence.add(new SeleniumEvidence("Unexpected error", ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64)));
exception = e.fillInStackTrace().getMessage();
} finally {
GenerateEvidenceReport.generatePDFEvidence(evidence, "null", null, null, exception);
}
}
}

https://code.google.com/p/selenium-java-evidence/wiki/HowToUse?wl=pt-BR
Seems to suggest:
A imageio.IOException will always appears if you don't put the images
in the correct path on your code. It's not a problem from
selenium-java-evidence... Revise your code and put all images in the
correct path.

Related

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Type mismatch: cannot convert from WebElement to List<WebElement>

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class AutoSuggestiveDropdown {
public static void main (String[] args) throws Exception {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\HP\\Downloads\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://rahulshettyacademy.com/dropdownsPractise/");
driver.findElement(By.id("autosuggest")).sendKeys("ind");
Thread.sleep(3000L);
List<WebElement> options = driver.findElement(By.cssSelector("li[class='ui-menu-item'] a")); //Error
for(WebElement option : options) {
if(option.getText().equalsIgnoreCase("India")) {
option.click();
break;
}
}
}
}
You used 'findElement' in 'driver.findElement(By.cssSelector("li[class='ui-menu-item'] a"))'
You have to use 'findElements' in 'driver.findElements(By.cssSelector("li[class='ui-menu-item'] a"));'

getWindowHandle() throws error message "Nullpointerexception"

this is a simple selenium function that is trying to get a windowhandle.
right at that statement it throws" Nullpointerexception"
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.AfterClass;
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.chrome.ChromeDriver;
public class WindowHandles {
WebDriver achromeDriver;
String abaseUrl;
public void setUp() throws Exception {
abaseUrl = "http://letskodeit.teachable.com/pages/practice";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();;
System.out.println("setup completed");
}
#Test
public void test() {
try{
String aparentwindowHandle = achromeDriver.getWindowHandle();
System.out.println("the parent window handle is "+ aparentwindowHandle);
WebElement aopenwindowelementbutton = achromeDriver.findElement(By.id("openwindow"));
aopenwindowelementbutton.click();
String achildwindowhandle = achromeDriver.getWindowHandle();
System.out.println("the child window handle is: " + achildwindowhandle);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
A bit more details about your usecase and your observation would have helped us to debug the issue in a better way. However, it seems as you are using the junit framework and in-absence of any of the annotations the setUp() method is never executed.
As the test() method is annotated with #Test the program reaches there with achromeDriver as Null
Solution
A quick solution would be to add #Before annotation to setUp() method as follows:
#Before
public void setUp() throws Exception {
abaseUrl = "http://letskodeit.teachable.com/pages/practice";
System.setProperty("webdriver.chrome.driver", "C:\\Selenium\\ChromeDirver\\chromedriver.exe");
achromeDriver = new ChromeDriver();
achromeDriver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
achromeDriver.manage().window().maximize();;
System.out.println("setup completed");
}

How to extract only text from a document(it may be a pdf,word or any other) using apache tika?

I am trying to extract only text from a pdf or word file that can contain images and other things as well using apache tika .How can I get only text from them?
What are the dependencies that i need in tika?
This the java code i have written:
package secondp;
import java.io.File;
import org.apache.tika.Tika;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tika.Tika;
import org.xml.sax.SAXException;
public class trial {
public static void main(final String[] args) {
try {
System.out.println(trial.convert("test.pdf"));
} catch (final Exception e) {
e.printStackTrace();
}
}
public static String convert(final String fileName) throws IOException, SAXException, TikaException {
try(final FileInputStream inputstream = new FileInputStream(new File(fileName))) {
final BodyContentHandler handler = new BodyContentHandler();
new PDFParser().parse(inputstream, handler, new Metadata(), new ParseContext());
return handler.toString().trim();
}
}
}

In my Junit test case driver.findElement(By.cssSelector) not being executed

For the Junit Test case, I am trying to open a browser, navigate to my site and the enter an email in an field. Although all my commands are correct, I cant understand why does it specifically stops and shows error for line 33 i.e. driver.findElement(By.cssSelector)
package JUnitTesting;
import static org.junit.Assert.*;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class BasicActions {
WebDriver driver;
String BaseUrl;
#Before
public void setUp() throws Exception {
//System.setProperty("webdriver.chrome.driver", "C:\\Automation\\chromedriver_win32\\chromedriver.exe");
driver = new FirefoxDriver();
BaseUrl = "https://www.flock.co/in/indexd/";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
#Test
public void test() {
driver.get(BaseUrl);
System.out.println("opening the base url");
driver.findElement(By.xpath("//div[#id='main-area']//input[#type='email']")).clear();
driver.findElement(By.cssSelector("._g-s-input>input")).sendKeys("testing#mailinator.com");
System.out.println("Entering a valid email id");
driver.findElement(By.xpath("//div[#id='main-area']/div[2]/div[2]//button[#class ='_g-s-button']")).click();
System.out.println("Redirecting to web.flock.co");
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}
Appropriate syntax to find element by css class is :
driver.findElement(By.cssSelector("input._g-s-input"));
I am assuming '_g-s-input' is your css class name, if not so, please replace it with appropriate css class name.

JUnit Parallel Testing

I'm trying to run a piece of code that I found online to run JUnit test in parallel but the only thing I've modified is the URL to hit my local host. Problem is I'm constantly getting the following error and even though I've setup selenium grid many times and know how to set paths etc. I don't know how to fix this one.
org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.ie.driver system property; for more information, see http://code.google.com/p/selenium/wiki/InternetExplorerDriver.
here's the code
package AutomationFrameWork;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.junit.runners.Parameterized;
import org.junit.runners.model.RunnerScheduler;
public class Parallelized extends Parameterized {
private static class ThreadPoolScheduler implements RunnerScheduler {
private ExecutorService executor;
public ThreadPoolScheduler() {
String threads = System.getProperty("junit.parallel.threads", "16");
int numThreads = Integer.parseInt(threads);
executor = Executors.newFixedThreadPool(numThreads);
}
#Override
public void finished() {
executor.shutdown();
try {
executor.awaitTermination(10, TimeUnit.MINUTES);
} catch (InterruptedException exc) {
throw new RuntimeException(exc);
}
}
#Override
public void schedule(Runnable childStatement) {
executor.submit(childStatement);
}
}
public Parallelized(Class<?> klass) throws Throwable {
super(klass);
setScheduler(new ThreadPoolScheduler());
}
}
package AutomationFrameWork;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.LinkedList;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Platform;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
#RunWith(Parallelized.class)
public class JUnitParallel {
private String platform;
private String browserName;
private String browserVersion;
private WebDriver driver;
#Parameterized.Parameters
public static LinkedList<String[]> getEnvironments() throws Exception {
LinkedList<String[]> env = new LinkedList<String[]>();
env.add(new String[]{Platform.WINDOWS.toString(), "chrome", "27"});
env.add(new String[]{Platform.WINDOWS.toString(),"firefox","20"});
env.add(new String[]{Platform.WINDOWS.toString(),"ie","11"});
env.add(new String[]{Platform.WINDOWS.toString(),"safari","5.1.7"});
//add more browsers here
return env;
}
public JUnitParallel(String platform, String browserName, String browserVersion) {
this.platform = platform;
this.browserName = browserName;
this.browserVersion = browserVersion;
}
#Before
public void setUp() throws Exception {
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability("platform", platform);
capability.setCapability("browser", browserName);
capability.setCapability("browserVersion", browserVersion);
capability.setCapability("build", "JUnit-Parallel");
capability.setCapability("InternetExplorerDriverServer.IE_ENSURE_CLEAN_SESSION",true);
System.setProperty("webdriver.ie.driver",
"C:\\Users\\path to driver\\IEDriverServer.exe");
driver = new RemoteWebDriver(
new URL("http://localhost:7777".concat("/wd/hub")),
capability
);
}
#Test
public void testSimple() throws Exception {
driver.get("http://www.google.com");
String title = driver.getTitle();
System.out.println("Page title is: " + title);
WebElement element = driver.findElement(By.name("q"));
element.sendKeys("BrowserStack");
element.submit();
driver = new Augmenter().augment(driver);
File srcFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(srcFile, new File("Screenshot.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
#After
public void tearDown() throws Exception {
driver.quit();
}
}