Capture upload file size in Selenium WebDriver - java

Is it possible if I want to capture the file size after I uploaded the files using Selenium WebDriver? Btw I'm using Java. Currently I'm following this one tutorial & here's the code:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class upload {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","webdriver location");
String baseUrl = "http://demo.guru99.com/test/upload/";
WebDriver driver = new ChromeDriver();
driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
// enter the file path onto the file-selection input field
uploadElement.sendKeys("file location");
// check the "I accept the terms of service" check box
driver.findElement(By.id("terms")).click();
// click the "UploadFile" button
driver.findElement(By.name("send")).click();
}
}
I want to print the file size output in console. May I know is this action is possible or not? Thanks.

Do you mean something like this? It prints the size of the file uploaded prior to uploading.
import java.io.File;
String fileName = "C:/users/something.txt";
File f = new File(fileName);
long fileSize = f.length();
System.out.format("The size of the file: %d bytes", fileSize);

Related

OCR implementation using using java

I have written java code to convert images into text using java.But my code is taking only single image as input . I want that the program should fetch images from a folder and then run the OCR on it.
My code is:
import java.io.FileOutputStream;
import org.bytedeco.javacpp.*;
import org.junit.Test;
import static org.bytedeco.javacpp.lept.*;
import static org.bytedeco.javacpp.tesseract.*;
import static org.junit.Assert.assertTrue;
import java.io.File;
public class BasicTesseractExampleTest {
#Test
public void givenTessBaseApi_whenImageOcrd_thenTextDisplayed() throws Exception {
BytePointer outText;
TessBaseAPI api = new TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api.Init(".", "ENG") != 0) {
System.err.println("Could not initialize tesseract.");
System.exit(1);
}
PIX image = pixRead("IMG_0012 (1).jpg");
api.SetImage(image);
// Get OCR result
outText = api.GetUTF8Text();
String string = outText.getString();
assertTrue(!string.isEmpty())
System.out.println(str);
// Destroy used object and release memory
api.End();
outText.deallocate();
pixDestroy(image);
}
}
To read a list of files out of a given Path use for example:
File f = new File("C:/programs");
File[] fileArray = f.listFiles();
Now you can check every File out of the fileArray if it is a directory and skip that with:
if(fileArray[0].isDirectory()) continue;
To find the images you can check for example the ending of the filename with:
fileArray[0].getName().endsWith(".jpg")
Do this check for all files out ouf the fileArray and call your method with the right files. To check the right file you have to change this line of your code:
PIX image = pixRead("IMG_0012 (1).jpg");
and add the fileArray[?] where the ? must be replaced with the right number.

Selenium Webdriver: Element Not Visible Exception

