Spread the love

How To Avoid Selenium Crash While Sending A Huge Text Using SendKeys?

Spread the love

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..

READ  Object Repository Concept In Selenium

use sendkeys and without using sendkeys

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..

READ  How To Resolve IllegalStateException for IEdriver in Selenium

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!!




Spread the love
Animesh Chatterjeehttps://techtravelhub.com/
I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on ani01104@gamil.com

Related Articles

How to Resolve NoSuchSessionException in selenium free?

How to Resolve NoSuchSessionException in selenium? NoSuchSessionException in Selenium is a tricky...

Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Recent Posts

Super Keyword in Java Simplified

Super Keyword Super Keyword in Java Simplified: Java defines a special...

Learn Object Repository in UFT in An Efficient and Fast Manner

Object Repository in UFT The post will cover the following...

Learn What Primary Tools For Test Automation

Primary Tools For Test Automation According to the Gartner report...

EDITOR PICKS


Spread the love