Selenium
Selenium

Share and Enjoy !

sendkey is a very useful and most frequently used command while developing test through java and Selenium.
But recently I have seen Selenium viz underlying browser [like-Mozilla Fireox mainly] hangs when I try to send more than 1000 characters.

let us assume I have a note to process.There is a textarea in the application which will accept 2500 characters. Now the code I have tried..

WebElement test = driver.findElement(By.id(TXT_APPT_NOTES));
test.sendKeys(ApptNotes);

if the ApptNotes is more than 1000 character , selenium viz Mozilla Firefox hangs.
So what could be a good solution–
Well there is something called clipboard Which is capable of storing huge text or information.I came across a nice post from java practices[http://www.javapractices.com/topic/TopicAction.do?Id=82]. This gave me an instant idea about how to overcome this problem.

We have to code accordingly and need to use this nice feature..

let me go step by step…

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class TextTransfer implements ClipboardOwner{

public static void main(String[] args) {
TextTransfer textTransfer = new TextTransfer();
WebDriver driver=new FirefoxDriver();
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
//Copying huge chunk of code to clipboard
WebElement test = driver.findElement(By.id(TXT_APPT_NOTES));
textTransfer.setClipboardContents(test.getText().toString());

}

private void setClipboardContents(String text2Copy) {
// TODO Auto-generated method stub
StringSelection stringSelection = new StringSelection(text2Copy);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);

}

@Override
public void lostOwnership(Clipboard arg0, Transferable arg1) {
// TODO Auto-generated method stub

}
}

This code will copy the huge text into clipboard.
Now getting text from clipboard is having two ways..

use sendkeys and without using sendkeys

READ  Learn How to Structure of Automation Test Case Free And Fast in 2020

Using sendKeys function.
say I want to set it to a webElement

WebElement test = driver.findElement(By.id(TXT_APPT_NOTES_2));
test.sendKeys(ctrl+v)

The second approach where we will not use the sendkeys..

public String getClipboardContents() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor)
;
if (hasTransferableText) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception ex){
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}

The entire code will be like below:

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.ClipboardOwner;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class TextTransfer implements ClipboardOwner{

public static void main(String[] args) {
TextTransfer textTransfer = new TextTransfer();
WebDriver driver=new FirefoxDriver();
Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
//Copying huge chunk of code to clipboard
WebElement test = driver.findElement(By.id(TXT_APPT_NOTES));
textTransfer.setClipboardContents(test.getText().toString());
//getting the huge text from clipboard
String getClipboardText=textTransfer.getClipboardContents();
}

private void setClipboardContents(String text2Copy) {
// TODO Auto-generated method stub
StringSelection stringSelection = new StringSelection(text2Copy);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(stringSelection, this);

}

@Override
public void lostOwnership(Clipboard arg0, Transferable arg1) {
// TODO Auto-generated method stub

}
public String getClipboardContents() {
String result = "";
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
//odd: the Object param of getContents is not currently used
Transferable contents = clipboard.getContents(null);
boolean hasTransferableText =
(contents != null) &&
contents.isDataFlavorSupported(DataFlavor.stringFlavor)
;
if (hasTransferableText) {
try {
result = (String)contents.getTransferData(DataFlavor.stringFlavor);
}
catch (Exception ex){
System.out.println(ex);
ex.printStackTrace();
}
}
return result;
}

}

hope that helps!!

Share and Enjoy !

1 COMMENT

  1. such a nice blog it is very important information it will prevent selenium from crash thanks for sharing valuable information.

LEAVE A REPLY

Please enter your comment!
Please enter your name here