Well, to answer your question - "Is it possible?" - Definitely Yes !!
And how ? - Well, it really depends on the chat application and how automation friendly is its interface.
Take for example this simple chat application (its like a chat bot) -
http://www.unionplatform.com/?page_id=2247
(I have used its direct URL in the sample code).
Check the code below, I am just checking the <span>
tag for the updated value and looped it 5 times to read that the user has entered and reply back with the same message + the loop count (to distinguish the message).
You may follow a similar approach, however again as I mentioned it all depends on the chat app.
Heres the code:
public class WhiteBoard {
public WebDriver driver;
public String css_chat_text = "div#chatPane>span";
public String css_input_msg = "input#outgoing";
public String css_send_button = "input[value='Send']";
public String last_message = "";
public static void main(String[] args) throws Exception {
WhiteBoard objTest = new WhiteBoard();
objTest.chatBot();
}
public void chatBot() throws Exception{
driver = new FirefoxDriver();
driver.navigate().to("http://unionplatform.com/samples/orbitermicro/UnionChatPart1/chat.html");
List<WebElement> objChats = driver.findElements(By.cssSelector(css_chat_text));
if(objChats.size() > 0){
System.out.println("Starting chattter...");
String status;
//loop until chat ready
do{
Thread.sleep(3000);
objChats = driver.findElements(By.cssSelector(css_chat_text));
status = objChats.get(objChats.size()-1).getText().toString();
System.out.println(status);
}while(!status.contains("Chat ready!"));
Thread.sleep(3000);
//start chatting...
sendMessage("Hello...");
for(int i=0; i<5; i++) {
objChats = driver.findElements(By.cssSelector(css_chat_text));
last_message = objChats.get(objChats.size()-1).getText().toString().trim();
sendMessage("LastMsg:" + last_message + "#" +i);
Thread.sleep(5000);
}
}
else{
System.out.println("Chat not found...");
}
}
public void sendMessage(String message){
//start chatting...
List<WebElement> objInput = driver.findElements(By.cssSelector(css_input_msg));
if(objInput.size() > 0){
objInput.get(0).sendKeys(message);
System.out.println("Sent message..." + message);
List<WebElement> btnSend = driver.findElements(By.cssSelector(css_send_button));
btnSend.get(0).click();
}
}
}
The sleeps are just to crudely kept to wait until the chatbox gets updated, ideally I would keep checking the <span>
count and proceed once incremented.
The console output of the above code would look like -
https://gist.github.com/anonymous/f1eef7198648d95f1d01
Also here's another interesting question on SO that may be of help -
How to test chat web app