Pages

Search This Blog

Thursday, August 19, 2010

How to verify html source in selenium

We can verify the html source with selenium commands

<html>
<head>
<title>HTML Source</title>
</head>
<body>
what need to be verifed Text is here we need to verify
</body>
</html>

Script


verifyTrue(selenium.getHtmlSource().matches("^[\\s\\S]*Text is here[\\s\\S]*$"));

above command will verify Text is here in above html srource

verifyTrue(selenium.getHtmlSource().matches("^[\\s\\S]*Text i here[\\s\\S]*$"));

above command will fail because it tries to verify Text i here which is not there in html source

How to handle the input promt dialog in selenium

There are some scenarios where we need to input when our web application need external input data by opening input prompt dialog box. To handle these dialog boxes in automation is tricky task. We can handle these situation with below codes.
Html file is at the bottom

selenium.open("Input_prompt_TestCase.html");

Verify there is no input dilog present while opening this file


assertTrue(!selenium.isPromptPresent());

When then Input prompts select NO first time by the command answerOnNextPrompt|no|

selenium.answerOnNextPrompt("no");

Now click to open the input dialog by clicking the link "Click here to open input dialog"

selenium.click("promptdialog");

Now verify prompt presents or not with the command verifyPromptPresent||
assertTrue(selenium.isPromptPresent());
If it takes time to open the prompt then we can wait till 1 minute with the below code.

boolean Bool = false;
for (int second = 0; second < 60; second++) { 
try { 
if ((selenium.isPromptPresent())) {
Bool = true;
break; 
}
}
catch (Exception ignore) {
}
pause(1000);
}
assertTrue(Bool);


Now after 1 minute verify the prompt presents or not with the command assertPromptPresent||


assertTrue(selenium.isPromptPresent());

If it verifies prompt is there then verify the text what is on prompt with the command
verifyPrompt|Type 'automationtricks' and click OK|


verifyEquals("Type 'automationtricks' and click OK", selenium.getPrompt());

You can also verify the title of input prompt with the command verifyTitle|Test Prompt|


verifyEquals("*Testcases for input Prompt", selenium.getTitle());

Now you just enter automationtricks in input promt with this command
answerOnNextPrompt|automationtricks|


selenium.answerOnNextPrompt("automationtricks");

and then click on ok with command clickAndWait|promptdialog|


selenium.click("promptdialog");

Now you can see the result based on your input


selenium.waitForPageToLoad("5000");
verifyTrue(selenium.isTextPresent("You have entered automationtricks!"));



For Selenium IDE user : How to handle prompt in selenium IDE


<tr>
<td>open</td>
<td>Input_prompt_TestCase.html</td>
<td></td>
</tr>
<tr>
<td>answerOnNextPrompt</td>
<td>automationtricks</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>promptdialog</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>You have entered automationtricks!</td>
<td></td>
</tr>
<tr>
<td>open</td>
<td>Input_prompt_TestCase.html</td>
<td></td>
</tr>
<tr>
<td>answerOnNextPrompt</td>
<td>niraj</td>
<td></td>
</tr>
<tr>
<td>click</td>
<td>promptdialog</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>You have not entered automationtricks!</td>
<td></td>
</tr>


For the above code find Html file below


<html>
<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html">
<script type="text/javascript">
function opendialog() {
if (prompt("Type 'automationtricks' and click OK") == 'automationtricks') {
document.write("You have entered automationtricks!");
}
else{
document.write("You have not entered automationtricks!");
}
}
</script>
<title>Testcases for input Prompt</title>
</head>
<body>
<img style="width: 644px; height: 41px;" alt="banner" src="banner.gif"><br>
<a id="promptdialog" href="javascript:opendialog();">Click here to open input dialog</a>
</body>
</html>