If you need to generate alert during your software test case execution then you can use java script Executor in selenium webdriver software testing tool. Java Script Executor is very helpful interface of selenium webdriver because using it we can execute any java script. I think manual alert generation in webdriver test is required only in rare case but main purpose of this post is to explore Java Script Executor. You can view more examples of JavascriptExecutor on THIS LINK.
If you remember, we can generate alert in selenium IDE software testing tool too using "runScript" command. THIS POST will describe you how to generate alert in selenium IDE software testing tool using "runScript" command with example.
Now let me describe you how to generate alert in selenium webdriver software automation if required during test case execution of your software web application.
Example 3 : JavascriptExecutor to generate alert in selenium webdriver
@Test
public void test () throws InterruptedException
{
//Generating Alert Using Javascript Executor
JavascriptExecutor javascript = (JavascriptExecutor) driver;
javascript.executeScript("alert('Test Case Execution Is started Now..');");
Thread.sleep(2000);
driver.switchTo().alert().accept();
}
In above example, Java Script Executor will simply execute the javascript and generate alert. You can your own alert text in it. Try it in your own software application test if required.
On typing " javascript.executeScript("alert('Test Case Execution Is started Now..');");" i get the following error notification,
ReplyDelete"The method executeScript(String) is undefined for the type javasxcriptexecutor
This comment has been removed by the author.
ReplyDeletevar username="abc"
ReplyDelete.execute(function(result,username){
alert(username)
})
am getting the result as undefined
var username="abc"
ReplyDelete.execute(function(result,username){
alert(username)
})
am getting result as undefined
Worked perfectly fine.. JavasrciptExecutorObject.executeScript("alert('Thanks a lot for this tutorial');");
ReplyDelete