Here is my code to click a simple login button on this Website
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Reports {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://platform.drawbrid.ge");
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
driver.findElement(By.xpath(".//*[#id='_loginButton']")).click();
}
}
I am getting following error:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 2.05 seconds
You have two buttons with given xpath on this page, first is not visible, thats why you are getting ElementNotVisibleException
One is under <div class="loginPopup">
Second (the one you need) is under <div class="page">
So change your xpath to look like this, and it will fix your problem:
By.xpath("//div[#class='page']//div[#id='_loginButton']")
There are even 3 elements with id="_loginButton" on the page, and only one is visible - the one located inside the login form, you can get it by a CSS selector:
By.cssSelector("form#_loginForm div#_loginButton")
There are 3 occurrences of id="_loginButton".
Used the id="_loginButton" under class="signIn" by cssSelector to get the exact button in the page.
By.cssSelector("div.signIn div#_loginButton")
Webdriver may throw an ElementNotVisible exception in-case there are multiple elements with the same locator and if Webdriver has already operated upon one of the element matching the locator.
In such scenarios you can first get the size of the element using
int var_ele_size= driver.findElements(By.xpath("locator")).size();
and then take the first element from the list and click on the element.
driver.findElements(By.xpath("locator")).get(var_ele_size-1).click();
public static void Listget (WebDriver driver) throws Exception
{
Thread.sleep(5000);
UtilityMethod.getAppLocaters(driver, "closeicon").click();
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//li[#class='parent dropdown aligned-left']"));
Thread.sleep(5000);
action.moveToElement(we).build().perform();
List<WebElement>links = driver.findElements(By.xpath("//span[#class='menu-title']"));
int total_count = links.size();
System.out.println("Total size :=" +total_count);
for(int i=0;i<total_count;i++)
{
WebElement element = links.get(i);
String text = element.getAttribute("innerHTML");
System.out.println("linksnameis:=" +text);
try{
File src = new File("D:ReadFile.xlsx");
FileInputStream fis = new FileInputStream(src);
XSSFWorkbook wb=new XSSFWorkbook(fis);
XSSFSheet sh = wb.getSheetAt(0);
sh.createRow(i).createCell(1).setCellValue(text);
FileOutputStream fos = new FileOutputStream(new File("D:/ReadFile.xlsx"));
wb.write(fos);
fos.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
}
Make sure your window on the remote server is big enough so the elements are not hidden because of space constraints ..
This worked for me: (I use c#)
driver.Manage().Window.Size = new System.Drawing.Size(1928, 1060);
You could try:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your locator value")));
Or
wait.until(ExpectedConditions.ElementIsVisible(By.xpath("your locator value")));

How to get pictures with names from an xls file using Apache POI

Using workbook.getAllPictures() I can get an array of picture data but unfortunately it is only the data and those objects have no methods for accessing the name of the picture or any other related information.
There is a HSSFPicture class which would contain all the details of the picture but how to get for example an array of those objects from the xls?
Update:
Found SO question How can I find a cell, which contain a picture in apache poi which has a method for looping through all the pictures in the worksheet. That works.
Now that I was able to try the HSSFPicture class I found out that the getFileName() method is returning the file name without the extension. I can use the getPictureData().suggestFileExtension() to get a suggested file extension but I really would need to get the extension the picture had when it was added into the xls file. Would there be a way to get it?
Update 2:
The pictures are added into the xls with a macro. This is the part of macro that is adding the images into the sheet. fname is the full path and imageName is the file name, both are including the extension.
Set img = Sheets("Receipt images").Pictures.Insert(fname)
img.Left = 10
img.top = top + 10
img.Name = imageName
Set img = Nothing
The routine to check if the picture already exists in the Excel file.
For Each img In Sheets("Receipt images").Shapes
If img.Name = imageName Then
Set foundImage = img
Exit For
End If
Next
This recognizes that "image.jpg" is different from "image.gif", so the img.Name includes the extension.
The shape names are not in the default POI objects. So if we need them we have to deal with the underlying objects. That is for the shapes in HSSF mainly the EscherAggregate (http://poi.apache.org/apidocs/org/apache/poi/hssf/record/EscherAggregate.html) which we can get from the sheet. From its parent class AbstractEscherHolderRecord we can get all EscherOptRecords which contains the options of the shapes. In those options are also to find the groupshape.shapenames.
My example is not the complete solution. It is only provided to show which objects could be used to achieve this.
Example:
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.InputStream;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.hssf.record.*;
import org.apache.poi.ddf.*;
import java.util.List;
import java.util.ArrayList;
class ShapeNameTestHSSF {
public static void main(String[] args) {
try {
InputStream inp = new FileInputStream("workbook1.xls");
Workbook wb = WorkbookFactory.create(inp);
Sheet sheet = wb.getSheetAt(0);
EscherAggregate escherAggregate = ((HSSFSheet)sheet).getDrawingEscherAggregate();
EscherContainerRecord escherContainer = escherAggregate.getEscherContainer().getChildContainers().get(0);
//throws java.lang.NullPointerException if no Container present
List<EscherRecord> escherOptRecords = new ArrayList<EscherRecord>();
escherContainer.getRecordsById(EscherOptRecord.RECORD_ID, escherOptRecords);
for (EscherRecord escherOptRecord : escherOptRecords) {
for (EscherProperty escherProperty : ((EscherOptRecord)escherOptRecord).getEscherProperties()) {
System.out.println(escherProperty.getName());
if (escherProperty.isComplex()) {
System.out.println(new String(((EscherComplexProperty)escherProperty).getComplexData(), "UTF-16LE"));
} else {
if (escherProperty.isBlipId()) System.out.print("BlipId = ImageId = ");
System.out.println(((EscherSimpleProperty)escherProperty).getPropertyValue());
}
System.out.println("=============================");
}
System.out.println(":::::::::::::::::::::::::::::");
}
FileOutputStream fileOut = new FileOutputStream("workbook1.xls");
wb.write(fileOut);
fileOut.flush();
fileOut.close();
} catch (InvalidFormatException ifex) {
} catch (FileNotFoundException fnfex) {
} catch (IOException ioex) {
}
}
}
Again: This is not a ready to use solution. A ready to use solution cannot be provided here, because of the complexity of the EscherRecords. Maybe to get the correct EscherRecords for the image shapes and their related EscherOptRecords, you have recursive to loop through all EscherRecords in the EscherAggregate checking whether they are ContainerRecords and if so loop through its children and so on.
Start here:
http://poi.apache.org/spreadsheet/quick-guide.html#Images
this tutorial can help you to extract an image's information from an xls spreadsheet using Apache POI

How to capture a remote website using Java Selenium

can anyone tell me how I can capture a webpage using Java Selenium? With an example....
See here: Capturing screenshots from remote Selenium RC.
In essence:
"To solve this you can use the captureScreenshotToString and captureEntirePageScreenshotToString commands, which return a Base64 encoded String of the screenshot, which you can then decode and save to disk on your testrunner machine."
I think this is what you are looking for. But try to be mores specific if it is not.
captureEntirePageScreenshot (
filename,kwargs )
Saves the entire contents of the current window canvas to a PNG file.
Contrast this with the
captureScreenshot command, which
captures the contents of the OS
viewport (i.e. whatever is currently
being displayed on the monitor), and
is implemented in the RC only.
Currently this only works in Firefox
when running in chrome mode, and in IE
non-HTA using the EXPERIMENTAL
"Snapsie" utility. The Firefox
implementation is mostly borrowed from
the Screengrab! Firefox extension.
Please see http://www.screengrab.org
and http://snapsie.sourceforge.net/
for details.
Arguments:
* filename - the path to the file to persist the screenshot as. No
filename extension will be appended by
default. Directories will not be
created if they do not exist, and an
exception will be thrown, possibly by
native code.
* kwargs - a kwargs string that modifies the way the screenshot
is captured. Example:
"background=#CCFFDD" . Currently valid
options:
background
the background CSS for the HTML document. This may be useful
to set for capturing screenshots of
less-than-ideal layouts, for example
where absolute positioning causes the
calculation of the canvas dimension to
fail and a black background is exposed
(possibly obscuring black text).
public static void getSnapShot(WebDriver driver, String event) {
{
try {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage originalImage = ImageIO.read(scrFile);
//int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
jpeg.setAlignment(Image.MIDDLE);
++index;
} catch (Exception e) {
e.printStackTrace();
}
}
}
I like to use PhantomJS driver for taking screenshots.
public class Test {
public static void main(String[] args) {
//PhantomJS headless driver
File file = new File("D:\\Webdriver\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe");
System.setProperty("phantomjs.binary.path", file.getAbsolutePath());
WebDriver driver = new PhantomJSDriver();
//Set Size here
driver.manage().window().setSize(new Dimension(1600,900));
//To wait until the element get visible or invisible.
WebDriverWait wait = new WebDriverWait(driver, 25);
//To access url.
driver.get("https://www.google.co.in");
//For wait until the element get visible.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("lst-ib")));
File shot=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(shot, new File("D:\\Webdriver\\Capture.jpg"));
}
}
It takes full page screenshot of chrome webpage.
System.setProperty("webdriver.chrome.driver", "F:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(options);
String baseUrl = "https://www.google.co.in";
driver.get(baseUrl);
String fullscreen =Keys.chord(Keys.F11);
driver.findElement(By.cssSelector("body")).sendKeys(fullscreen);
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File("F://test.png");
FileUtils.copyFile(SrcFile, DestFile);
driver.close();
I see a lot of answers explaining screenshots, but just incase you are asking how to get the entire source of the page use the following method:
String pageSource = driver.getPageSource();
Here is a runnable example.

Java noob question - how to store a string to a new text file

Here is my scenario:
Selenium grabbed some text on the html page and convert it to a string (String store_txt = selenium.getText("text");) - the text is dynamically generated.
Now I want to store this string into a new text file locally every time I run this test, should I use FileWriter? Or is it as simple as writing a System.out.println("string");?
Do I have to write this as a class or can I write a method instead?
Thanks in advance!!
Use createTempFile to create a new file every time, use FileWriter to write to the file.
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
public class Main {
public static void main(String[] args) throws IOException {
File f = File.createTempFile("selenium", "txt");
FileWriter writer = new FileWriter(f);
writer.append("text");
}
}
Yes, you need to use a FileWriter to save the text to file.
System.out.println("string");
just prints to the screen in console mode.
Always remember to close the filewriter afterwards using
writer.close()
Otherwise you could end up with a half written file.

Categories